Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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+)?

like image 297
ᴇʟᴇvᴀтᴇ Avatar asked Jun 09 '26 07:06

ᴇʟᴇvᴀтᴇ


1 Answers

I don't think this is possible without an extra wrapper div and a media query.

HTML:

<div class="box-wrap">
  <div class="box">a</div>
  <div class="box">b</div>
</div>
<div class="box">c</div>

CSS:

.box {
  border: 1px solid #000;
  float: left;
  height: 300px;
  width: 300px;
}

.box-wrap {
  float: left;
  width: 300px;
}

@media (min-width: 900px) {
  .box-wrap {
    width: auto;
  }
}

Demo: http://codepen.io/sdsanders/pen/zLFJj

like image 57
Steve Sanders Avatar answered Jun 11 '26 20:06

Steve Sanders