Tutorials

Latest Word » Tutorials » Liquid + Fixed Two Column Lists with CSS
Liquid + Fixed Two Column Lists with CSS

Liquid + Fixed Two Column Lists with CSS

Tags:

Ever tried creating a two column liquid llist with one of the columns being a fixed width? Usually creating table-like layouts in CSS can be a pain, but this quick tutorial will fix this common issue.
Liquid Fixed Layout Example

Common Mistakes and How to Correct Them

Typically most people will run into two annoying CSS issues.

  1. Bug Example #1
    The second column spills underneath the first column. View example
  2. Bug Example #2
    Both columns do not line up properly. View example

The Correct Method
XHTML & CSS

XHTML
I usually start off creating the rows using a list, then break the columns into its own divs.
The first div will be the fixed column, which in my example will contain the image and the image description. The second div will be the part that is liquid, allowing the content to be flexible according to the browser size.

<ul class="column">
	<li>
		<div class="imgblock">
			<img src="http://l.yimg.com/a/i/us/autos/autos_mb_concept_139x119.jpg" alt="" />
			<strong>Image Name</strong>
		</div>
		<div class="detail">
			<h2>Title</h2>
			<p>Caecus damnum commoveo abbas nunc sed multo neque eum. Ut qui mauris refero te nobis ullamcorper dolus, duis, adsum, odio. Loquor at loquor quis occuro valde facilisis dignissim plaga, molior interdico. Eros tego, verto ibidem tego, huic usitas adsum. Ullamcorper in vulpes, obruo dolore enim os quod. Velit abluo tation tum scisco, nullus odio ut quadrum luctus tum. </p>
		</div>
	</li>
</ul>

CSS
Ill start by styling the main list that will be spitting out each row.

Main list CSS

ul.column{
	font-size: 1.2em;
	margin: 10px 0;
	padding: 0;
	list-style: none;
	float: left;
	width: 100%;
	border-top: 1px solid #ddd;
}
ul.column li {
	float: left;
	background: #f0f0f0;
	width: 100%;
	padding: 10px 0;
	margin: 0;
	border-bottom: 1px solid #ddd;
}

Fixed Column CSS

ul.column li .imgblock {
	font-weight: bold;
	float: left;
	width: 150px;
	padding: 0 10px;
	text-align: center;
}
ul.column li .imgblock img {
	padding: 5px;
	margin-bottom: 5px;
	background: #fff;
	border: 1px solid #ccc;
}

Liquid Column CSS (Notice I do not add a float left, nor do I specify a width size to this column).

ul.column li .detail{
	padding-left: 170px;
}

Additional Touch-ups

Now let’s dress up the elements inside of these columns

Heading and Paragraphs CSS

ul.column li h2, ul.column li p {margin: 5px 0; padding: 5px 0;}

Bullet List
Incase you would like a bullet list in your detail section.

ul.column li ul.specs li {
	float: none;
	margin: 0 5px 0 20px;
	padding: 0;
	list-style: disc;
	border: 0;
	background: none;
	width: auto;
}

Finally, let’s add a class to every other list item for better legibility.

