Search code examples
cssdynamic-columns

CSS to dynamically create columns


I have three <div> elements:

<div class="foo">A</div>
<div class="foo">B</div>
<div class="foo">C</div>

Desired behavior

I'd like to write some CSS to create the following effect as the screen size changes:

enter image description here

Undesired behavior

I know how to implement the following (undesired) behavior:

enter image description here

div.foo {
   display: inline-block;
   vertical-align: top;
   width: 300px;
}

Is there a simple way (pref. without any Javascript library) to achieve my desired behavior that will work on major browsers (Chrome, Safari, Firefox, IE9+)?


Solution

  • Wrap the A and B into it's own container.

    <div class="foo-wrap">
        <div class="foo">A</div>
        <div class="foo">B</div>
    </div>
    <div class="foo">C</div>
    

    Then use a media query to control the display of the A/B column on smaller screens:

    .foo-wrap {
        display: inline-block;
    }
    
    div.foo {
       display: inline-block;
       vertical-align: top;
       width: 300px;
    }
    
    @media all and (max-width: 925px) {
        .foo-wrap {
            width: 300px;
        }
    }
    

    Demo: http://jsfiddle.net/axertion/j48da85n/1/