Say I have a div that I want to be centered horizontally, and set at a fixed distance from the top or bottom of the window. I want to set its width to say 75%, and its height to be fluid depending on dynamic content. The page will never have content extending past the viewport so it won’t ever scroll. How can I do this with CSS? HTML is here, basically:
<body>
<div id="main">
<div id="div_in_question">
Omg stuff goes here it will probably change though via jQuery.
</div>
</div>
</body>
You can do it like this Demo
HTML
CSS
Explanation: give
position: absolute;to yourdivand the real trick to get it centered horizontally is givemargin-leftwhich will be half of your total width of thedivand giveleftas50%to bring it horizontally centered