css - Spacing out floating elements with both sides of a container -
i have floating elements laid out grid , want make them flush both left , right sides of container while retaining internal separation.
here's example red squares floating 50x50 divs 10 pixels of separation inside blue div fixed width , auto-height.
http://i.stack.imgur.com/ln8jf.png

<div class='blue' style='width: 110px'> <div class='red'></div> <div class='red'></div> </div> <div class='blue' style='width: 110px'> <div class='red'></div> <div class='red'></div> <div class='red'></div> <div class='red'></div> </div> <div class='blue' style='width: 170px'> <div class='red'></div> <div class='red'></div> <div class='red'></div> <div class='red'></div> <div class='red'></div> <div class='red'></div> </div> here's relevant fiddle: https://jsfiddle.net/sujadkvb/5/
i want make red squares flush left , right sides of blue square. blue square can of several widths , red squares can added or hidden nth-of-type selector modifies margins on specific red squares isn't practical.
this solved giving blue squares negative right-padding css spec disallows negative padding unspecified reason i'm stuck. don't see how can justify allowing negative margins disallowing negative padding.
here 1 way of realizing layout using inline-blocks instead of floated elements.
first, .blue containing block, set following css properties: text-align: justify , line-height: 0.
second, .red child elements, remove float property , add display: inline-block.
the result .red elements inline , spaced evenly in line within width of .blue container. since hard-coded nice value of width .blue, horizontal spacing want between .red blocks (no need set right margin).
the remaining issue text-align: justify final line not stretch fill full width (this how justify setting works in order prevent short lines being stretched out in ugly fashion).
to work around this, add pseudo-element .blue:after inline-block of width 100%. force new line created after red blocks, , result red blocks left/right justified.
the line-height: 0 on .blue , vertical-align: top on .blue:after take care of white space @ bottom due line leading (space above , below baseline of text).
.blue { margin-top: 10px; background-color: #00a2e8; height: auto; overflow: auto; padding-bottom: 10px; text-align: justify; line-height: 0; } .blue:after { content:''; display: inline-block; width: 100%; vertical-align: top; } .red { background-color: red; width: 50px; height: 50px; display: inline-block; margin-top: 10px; } <div class='blue' style='width: 110px'> <div class='red'></div> <div class='red'></div> </div> <div class='blue' style='width: 110px'> <div class='red'></div> <div class='red'></div> <div class='red'></div> <div class='red'></div> </div> <div class='blue' style='width: 170px'> <div class='red'></div> <div class='red'></div> <div class='red'></div> <div class='red'></div> <div class='red'></div> <div class='red'></div> </div>
Comments
Post a Comment