user1432269 user1432269 - 1 year ago 70
CSS Question

CSS - center big div inside smaller one + hidden overflow


<div class="container">
<div class="wide1">test</div>

<div class="container">
<div class="wide2">test</div>

<div class="container">
<div class="wide3">test</div>


.container { width: 100%; text-align: center; margin: 20px 0; overflow: hidden; }
.container div { background: red; margin: 0 auto; }

.wide1 { width: 100px; }
.wide2 { width: 600px; }
.wide3 { width: 1100px; }

I would like to have all "test" in one vertical line (centered). I need use it in situation where I don't know width of the inner divs.

Answer Source

Got it! But with JavaScript ...

    $(".container > *").each(function(){
       var m = ($(this).parent().innerWidth() - $(this).outerWidth())/2;
       $(this).css("margin-left", m);

Maybe there is a solution with pure CSS (?)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download