Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two column div layout with fluid left and fixed right column

I want to make a two column layout using DIVs, where right column will have a fixed width of 200px and the left one would take all the space that is left.

It's quite easy, if you use tables:

<table width="100%">   <tr>     <td>Column 1</td>     <td width="200">Column 2 (always 200px)</td>   </tr> </table> 

But how about DIVs? Is it possible to accomplish this? If yes, then how?

like image 794
Silver Light Avatar asked Apr 13 '11 07:04

Silver Light


People also ask

How do I put two elements side by side in a div?

The most common way to place two divs side by side is by using inline-block css property. The inline-block property on the parent placed the two divs side by side and as this is inline-block the text-align feature worked here just like an inline element does.

How do I make a div with two columns?

More columns can be added by adding more divs with the same class. The following syntax is used to add columns in HTML. <div class="row"> tag is used to initialize the row where all the columns will be added. <div class="column" > tag is used to add the corresponding number of columns.

What is 2 column layout?

Two Column Layouts Generally, they consist of a header, footer and then two columns in the content area. One column is for the main content while the other is a sidebar. The essential CSS code for a centered 2 column layout with the CSS on the left is as follows.


1 Answers

The following examples are source ordered i.e. column 1 appears before column 2 in the HTML source. Whether a column appears on left or right is controlled by CSS:

Fixed Right

#wrapper {    margin-right: 200px;  }  #content {    float: left;    width: 100%;    background-color: #CCF;  }  #sidebar {    float: right;    width: 200px;    margin-right: -200px;    background-color: #FFA;  }  #cleared {    clear: both;  }
<div id="wrapper">    <div id="content">Column 1 (fluid)</div>    <div id="sidebar">Column 2 (fixed)</div>    <div id="cleared"></div>  </div>

Fixed Left

#wrapper {    margin-left: 200px;  }  #content {    float: right;    width: 100%;    background-color: #CCF;  }  #sidebar {    float: left;    width: 200px;    margin-left: -200px;    background-color: #FFA;  }  #cleared {    clear: both;  }
<div id="wrapper">    <div id="content">Column 1 (fluid)</div>    <div id="sidebar">Column 2 (fixed)</div>    <div id="cleared"></div>  </div>

Alternate solution is to use display: table-cell; which results in equal height columns.

like image 119
Salman A Avatar answered Oct 05 '22 17:10

Salman A