Duncan Duncan - 6 months ago 14
HTML Question

Center divs inside div

I have two tables inside divs inside another div:

HTML

<div class="container center">
<div class="thing">
<table class="tabel center" style="width:230px">
<tr><td>Example</td></tr>
</table>
</div>

<div class="thing">
<table class="tabel center" style="width:230px">
<tr><td>Example</td></tr>
</table>
</div>
</div>


CSS

.container {
width: 100%;
overflow: hidden;
float: left;
border: 1px solid red;
text-align: center;
}
.tabel {
border: 1px solid black;
border-collapse: collapse;
padding: 5px;
}
.thing {
width: 50%;
float: left;
min-width: 230px;
}

.center {
margin: 0 auto;
}


The two cells are centered correctly

[Image deleted by host]

but when the width of the screen gets too small, the cells go underneith each other (what I want), but they no longer get centered:

[Image deleted by host]

So what I want them to look like:

[Image deleted by host]

Answer

You have to remove float: left;, set display: inline-block; and eliminate white space between them.

.thing {
    width: 50%;
    display: inline-block;
    /* float: left; */
    min-width: 230px;
    vertical-align: top;
}

fiddle: http://jsfiddle.net/3g7xk0sj/1/