5

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>  
Zo Has
  • 12,599
  • 22
  • 87
  • 149
  • possible duplicate of [How to center DIV in DIV?](http://stackoverflow.com/questions/114543/how-to-center-div-in-div) – ChrisR Dec 21 '11 at 09:57
  • Here are two simple methods to center divs within divs, vertically, horizontally or both (pure CSS): http://stackoverflow.com/a/31977476/3597276 – Michael Benjamin Aug 20 '15 at 15:23
  • Possible duplicate of [How to horizontally center a
    ?](https://stackoverflow.com/questions/114543/how-to-horizontally-center-a-div)
    – lesyk Jul 30 '19 at 08:46

2 Answers2

15

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>  
socha23
  • 10,171
  • 2
  • 28
  • 25
3

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

Joakim Johansson
  • 3,196
  • 1
  • 27
  • 43