Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Center align div inside div?

Tags:

css

How do I center align 3 floating divs inside each other centered on page ?

<div style="width:100%">
     <div style="width:90%">
           <div style="width:80%">
                //Content
           </div>
     </div>
</div>  
like image 644
Zo Has Avatar asked Dec 04 '22 05:12

Zo Has


2 Answers

Use margin: auto in CSS.

<div style="background-color: red; height: 100px; width: 500px; margin: auto;">
     <div style="background-color: green; height: 100px; width: 300px; margin: auto;">
           <div style="background-color: blue; height: 100px; width: 100px; margin: auto;">
                //Content
           </div>
     </div>
</div>  
like image 158
socha23 Avatar answered Dec 29 '22 22:12

socha23


If the element you're centering has a specified width, you can center it with margin: 0 auto

like image 44
Joakim Johansson Avatar answered Dec 30 '22 00:12

Joakim Johansson