user2275661
user2275661

Reputation: 11

Two column, equal height layout - space between columns

I found some code that creates the perfect two column layout with header and footer. The two columns stretch down and to the right perfectly, regardless of content, which is what I was looking for.

The problem: I cannot find a way of creating space between the two columns. I need the space because I'm using borders and it looks cramped as is. The columns are not floated and margins don't do the trick.

Can anybody think of a way of separating the two without breaking the functionality?

Here is the jsfiddle link:http://jsfiddle.net/7M9rg/3/

Many thanks!

Here is the code:

<div id="wrapper">
<div id="header">
</div>

<div id="main">

<div id="side">
<div id="side-stuff">
<ul>
<li><a href="../English/index.html">Home</a></li>
</ul>
</div>
</div>

<div id="content">
<p>Contrary to popular belief, Lorem Ipsum is not simply random text. It has</p>
</div>
</div>

<div id="footer">&copy; 2013 </div>
</div>

CSS:

/*css reset*/
html,body {position:relative;margin:0;padding:0;min-height:100%;width:100%;
height:100%;}
div,p,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,form,fieldset,input,
textarea,p,blockquote,th,td, figure {margin:0;padding:0;}
ol,ul {list-style:none;}
li {list-style-type: none;}
* { -moz-box-sizing: border-box; -webkit-box-sizing: border-box; box-sizing:
border-box; }


html, body {
font-family: Helvetica;
height: 100%; /*important for equal height columns*/
min-width: 650px;
}

#wrapper{
height: 100%; /*important for equal height columns*/
padding-bottom:130px; /*This must equal the height of your header*/}

#header{
height: 130px; /*This must equal padding bottom of wrap*/
display:block;
padding: 5px;
color: #fff;
border: thin solid #ebebeb;
border-radius: 10px;
margin: 10px;
background-image: url(Images/gradient.png);
background-repeat: repeat-x;    
width: 99%;}

#main {
position: relative;
height: 100%; /*important for equal height columns*/
width: 99%;
overflow:auto;
display: table; /* This is needed fo children elements using display table cell*/
table-layout: fixed;
padding-bottom: 50px; /*This needs to match footer height*/
overflow: auto;
margin-left: 10px;}

#side{
background-color: #fff;
width: 150px;
margin: 10px;
vertical-align: top;
padding-top: 20px;
padding-right: 10px;
display: table-cell;
border-radius: 10px;
border: thin solid #CCC;}

#side-stuff{
display: block;
padding-left: 10px;}

#content{
background-color: #fff;
padding: 10px;
display: table-cell; /*To make sibling columns equal in height*/
margin-bottom:10px;
border-radius: 10px;
border: thin solid #CCC;}

#content-stuff{
width: auto;
height: auto;}

#footer{
position: relative;
height: 40px;
margin-top: -40px; /* margin-top is negative value of height */
margin-left: 10px;
clear: both; /* Use if floating elements */
color: #999;
width: 99%;
border: thin solid #ebebeb;
border-radius: 10px;
background-image: url(Images/footer_gradient.png);
background-repeat: repeat-x;
background-position: bottom;}

Upvotes: 1

Views: 1981

Answers (2)

cimmanon
cimmanon

Reputation: 68319

Because your element is now effectively a table, you can use all properties that apply to tables. The border-spacing property is what you're looking for, and it applies to table elements.

http://jsfiddle.net/7M9rg/6/

#main {
    border-spacing: 10px;
}

You'll need to do a bit of tinkering on the margins of surrounding elements to get your #main element back into position where it should be.

Upvotes: 0

Marc Audet
Marc Audet

Reputation: 46785

Because you are using display: table-cell, margins do not work.

Here is one work around. Create a separator as follows, and insert between #side and #content:

<hr class="spacer"> 

Style the new element as:

hr.spacer {
    display: table-cell;
    border: 1px;
    width: 10px;
}

Set the width to a suitable value.

Fiddle: http://jsfiddle.net/audetwebdesign/navc5/

This introduces an extra element but it is easy to implement and reliable.

Note that table-cell is not supported in IE7 and earlier. For some people, this is an issue.

Upvotes: 1

Related Questions