Jump to content

How to center a div


newcoder1010
 Share

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. 

 

Link to comment
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'

Link to comment
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
 Share

×
×
  • Create New...