Skip to content Skip to sidebar Skip to footer

My Div Will Not Center

I am trying to center everything on my page, everything is centered except the div I created. I don't know why. Here is my HTML:

Solution 1:

If you apply text-align: center to the section element, that will cause all the inline child elements to be centered, which is what you want. However, since #mydiv3 is a block element, it won't be affected by the text-align property of its parent container.

On the other hand, is you applied display: inline-block to #mydiv3, then it would align to the center.

The margin: 0 auto works as demonstrated because you also assigned a specific width to #mydiv3.

Either approach is valid.

Solution 2:

Add margin: 0 auto; to your #mydiv3 rule. See this JSFiddle

Solution 3:

here is a link that you can use for help. http://designshack.net/articles/css/how-to-center-anything-with-css/ personally i have used

margin-right:auto;
margin-left:auto;

to center a div. apply that to your #mydiv3. Hopefully this helps

Solution 4:

Try this inside the #mydiv3:

margin:auto;

See this fiddle.

Solution 5:

To answer why: text-align will align your text to the center, but the div is not centered in its parents. Therefore, you need to set the margin as others have suggested. Of course, one alternative would be to force your div to have the same width as your parent and use text-align, it would work. This has its own trouble, though; width: 100% does not work straight forward.

Post a Comment for "My Div Will Not Center"