Jump to content


Photo

Jeeves dead simple CSS layout


  • Please log in to reply
5 replies to this topic

#1 Jeeves

Jeeves

    I write the interwebz

  • Members
  • 4,156 posts
  •  Friendly neighborhood standards Nazi

Posted 31 March 2009 - 02:45 AM

Now, people are always telling me "noo, CSS layouts are too hard!" Some even claim "I would use them but tables are easier!"
Such arguments hold as much weight as a half-eaten marshmallow tied to 47 helium balloons - you can see from the colorful balloons that somethings there, but its not what you'd call grounded. So to show just how easy CSS layouts can be, heres the easiest method I know for creating the most sought-after layout by the pro-table slackers - three equalizing columns.
I'll be assuming a fundemental understanding of CSS, so if you don't know how to implement color: red; to make something red, theres lots of good tutorials out there to get you started (HTMLDog, W3Schools, etc.).
To begin, lets draw up the HTML. This layout requires standards mode, so just make sure you use a valid doctype to trigger it.
CODE
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR...l1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-GB" xml:lang="en-GB">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="Content-Style-Type" content="text/css; charset=UTF-8" />
<title>Example Fluid Equalising Three-column Layout</title>
</head>

<body>
<div id="branding">
<h1>Example Fluid Equalising Three-column Layout</h1>
</div>

<div id="contentWrapper">
<div id="columnLeft" class="column">
<h3>Left 20%</h3>
</div>

<div id="columnCenter" class="column">
<h3>Center 60%</h3>
</div>

<div id="columnRight" class="column">
<h3>Right 20%</h3>
</div>
</div>

<div id="copyright">
<p>Copyright - 100%</p>
</div>

</body>
</html>
Now, as you can see I've needed to use a wrapper, which I hate, but is essential for the layout. The magic to the following technique is to make the columns far longer then they need to be, then pull the footer back up and hide the difference. And it sounds more complex than it is, so lets begin our CSS by setting the widths of the columns and floating them;
CODE
#branding { background: #ddd; }

#columnLeft { float: left; width: 20%; background: #fdd; }
#columnCenter { float: left; width: 60%; background: #dfd; }
#columnRight { float: right; width: 20%; background: #ddf; }

#copyright { clear: both; background: #ddd; }
At this stage, you already have a working three-column layout with header and footer. See? Not hard at all. However, you'll notice if you change the content, that the columns heights are independent. So now for the magic bit - making the columns equalize.
You should have already noticed the column class, and the aforementioned wrapper. So now we use a large padding and equal negative margin on the bottom of the columns, and hide the overflow with the wrapper. This makes them bigger than they need to be, without displaying the full height.
CODE
.column { padding-bottom: 9999em; margin-bottom: -9999em; }

#contentWrapper { overflow: hidden; background: #dfd; }
End result? try it yourself, three equal height fluid columns, surrounded by branding and copyright sections. But wait, theres more;
A limitation of float widths being set to percentages is you can't add padding inside the columns themselves without it breaking, so you might want to add a container content div inside each and reset default padding.
Also, if you need to support IE7 or 6, you'll need some additional styles to fix its broken box-model implimentation and 3px jog;
CODE
<!--[if lte IE 7]>
<style type="text/css">
body { word-wrap: break-word; }
#columnCenter { width: 59.8%; }
* html #contentWrapper { display: inline-block; }
</style>
<![endif]-->

So to finish, with the above tweaks and some dummy text;
CODE
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR...l1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-GB" xml:lang="en-GB">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="Content-Style-Type" content="text/css; charset=UTF-8" />
<title>Example Fluid Equalising Three-column Layout</title>

<style type="text/css">
* { margin: 0; padding: 0; }

#branding { background: #ddd; }

#columnLeft { float: left; width: 20%; background: #fdd; }
#columnCenter { float: left; width: 60%; background: #dfd; }
#columnRight { float: right; width: 20%; background: #ddf; }
.column { padding-bottom: 9999em; margin-bottom: -9999em; }

