I am looking to vertically center a <div> in the viewport (browser window) without resorting to Javascript (pure HTML and CSS only). I have several constraints:
- The
divmust be centered vertically in the viewport. Methods I have seen only support centering inside another<div>, which is not what I want. - The height of the
divis not known.
Other constraints:
- The
divmust be aligned to the right. - The
divhas a constant width. - The
divmust support padding. - Other elements will be placed on the web page. The
divacts as a menu. - The div must support a background colour/image.
This gets me close to what I want, but not exactly:
#nav {
position: fixed;
right: 0;
top: 50%;
}
However, the top of the nav is in the middle, not the middle of the nav.
Is there some technique which allows me to center my div with these constraints?