Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a div center align in HTML [duplicate]

Tags:

html

css

Possible Duplicate:
How to horizontally center a div?

One simple way to make an object centered in HTML is using align='center', but it's not working for a div.

I tried:

style='text-align:center'; style='left:50%'; 

I even true a center tag

<center> 

But I can't make my target div to be center.

like image 549
Mac Taylor Avatar asked Apr 22 '10 13:04

Mac Taylor


People also ask

How can I center a div within another div duplicate?

To move the inner div container to the centre of the parent div we have to use the margin property of style attribute. We can adjust the space around any HTML element by this margin property just by providing desired values to it.

How set Align Center in div in HTML?

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 you center copy in HTML?

Using the <center></center> tags One way to center text or put it in the middle of the page is to enclose it within <center></center> tags. Inserting this text within HTML code would yield the following result: Center this text!

How do I center a div in HTML without CSS?

Without setting an explicit width, the <div> tag will automatically expand to 100% of the width of its parent. Therefore, setting margin: 0 auto; will make it center -- with 0px on both the left and right.


2 Answers

<!DOCTYPE html>  <html>    <head>      <title>Center</title>    </head>    <body>        <div style="text-align: center;">        <div style="width: 500px; margin: 0 auto; background: #000; color: #fff;">This DIV is centered</div>      </div>      </body>  </html>

Tested and worked in IE, Firefox, Chrome, Safari and Opera. I did not test IE6. The outer text-align is needed for IE. Other browsers (and IE9?) will work when you give the DIV margin (left and right) value of auto. Margin "0 auto" is a shorthand for margin "0 auto 0 auto" (top right bottom left).

Note: the text is also centered inside the inner DIV, if you want it to remain on the left side just specify text-align: left; for the inner DIV.

Edit: IE 6, 7, 8 and 9 running on the Standards Mode will work with margins set to auto.

like image 171
Tower Avatar answered Sep 24 '22 05:09

Tower


I think that the the align="center" aligns the content, so if you wanted to use that method, you would need to use it in a 'wraper' div - a div that just wraps the rest.

text-align is doing a similar sort of thing.

left:50% is ignored unless you set the div's position to be something like relative or absolute.

The generally accepted methods is to use the following properties

width:500px; // this can be what ever unit you want, you just have to define it margin-left:auto; margin-right:auto; 

the margins being auto means they grow/shrink to match the browser window (or parent div)

UPDATE

Thanks to Meo for poiting this out, if you wanted to you could save time and use the short hand propery for the margin.

margin:0 auto; 

this defines the top and bottom as 0 (as it is zero it does not matter about lack of units) and the left and right get defined as 'auto' You can then, if you wan't override say the top margin as you would with any other CSS rules.

like image 40
thecoshman Avatar answered Sep 20 '22 05:09

thecoshman