Jump to content
newcoder1010

How to center a div

Recommended Posts

Hello, 

HTML:

<section class ="patent"> 

<div class ="child">

</div>

</section>

CSS:

 

.parent{text-align:center;}

 It centers everything inside child class. 

.child{margin:0 auto;}

It does nothing.

How can I place div class child in the center of the page? I will have other div classes inside child classes. I would like to center child div instead of centering elements inside the child class. Please advise. 

 

Share this post


Link to post
Share on other sites

The child div needs a width. The concept of centering doesn't make sense when the element is as wide as the screen.

Share this post


Link to post
Share on other sites

If you want to centre .child using text-align, it must be a inline-block which makes it act like text, then you need to counteract the text align: center; by resetting it

.parent {text-align: center;}
.parent > .child{display: inline-block; text-align: initial;}

Note your section class has 'patent' NOT 'parent'

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×