How can I align 3 divs in one line left-center-right without having to define explicit sizes?
Left should be aligned most to the left edge, and right to the right edge.
The following does not work:
<div style="float: left;">
left
</div>
<div style="float: right;">
right
</div>
<div style="margin: 0 auto;">
center
</div>
Add a wrapper div
and give text-align:center
CSS
.wrap{
text-align:center
}
HTML
<div class="wrap">
<div class="left">
left
</div>
<div class="right">
right
</div>
<div class="center">
center sdv dg sdb sdfbh sdfhfdhh h dfh
</div>
</div>
DEMO
Heres an example of how to do this by placing the floats in the correct order.
jsFiddle Example
<div class="square" style="float: left;">left</div>
<div class="square" style="float: right;">right</div>
<div class="square" style="margin:0 auto !important;">center</div>
.square {
width:50px;
height:50px;
background-color:#ff0000;
text-align:center;
border: 1px solid #000;
}
<div style="width:100%;margin:0 auto; padding: 0">
<div style=" float:left;width:32%;border: thin solid black">
left
</div>
<div style=" float:left;width:32%;border: thin solid black">
center
</div>
<div style=" float:left;width:32%;border: thin solid black">
right
</div>
</div>
<div style="clear:both">
</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With