#contentWrapper { overflow: hidden; background: #dfd; }
#copyright { clear: both; background: #ddd; }

.content { padding: 1em; }
</style>

<!--[if lte IE 7]>
<style type="text/css">
body {word-wrap:break-word;}
#columnCenter {float:left; width:59.8%; background:#c0c0c0;}
* html #contentWrapper {display:inline-block;}
</style>
<![endif]-->

</head>

<body>
<div id="branding">
<h1>Example Fluid Equalising Three-column Layout</h1>
</div>

<div id="contentWrapper">
<div id="columnLeft" class="column">
<div class="content">
<h3>Left 20%</h3>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.</p>
<p> Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo.</p>
<p> Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus.</p>
<p> Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus.</p>
<p> Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum. Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus. Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante. Etiam sit amet orci eget eros faucibus tincidunt. Duis leo. Sed fringilla mauris sit amet nibh. Donec sodales sagittis magna. Sed consequat, leo eget bibendum sodales, augue velit cursus nunc, </p>
</div>
</div>

<div id="columnCenter" class="column">
<div class="content">
<h3>Center 60%</h3>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa.</p>
<p> Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.</p>
<p> Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim.</p>
<p> Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo.</p>
<p> Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, </p>
</div>
</div>

<div id="columnRight" class="column">
<div class="content">
<h3>Right 20%</h3>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa.</p>
<p> Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem.</p>
<p> Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer </p>
</div>
</div>
</div>

<div id="copyright">
<p>Copyright section - 100%</p>
</div>

</body>
</html>

Adaption;
  • You can make is fixed or elastic width, just change the percentage values
  • You can easily change a colum div to a ul for a side menu
  • Playing around is the best way to learn

Attached Files


World Domination Status: 2.7%


#2 Digz

Digz

    title available

  • Division Leaders
  • 1,070 posts
  • Location:London, England
  • Projects:Petrolution
  • Division:Petrolution
  • Job:Division Leader

Posted 31 March 2009 - 02:35 PM

Hey, that's pretty decent for a basic layout, I thought CSS was rather long when I was converting my PSD to CSS using some tutorial I found on the net, but that was obviously more complicated but nice start though!

#3 Hostile

Hostile

    Benefitting Humanity Simply by Showing Up!

  • Veterans
  • 9,551 posts
  • Location:Washington DC
  •  T3A Founder
  • Division:Revora
  • Job:Global Administrator
  • Donated
  • Association

Posted 31 March 2009 - 10:29 PM

http://scorpioninspect.com/

A site I made for my brothers business 6 years ago. Similar principal and it expands easily.

#4 Beowulf

Beowulf

    Shipgirl

  • Advisors
  • 7,219 posts
  •  Azur Lane Fangirl

Posted 01 April 2009 - 03:54 AM

Using tables isn't about being a slacker. It's about not fighting stupid browser shit. I only use them because of that... and because I'm partly a slacker.

But this is really nice. I may have to use this Jeeves. Thanks. :p

NZ.org | BBPCG
Discord: The Astronomer#1314
Steam


#5 Kravvitz

Kravvitz

    [X]HTML, CSS, & JS/DOM Adept

  • Members
  • 443 posts
  • Location:USA

Posted 01 April 2009 - 01:08 PM

That technique has some problems.

I prefer this one, when I can't use something as simple as what Hostile used.

Edited by Kravvitz, 01 April 2009 - 01:09 PM.


#6 Jeeves

Jeeves

    I write the interwebz

  • Members
  • 4,156 posts
  •  Friendly neighborhood standards Nazi

Posted 01 April 2009 - 09:33 PM

Yeah, its basic a demo of how simple CSS layouts can be and throw some CSS that can be experimented with into the ring, its far from perfect, but a demo of simplicity. And as most sites at Revora don't use anchors it is a viable option for modders who don't know where to start with a site.
For work we actually use this method as a house standard.

World Domination Status: 2.7%



Back to Web Design


0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users