Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to center a wrapper-div and not the content

I want my page to always be centered in the browser without affecting the content( like align-text: center; does). I want to center my wrapper-div.. How do I do this?

Simplifed existing page:

<div id="wrapper">       
    <div id="header">
        Music Heaven
    </div>
    <div id="topmeny">             
    </div>
    <div id="menu">        
    </div>        
    <div id="content">                  
    </div>           
    <div id="bottom">
        SiteAdmin
    </div>
</div>

Entire fiddle: http://jsfiddle.net/EndQb/

like image 342
DoobyScooby Avatar asked Jan 02 '12 12:01

DoobyScooby


People also ask

How do you center a div and its content?

You can do this by setting the display property to "flex." Then define the align-items and justify-content property to “center.” This will tell the browser to center the flex item (the div within the div) vertically and horizontally.

How do I force a div to center?

Simply add text-align center to parent div and set the child div display to inline-block. This will force our div to behave like inline element and therefore subjected text-align center.

How do I center a div horizontally?

To Horizontally centered the <div> element: We can use the property of margin set to auto i.e margin: auto;. The <div> element takes up its specified width and divides equally the remaining space by the left and right margins.

How do you center a child div?

Like last time, you must know the width and height of the element you want to center. Set the position property of the parent element to relative . Then set the child's position property to absolute , top to 50% , and left to 50% . This just centers the top left corner of the child element vertically and horizontally.


2 Answers

 #wrapper {
     width: ...
     margin: 0 auto;
 }

no need to set text-align

like image 135
Fabrizio Calderan Avatar answered Oct 01 '22 22:10

Fabrizio Calderan


Set your style something like for the wrapper div.

#wrapper {

width: 841px;
margin: 0 auto;

}

In this way the wrapper div will always be centered according to your screen width. I hope it helps.

like image 27
samir chauhan Avatar answered Oct 01 '22 21:10

samir chauhan