Css: Center Element Within a ≪Div≫ Element

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>

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.

HTML center element inside div

You just have to add text-align:center to .MainDiv class and you are done.

See here

.MainDiv {
height:25vh;
background-color:transparent;
width:50%;
margin: 0 auto;
text-align:center;
}

CSS center content inside div

To center a div, set it's width to some value and add margin: auto.

#partners .wrap {
width: 655px;
margin: auto;
}

EDIT, you want to center the div contents, not the div itself. You need to change display property of h2, ul and li to inline, and remove the float: left.

#partners li, ul, h2 {
display: inline;
float: none;
}

Then, they will be layed out like normal text elements, and aligned according to text-align property of their container, which is what you want.

How to center a p element inside a div container?

You dont need absolute positioning
Use

p {
text-align: center;
line-height: 100px;

}

And adjust at will...

If text exceeds width and goes more than one line

In that case the adjust you can do is to include the display property in your rules as follows;

(I added a background for a better view of the example)

div
{
width:300px;
height:100px;
display: table;
background:#ccddcc;
}


p {
text-align:center;
vertical-align: middle;
display: table-cell;
}

Play with it in this JBin

Sample Image

css center div inside container?

The CSS property for the text align is called text-align not align like in the inline DOM attribute.


If you want to center a block element (like div, p, ul, etc...) itself you need to set its width and set the horizontal margins to auto.

For example, the following code will make every div inside an element with the MyContainer class 80% the size of its parent and center it in the middle of its container.

.MyContainer div {
margin: 0 auto;
width: 80%;
}

Code snippet

div {
border: 2px solid black;
margin: 10px;
}

.MyContainer div {
margin: 10px auto;
width: 80%;
}

.centered {
text-align: center;
}
<div class="MyContainer">
<div>Inner DIVs are centered
<div class="centered">Here the text is also centered</div>
</div>
</div>

Center a div in CSS

The text-align: center; only centers the element's inline contents, not the element itself.

If it is a block element (a div is), you need to set margin: 0 auto;, else if it is an inline element, you need to set the text-align: center; on its parent element instead.

The margin: 0 auto; will set top and bottom margin to 0 and left and right margin to auto (of the same size) so that it automagically puts itself in the center. This only works if the block element in question has a known width (either fixed or relative), else it cannot figure where to start and end.

How can I center text (horizontally and vertically) inside a div block?

If it is one line of text and/or image, then it is easy to do. Just use:

text-align: center;
vertical-align: middle;
line-height: 90px; /* The same as your div height */

That's it. If it can be multiple lines, then it is somewhat more complicated. But there are solutions on http://pmob.co.uk/. Look for "vertical align".

Since they tend to be hacks or adding complicated divs... I usually use a table with a single cell to do it... to make it as simple as possible.


Update for 2020:

Unless you need make it work on earlier browsers such as Internet Explorer 10, you can use flexbox. It is widely supported by all current major browsers. Basically, the container needs to be specified as a flex container, together with centering along its main and cross axis:

#container {
display: flex;
justify-content: center;
align-items: center;
}

To specify a fixed width for the child, which is called a "flex item":

#content {
flex: 0 0 120px;
}

Example: http://jsfiddle.net/2woqsef1/1/

To shrink-wrap the content, it is even simpler: just remove the flex: ... line from the flex item, and it is automatically shrink-wrapped.

Example: http://jsfiddle.net/2woqsef1/2/

The examples above have been tested on major browsers including MS Edge and Internet Explorer 11.

One technical note if you need to customize it: inside of the flex item, since this flex item is not a flex container itself, the old non-flexbox way of CSS works as expected. However, if you add an additional flex item to the current flex container, the two flex items will be horizontally placed. To make them vertically placed, add the flex-direction: column; to the flex container. This is how it works between a flex container and its immediate child elements.

There is an alternative method of doing the centering: by not specifying center for the distribution on the main and cross axis for the flex container, but instead specify margin: auto on the flex item to take up all extra space in all four directions, and the evenly distributed margins will make the flex item centered in all directions. This works except when there are multiple flex items. Also, this technique works on MS Edge but not on Internet Explorer 11.


Update for 2016 / 2017:

It can be more commonly done with transform, and it works well even in older browsers such as Internet Explorer 10 and Internet Explorer 11. It can support multiple lines of text:

position: relative;
top: 50%;
transform: translateY(-50%);

Example: https://jsfiddle.net/wb8u02kL/1/

To shrink-wrap the width:

The solution above used a fixed width for the content area. To use a shrink-wrapped width, use

position: relative;
float: left;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);

Example: https://jsfiddle.net/wb8u02kL/2/

If the support for Internet Explorer 10 is needed, then flexbox won't work and the method above and the line-height method would work. Otherwise, flexbox would do the job.

How can I horizontally center an element?

You can apply this CSS to the inner <div>:

#inner {
width: 50%;
margin: 0 auto;
}

Of course, you don't have to set the width to 50%. Any width less than the containing <div> will work. The margin: 0 auto is what does the actual centering.

If you are targeting Internet Explorer 8 (and later), it might be better to have this instead:

#inner {
display: table;
margin: 0 auto;
}

It will make the inner element center horizontally and it works without setting a specific width.

Working example here: