Friday, April 6, 2018

css - How to vertically center a div for all browsers?



I want to center a div vertically with CSS. I don't want tables or JavaScript, but only pure CSS. I found some solutions, but all of them are missing Internet Explorer 6 support.




Div to be aligned vertically





How can I center a div vertically in all major browsers, including Internet Explorer 6?


Answer



Below is the best all-around solution I could build to vertically and horizontally center a fixed-width, flexible height content box. It was tested and working for recent versions of Firefox, Opera, Chrome, and Safari.





.outer {
display: table;
position: absolute;

top: 0;
left: 0;
height: 100%;
width: 100%;
}

.middle {
display: table-cell;
vertical-align: middle;
}


.inner {
margin-left: auto;
margin-right: auto;
width: 400px;
/*whatever width you want*/
}





The Content


Once upon a midnight dreary...









View A Working Example With Dynamic Content




I built in some dynamic content to test the flexibility and would love to know if anyone sees any problems with it. It should work well for centered overlays also -- lightbox, pop-up, etc.


No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...