Align a Div to Center

How to align a <div> to the middle (horizontally/width) of the page

<body>
<div style="width:800px; margin:0 auto;">
centered content
</div>
</body>

How can I center a div within another div?

You need to set the width of the container (auto won't work):

#container {
width: 640px; /* Can be in percentage also. */
height: auto;
margin: 0 auto;
padding: 10px;
position: relative;
}

The CSS reference by MDN explains it all.

Check out these links:

  • auto - CSS reference | MDN
  • margin - CSS reference | MDN
  • What is the meaning of auto value in a CSS property - Stack Overflow

In action at jsFiddle.

How to align <div> to center?

margin auto do not work until you do not give the width of that element.

 <div style="margin:0px auto; display:block; width:500px;">

<asp:Menu ID="Menu1" runat="server" Orientation="Horizontal">
<Items>
<asp:MenuItem Text="Home" NavigateUrl="Home.aspx" />
<asp:MenuItem Text="test" NavigateUrl="test.aspx" />
<asp:MenuItem Text="Reports" NavigateUrl="Reports.aspx" />
<asp:MenuItem Text="Review" NavigateUrl="Review.aspx" />
<asp:MenuItem Text="Management" NavigateUrl="mg.aspx" />
<asp:MenuItem Text="Scripts" NavigateUrl="scr.aspx" />
<asp:MenuItem Text="Notification" NavigateUrl="Notification.aspx" />
</Items>
</asp:Menu>
</div>

How to align 3 divs (left/center/right) inside another div?

With that CSS, put your divs like so (floats first):

<div id="container">
<div id="left"></div>
<div id="right"></div>
<div id="center"></div>
</div>

P.S. You could also float right, then left, then center. The important thing is that the floats come before the "main" center section.

P.P.S. You often want last inside #container this snippet: <div style="clear:both;"></div> which will extend #container vertically to contain both side floats instead of taking its height only from #center and possibly allowing the sides to protrude out the bottom.

<div align=center></div> is this bad?

You could have a class of center in your CSS which works for most block elements.

For the CSS code:

.center {margin:0 auto;}

In your HTML:

<div class="center">Content Here</div>

If you wanted the text to be aligned center aswell as the div then you would just update the above CSS to be:

.center {margin:0 auto; text-align:center;}

Align a div to center

There is no float to center per se. If you want to center a block element inside another do this:

<div id="outer">
<div id="inner">Stuff to center</div>
</div>

with:

#outer { width: 600px; }
#inner { width: 250px; margin: 0 auto; }

Now that won't make the text wrap around it (like it would with a float left or right) but like I said: there is no float center.

CSS: center element within a <div> element

Set text-align:center; to the parent div, and margin:auto; to the child div.

#parent {    text-align:center;    background-color:blue;    height:400px;    width:600px;}.block {    height:100px;    width:200px;    text-align:left;}.center {    margin:auto;    background-color:green;}.left {    margin:auto auto auto 0;    background-color:red;}.right {    margin:auto 0 auto auto;    background-color:yellow;}
<div id="parent">    <div id="child1" class="block center">        a block to align center and with text aligned left    </div>    <div id="child2" class="block left">        a block to align left and with text aligned left    </div>    <div id="child3" class="block right">        a block to align right and with text aligned left    </div></div>

Center a DIV horizontally and vertically

After trying a lot of things I find a way that works. I share it here if it is useful to anyone. You can see it here working: http://jsbin.com/iquviq/30/edit

.content {
width: 200px;
height: 600px;
background-color: blue;
position: absolute; /*Can also be `fixed`*/
left: 0;
right: 0;
top: 0;
bottom: 0;
margin: auto;
/*Solves a problem in which the content is being cut when the div is smaller than its' wrapper:*/
max-width: 100%;
max-height: 100%;
overflow: auto;
}


Related Topics



Leave a reply



Submit