ul.column li.even { background: #fff; }

If you have a different solution or have any comments or questions, don’t be afraid to let me know!

Related Posts

Author Bio

My name is Soh Tanaka and I am a Los Angeles based designer/front-end developer specializing in CSS driven web design with an emphasis on usability and search engine optimization. I also run a CSS Gallery which is updated daily with the best CSS websites from around the world. Come check it out!

You can learn more about me or Twitter  Follow me on twitter for more updates and resources!

Did You Enjoy This Post?

subscribe  Subscribe via RSS or by email to get all upcoming tutorials and articles delivered straight to you.

+ Add Comment29 Peeps Have Spoken Their Minds...

  1. Edward

    Thanx for the tutorial, helped me out!

  2. Mitternacht

    I struggled with exactly the same problem some time ago, trying to style comments for a Dotclear theme, and I’m glad to see I came up with what you think is the “correct method”. To me Problem #2 was the most annoying because it showed up in Firefox when it was solved in IE6, and vice-versa.

  3. Dave Q

    Nice fix for the columns. I agree with Mitternacht that #2 issue is one stupid bug!! Thanks for the article.

  4. gamehome

    Haha`You are really great,thanks a lot!

  5. bj

    As a newbie I can make sense of that, however if I wanted to place the image in the css code how would I do that? And then center it all?

  6. Soh

    bj, placing an image in the css code, do you mean like a background image in the css? and when you say center, do you mean horizontally center aligning inside of the columns? Just want to make sure I’m not misunderstanding your question~ :-) Let me know~

  7. bj

    Soh, yes place the image as background in the css. And for center…. make 2 even colomns that center on the page. Thanks

  8. Soh

    Hey bj,

    You can do the following to get your background Images on the columns:

    ul.column li .imgblock {
    	font-weight: bold;
    	float: left;
    	width: 150px;
    	padding: 0 10px;
    	text-align: center;
    	background: url(image.jpg) no-repeat left top;
    }
    ul.column li .detail{
    	padding-left: 170px;
    	background: url(image.jpg) no-repeat 170px top;
    }
    

    You may have to adjust the padding or background positioning depending on what you are trying to achieve with your image~

    and to center align these columns as a whole, you can wrap it in a div and specify a width, then add a margin: 0 auto; and that should do the trick :-)

  9. mark P

    Finally someone with a good tutorial on how to make this all work!!

    Thanks

  10. binita

    i have two column and i want to add them .for example

    2 + 4 = 6
    4 +8 =12
    onhere,2 is firscolum and 4,8 is second column,result is third column .i awant to add all column like this by one formula how ?

  11. marc

    hmm

    when i use a different size dimension image, it overlaps the paragraph. When i use a smaller image, the image name goes to the right of the image. When i have a long paragraph, it wraps under the image…

    changing the width in the imageblock, the long paragraph still goes under the image.

    am i doing something wrong? i just cut and paste the final demo and alter images and descriptions..

  12. Soh

    @binita, Im not sure if I understand your question, can you please rephrase it or give me an example?

    @marc, if you used the example on the demo page, and used a different size images, are you sure you adjusted the width in the CSS? If you have a sample, I can take a look~

  13. marc

    soh,

    i think the prob is that the dynamic images are not a fix size. So any image larger than what is specify in the CSS will overlap the descriptions. Any image smaller then the CSS will cause the “name of image” to float to the right.. I just simply added a BR after the image, however being dynamic I can’t do much about larger image. So it all works fine if all images are fixed sizes – which im forcing it now to have the same width.

  14. Soh

    How about you wrap your image with a div that has an overflow hidden or has a fixed/max-width of the dynamic image, this is not the prettiest solution but it will keep the images from flowing overboard into the content~

    And about the “image name” being shoved to the right, you can just specify a display:block on the image, and it should do the trick.

  15. Stefan Olsson

    Thanks! This helped me a lot :)

  16. Steven

    Works fine but not in IE. Try to replace bulleted list with additional portion of text and shrink browser window. You will see text indent and columns won’t be equal.

  17. Steven

    Works fine but not in IE. Try to replace bulleted list with additional portion of text and shrink browser window. You will see text indent and columns won’t be equal. Please fix this.

  18. Soh

    It actually works perfectly in IE6, 7, and 8 :-) , you may have missed something in the tutorial?

    I went ahead and updated my demo page for you, taking out the bullets and added 2 extra paragraphs.

  19. Steven

    Thanks for quick response. I’ve checked your solution again in IE6 SP2 ( used in my company ) and it doesn’t work properly. I do not know why, maybe it depends on browser version or something…

  20. CW Web Solutions

    Great tutorial. Love your site’s design.

  21. Louis

    Great tutorial. Please double check Steven’s complaint though. Viewing your sample in IE6/SP2, any text that extends below the image will jog over to the left slightly — about 2-3 characters.
    You might need to make browser window a little smaller to make text or bullets on right extend vertically.
    I’ve tried adjusting paddings & margins for all, but can’t seem to get rid of this. :(
    Any ideas?

  22. Soh

    Ah, you know what I apologize to both, I was misunderstanding what the issue was. Thanks Louis for making that clear for me~

    I just tried a quick fix that worked. (seems to be an IE6 Bug) If you wrap your content (content inside of “detail”) with another div with a float: left; it got rid of the problem for me.

    Let me know if that works out for you~

    Thanks again fellas :-)

  23. Louis

    beautiful! worked like a charm.
    I’m glad I checked back for your answer. I was messing around with display:inline-block which also fixed but needed different paddings for ie6 (& hence a different css).

  24. Louis

    Help — I ran into two issues related to printing the page:
    1. if any content in tags precedes the tag then the printed version starts a new page regardless of how much white space is unused.
    Example, add, “View the table” just before the . Then open print preview & you’ll see.

    2. The last row of every page is cut off. The next page begins with the next row. copy/paste the last two rows of your sample and append to end of list a few times & you’ll see this clearly.

  25. Jason

    I don’t know why I waste time checking the rest of the web for answers cause I always end up finding them here. Thanks again Soh, just what I needed help with.

  26. henni

    thanks for that, very useful :)

  27. Denis

    Hi, thanks for the tutorial, can this be used for a “sandwich” layout:
    fixed + liquid + fixed ?
    At the moment, I can’t make it working, anybody any ideas?

  28. Albert

    oh wow! padding-left! it’s so simple yet brilliant! this is one of those things that make you go: “now why didn’t I think of that?” lol :D

    thanks for the tip! :)

Speak Your Mind...

Post HTMLNeed to Post HTML Code? Use Postable to post code friendly html. *Wrap all html code in <pre></pre> tags.
Post HTMLNeed to Keep Updated with Comments? Subscribe to comments now.

CSS Gallery