0

I'm trying to center div's inside one outter div, but I can't.

My html is something like this :

<div class="outterDiv">
  <div class="innerDivBig">
    <div style="width: 180px; float:left;margin-right: 5px;background-color: yellow;">
      Inner Div
    </div>
    <div style="width: 180px; float:left;margin-right: 5px;background-color: yellow;">
      Inner Div
    </div>
    <div style="width: 180px; float:left;margin-right: 5px;background-color: yellow;">
      Inner Div
    </div>
    <div style="clear:both"/>
  </div>
</div>

And my css is something like this :

.outterDiv{
 width: 600px;
 border: 1px solid #f00;
 text-align: center;
}

.innerDivBig{
  margin: 0 auto;
  display:table;
}

Here is jsfiddle.

Prasath V
  • 1,314
  • 4
  • 18
  • 34
Boky
  • 10,369
  • 24
  • 79
  • 150

2 Answers2

1
.outterDiv{
 width: 600px;
 border: 1px solid #f00;
 text-align:center;
}

.innerDivBig{

  display: inline-block;
}

https://jsfiddle.net/2a8514nf/7/

Tuhin
  • 3,170
  • 1
  • 13
  • 25
0

UPDATE: https://jsfiddle.net/2a8514nf/4/

I use display: table because the browser calculates the width to fit all the child elements width display: table-cell so that you wont have to worry about the width.
I also use padding instead of margin since it does not expand the element so the parent size remains the same.

.outer {
    width: 500px;
    border: 1px solid #000;
    padding: 15px;
    margin: 0 auto;
}

.inner {
    display: table;
    table-layout: fixed;
    width: 100%;
}

.inner > div {
    display: table-cell;
    padding: 0 5px;
    text-align: center;
}

.inner > div > div {
    padding: 15px;
    text-align: center;
    border: 1px solid #00F;
}
<div class="outer">
    <div class="inner">
        <div>
            <div>Inner Div 1</div>
        </div>
        <div>
            <div>Inner Div 2</div>
        </div>
        <div>
            <div>Inner Div 3</div>
        </div>
    </div>
</div>
Jonathan Nielsen
  • 1,254
  • 14
  • 28