4

So this might seem like an obvious question to some, but what is the best practice for aligning versatile span content in a responsive grid? I know you could simply set a pixel height, but wouldn't that kind of defeat the purpose of keeping things responsive?

Take the below screenshot for instance:

varying span content

<div class="container">
    <div class="row">
       <div class="span3 well"><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec facilisis venenatis sollicitudin. Nam eros risus, lobortis a ultricies sed, interdum in mi. Donec elementum ullamcorper odio, vel gravida velit pretium quis. Donec sagittis, sem nec rhoncus tristique, dui ante volutpat nisl, sit amet feugiat velit lorem sagittis turpis. Quisque laoreet arcu et sapien volutpat nec porta augue iaculis. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean accumsan feugiat libero, vel fringilla neque euismod vitae. Nullam justo mi, faucibus sagittis pharetra non, egestas sit amet nulla.</p></div>
       <div class="span3 well"><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec facilisis venenatis sollicitudin. Nam eros risus, lobortis a ultricies sed, interdum in mi. Donec elementum ullamcorper odio, vel gravida velit pretium quis. Donec sagittis, sem nec rhoncus tristique, dui ante volutpat nisl, sit amet feugiat velit lorem sagittis turpis. Quisque laoreet arcu et sapien volutpat nec porta augue iaculis. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean accumsan feugiat libero, vel fringilla neque euismod vitae. Nullam justo mi, faucibus sagittis pharetra.</p> </div>
       <div class="span3 well"><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec facilisis venenatis sollicitudin. Nam eros risus, lobortis a ultricies sed, interdum in mi. Donec elementum ullamcorper odio, vel gravida velit pretium quis. Donec sagittis, sem nec rhoncus tristique, dui ante volutpat nisl, sit amet feugiat velit lorem sagittis turpis. Quisque laoreet arcu et sapien volutpat nec porta augue iaculis. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean accumsan feugiat libero.</p> </div>
    </div>    
</div>

You can find the relevant JSfiddle here.

Note: Responsiveness seems to be broken in this JSFiddle for some reason, works fine in my own Twitter Bootstrap application however.

Noz
  • 6,216
  • 3
  • 47
  • 82
  • Did you mean "vertical" instead of "versatile"? – Mike Robinson Nov 20 '12 at 19:54
  • Nope, I meant versatile in the sense that the content of one span isn't quite proportionate to the content in the other. I could have phrased that a little better. – Noz Nov 20 '12 at 19:56
  • @CyleHunter so all the heights of the spans are equal? – Oliver Atkinson Nov 20 '12 at 20:43
  • Why do you want them to be the same size? Because you don't like that the well's don't align? – Shaun Mason Nov 20 '12 at 20:56
  • 1
    That's correct, I'm trying to build a landing page where spans of text / images aren't always quite the same height, I wanted to at least make the borders match up for a clean, balanced look. I also wanted to uphold the responsiveness of the application and make sure text doesn't overflow it's parent div on smaller screen sizes. – Noz Nov 20 '12 at 21:19

2 Answers2

5

If you take a look on DigitalLabs which is a website I worked on then if you look at the profiles I came across a similar error - i wanted them all to be the same height.

See my JSFiddle here: http://jsfiddle.net/LDtRr/2/

(scroll down to the bottom and press resize to see them all dynamically resize)

I used some javascript to fix the heights - I will show you the code that I used.

function resize(resize, resizeinner) {
var max = 0;

//reset height back to 0
$(resize).each(function(index, element) {
    $(element).css({ 'height' : '0' });
});

//find height of the profiles
$(resizeinner).each(function(index, element) {
    var height = $(element).height();
    console.log('    height=' + height);
    if(height > max) {
        max = height;
    }

});

//set the height dynamically based on the maximum so they are all uniform
$(resize).each(function(index, element) {
    $(element).css({ 'height' : max });
    console.log('    resizedTo=' + $(element).height());
});

console.log('max - ' + max);
}

Then for the html i used

<div class="span4">
    <div class="well profile">
        <div class="profile-resize">
            <!-- content -->
        </div>
     </div>
 </div>

What the code does is it gets the maximum height for the divs with the profile class, then sets all of the divs with that class to the maximum height - you can also bind this to the window resize so it automatically resizes the heights with the window.

$(window).load(function() { 
//initially size the elements
resize('.profile', '.profile-resize');
});

Maybe not the most elegant solution but I couldnt think of a better one at the time.

Oliver Atkinson
  • 7,970
  • 32
  • 43
2

Although your markup doesn't keep the hierarchy recommended by the bootstrap doc (.container > .row > .span > .well), have you thought about absolute positioning ? No JS involved.

Demo (jsfiddle)

<div class="container" style="position: relative;">
    <div class="row faux-row">
        <div class="span3 well"></div>
        <div class="span3 well"></div>
        <div class="span3 well"></div>
    </div>
    <div class="row vrai-row">
        <div class="span3"><p>...</p></div>
        <div class="span3"><p>...</p></div>
        <div class="span3"><p>...</p></div>
    </div>
</div>
.vrai-row { position: relative;z-index: 101; }

.faux-row { position: absolute;top: 0;left: 0;right: 0;bottom: 0;z-index: 100; }
.faux-row .well {
    height: 100%;
    /* The following is because .span* elements should not have paddings, margins nor borders see http://stackoverflow.com/a/11299934/1478467 */
    box-sizing: border-box;
}

If you want to set padding, margin, borders (styling that actually take space), it should be applied to the real one and the faux one - not the columns themselves, but their children for example.

The downside is that (as it is in the demo) you have to stick to the non-responsive grid (fluid or static). But it should work with a few more rules encapsulated in media queries.


Update

Responsiveness is actually not so hard to get, if you keep the .well class on all spans :

Demo responsive (jsfiddle)

@media (max-width: 767px) {
    .faux-row { display: none!important; }
}
@media (min-width: 768px) {
    .vrai-row .well { /* Deactivate well styles */
      background-color: transparent;
      border-color: transparent;
      -webkit-box-shadow: none;
         -moz-box-shadow: none;
              box-shadow: none;
    }
}
Sherbrow
  • 17,279
  • 3
  • 64
  • 77