MegaHit
MegaHit

Reputation: 2644

Fixed width columns with fluid gutters

I know this can be done with columns, but I have to support IE.

I'm trying to get to a layout whose columns are all fixed width, with the gutters being fluid.

I couldn't get this to work with floats, so I settled on using justified inline-block items:

HTML

<div class="wrapper">
    <div></div>
    <div></div>
    <div></div>
    <!-- more divs... -->
</div>

CSS

.wrapper {
    text-align: justify;
}

.wrapper div {
    width: 100px;
    display: inline-block;
}

This works wonderfully, but the last row of divs are all aligned to the left: http://jsfiddle.net/EsHh3/

The only solution I found is to add additional unnecessary divs: http://jsfiddle.net/EsHh3/1/

I feel uncomfortable about this, so I'd like to know if there are any other options.


Please don't tell me not to re-invent the wheel. I have not found any fluid grid system that supports fluid gutters.

Upvotes: 0

Views: 1607

Answers (4)

frozenkoi
frozenkoi

Reputation: 3248

For what you want to do, I'm afraid a CSS only solution is not available at the moment, much less if you want it to work in IE8.

Since you want to have (a) items that are in the HTML source as a list (b) a variable number of columns depending on available space (c) column spacing depending on width of container I think the solution you'll need would have to employ at least a bit of javascript.

Consider on of the frameworks proposed in the other answers. One I've worked with and could do what you want is Masonry (or the for-pay bigger brother Isotope). (There's also a non-jQuery version of Masonry). You'll have to come up with a function that when the page is resized, recalculates the desired gutter and reconfigures the framework. Something along the lines of calculating x = how many items would fit per line based on the container width and item width and then dividing the remaining space by x-1.

If you want to stick with the idea of adding extra DIV's to the markup, an alternative would be to listen to resize events, and add DIVs as needed based on the width and how many items would fit per line.

ORIGINAL ANSWER, which failed to fit all the criteria.

Since you're relying on text-align: justified the reason the last line doesn't expand to the full width is because there's no line break at the end of it. So to accomplish that we add an extra element with an wrapper:after {} rule, that is also an inline block with a width of 100% so it guaranties a line break.

See fiddle

The CSS ends up something like:

.wrapper {
    text-align: justify;
    width: 380px;
    margin: 0 auto;
}

.wrapper div {
    width: 100px;
    display: inline-block;
}

.wrapper:after {content: ''; width: 100%; display: inline-block; background: pink; height: 2px; overflow: hidden}

Note that the pink background is there so that you can see where the element is. You might need to play with the border/margin/padding of that extra element or the wrapper so that content that comes after wrapper doesn't gain extra margin. In chrome unfortunately there's a slight missalignment of the last row items, possibly because of the extra space between the last DIV and the fake element.

Upvotes: 1

jdott
jdott

Reputation: 1

This is how I would go about it: http://codepen.io/jeremychurch/pen/wmtJz

.container {
  display: table;
  width: 100%; }
.cell {
  display: table-cell; }
.content {
  width: 15em;
  margin: 0 auto; }

<div class="container">
  <div class="cell">
    <div class="content">
    </div>
  </div>

  <div class="cell">
    <div class="content">
    </div>
  </div>

  <div class="cell">
    <div class="content">
    </div>
  </div>
</div>

Upvotes: 0

AlexC
AlexC

Reputation: 9661

you can try this:

 .wrapper {
    text-align: justify;
    width: 380px;
    margin: 0 auto;
    moz-column-count: 3;
    -moz-column-gap: 20px;
    -webkit-column-count: 3;
    -webkit-column-gap: 20px;
    column-count: 3;
    column-gap: 20px;
}

Updated URL

Upvotes: 0

KKS
KKS

Reputation: 3630

Hey I don't know why you want a fluid gutter, but I have a simple grid sample which you might want to have a look and if you want to see the css then click the SCSS on the codepen site. Also, if you are learning then this sample is very good start point for how to make your own grid. Also, to avoid yourself reinventing the wheel you might want to try different grid frameworks out there. Just google css grid frameworks.

Upvotes: 0

Related Questions