easwee easwee - 3 years ago 165
HTML Question

3 column layout HTML/CSS

I have the following HTML layout:

<div class="container">
<div class="column-center">Column center</div>
<div class="column-left">Column left</div>
<div class="column-right">Column right</div>
</div>


Any chance to arrange the columns like on the below sample grid without changing HTML using CSS only?

----------------------------------------------------
| | | |
| Column left | Column center | Column right |
| | | |
----------------------------------------------------

Answer Source

Something like this should do it:

.column-left{ float: left; width: 33%; }
.column-right{ float: right; width: 33%; }
.column-center{ display: inline-block; width: 33%; }

DEMO

EDIT

To do this with a larger number of columns you could build a very simple grid system. For example, something like this should work for a five column layout:

.column {
    float: left;
    position: relative;
    width: 20%;
  
    /*for demo purposes only */
    background: #f2f2f2;
    border: 1px solid #e6e6e6;
    box-sizing: border-box;
}

.column-offset-1 {
    left: 20%;
}
.column-offset-2 {
    left: 40%;
}
.column-offset-3 {
    left: 60%;
}
.column-offset-4 {
    left: 80%;
}

.column-inset-1 {
    left: -20%;
}
.column-inset-2 {
    left: -40%;
}
.column-inset-3 {
    left: -60%;
}
.column-inset-4 {
    left: -80%;
}
<div class="container">
   <div class="column column-one column-offset-2">Column one</div>
   <div class="column column-two column-inset-1">Column two</div>
   <div class="column column-three column-offset-1">Column three</div>
   <div class="column column-four column-inset-2">Column four</div>
   <div class="column column-five">Column five</div>
</div>

Or, if you are lucky enough to be able to support only modern browsers, you can use flexible boxes:

.container {
    display: flex;
}

.column {
    flex: 1;
    
    /*for demo purposes only */
    background: #f2f2f2;
    border: 1px solid #e6e6e6;
    box-sizing: border-box;
}

.column-one {
    order: 3;
}
.column-two {
    order: 1;
}
.column-three {
    order: 4;
}
.column-four {
    order: 2;
}
.column-five {
    order: 5;
}
<div class="container">
   <div class="column column-one">Column one</div>
   <div class="column column-two">Column two</div>
   <div class="column column-three">Column three</div>
   <div class="column column-four">Column four</div>
   <div class="column column-five">Column five</div>
</div>

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download