views:

144

answers:

6

Hey guys, I am trying to create a grid like system with div's with content floated left. The problem is that the height of each div is not static since the content is different in each one. Since the height is different.. things get outta whack if one is taller then another in one row.

Is there some sort of css way or js (prefer javascript) way to get around this. I really want to avoid setting large heights on the divs to get them to line up.

You can see the example here: http://www.foreclosurejournalinc.com/index.php?option=com_hotproperty&view=type&id=1

A: 

Floating <div> elements are tricky to work with, especially if you're aiming at multiple browsers.

If it's important that they line up in a grid format, I'd use a <table> instead; <div>s are meant for dynamic content that may or may not line up with everything else, and it seems like a misuse to expect them to line up perfectly, especially if their sizes are unspecified.

Also, since the float CSS property modifies how block-level elements interact with other floating and non-floating elements, I'd float the <table> element and give it a set width.

amphetamachine
i am trying to avoid using tables for anything.. but table like stuff. semantically incorrect to use a table here. though it seems like it may be the most logical solution unfortunately.
Roeland
I see the table haters are modding down, you can spend all day coming up with some complex css solution, or just use a table. People have this irrational hate for tables because someone decided that using them is improper, even if they are the best tool for a job. Use the best tool for the job, a table.
Rob
sometimes i must admit i agree with you rob. i guess in the end.. the "end user" doesnt give a rats ass if tables are used or not. is there some sort of SEO pitfalls for using tables?
Roeland
there are serious accessibility pitfalls and if you don't care, then you are not worth the lost customers.
dusoft
A: 

You might want to look into some of the grid-oriented CSS layout "frameworks" out there. The general idea is to have a bunch of classes that are applied to blocks of content so that you're not dropping little numbers all over the place. There are various different approaches; a google search for "CSS grid framework" should get you started.

Here's another idea: do you know how many tables are in a row? Well, can you not wrap each row of <div> boxes in another "row" <div>, with that one set to overflow: hidden so that it stretches out around the floated boxes inside it? The outer <div> elements would not need to float at all (though they could, if need be). That would have the effect of making each row as big as the biggest cell in it.

Pointy
Blueprint! However that one doesn't include height ;)
Ropstah
not a bad idea.
Roeland
i just created a div.hp_prop_row around every 2 inner divs. i guess its extra markup.. but i like this best :)
Roeland
A: 

Check CSS display: table; for layout: http://www.onenaught.com/posts/201/use-css-displaytable-for-layout

dusoft
display:table isn't widely supported enough for most projects. Something a lot of people don't realise is that many implementations that do exist require you to have HTML that mimics all of the structure of a table. For instance, you can't just set `display:table-cell;` on an list item, without setting `display:table-row;` on the list itself, and wrapping the whole thing in a container with `display:table;` - the browser needs 'cells' to be within 'rows' to be within 'tables'.
Beejamin
A: 

I think if you add a cleared element after every two entries that would help.

<div style="clear:both"></div>

That is, you have hp_prop twice then insert the cleared div.

The

 <br class="clearboth">

is not having any effect in the position that you have them.

Vincent Ramdhanie
i could do that with php logic, though i was trying to do this "right" so adding more markup is generally not considered "right". thanks though, i like this solution better then converting to tables.
Roeland
A: 

There are two options:

  1. Mark the end of each "row" with a <div>or <hr> which "clears" both sides by using the css "clear" attribute. You can learn more about hit here: http://www.w3schools.com/Css/pr_class_clear.asp

  2. Do what Pointy said and use a CSS framework. I would specifically recommend 960 Grid (http://960.gs) and/or BluePrint CSS (http://code.google.com/p/blueprintcss/). Both are simple, small, and easy to use. They basically use the above technique to achieve the result as well as offering you a bunch of other nice features (easy to create symmetrical and nicely proportioned layouts).

Mystic
+1  A: 

First off - it is not correct to use a table in this situation: The table structure defines a relationship between data in the same row, and data in the same column. Since the second column is the same type of data as the first, there's no table relationship here.

You shouldn't use a table because you're defining your layout in markup, so you can only ever have two columns. For example, if the client decides they want three columns, or you want to show a single-column list for a mobile view or print stylesheet, you need to change the markup (or even have several sets of markup for different purposes).

Flexible markup

OK - let's look at your markup. At the moment, you have almost got a table structure, because you're wrapping each row in a DIV. Again, this means you can't ever have a single column or 3 or 4 columns side-by-side.

A more flexible solution is to markup your data as a list. Because it's sorted, an ordered list is the right choice:

<ol id="list_properties">

  <li class="property">
    <h3 class="propertyTitle"><a href="#">08-CA-001731</a></h3>
    <dl class="details">
      <dt>Plantiff</dt>
      <dd>Bob's Mortgages</dd>
      <dt>Defendant</dt>
      <dd>Harry Skinflint</dd>
    </dl>
  </li>

  <li class="property">
    <h3 class="propertyTitle"><a href="#">08-CA-001731</a></h3>
    <dl class="details">
      <dt>Plantiff</dt>
      <dd>Bob's Mortgages</dd>
      <dt>Defendant</dt>
      <dd>Harry Skinflint</dd>
    </dl>
  </li>

</ol>

You'll have one <li class="property"> per item in your 'grid'. I've suggested some nicer markup for the details, too - help yourself to that if you like.

Now to finish it off: Sadly, this can't be done in a cross browser fashion without a bit of final tweaking with Javascript. That said, the JS is unobtrusive, so it really only acts as a bit of polish at the end.

Base styling

Your base styles will look like:

#list_properties {
  list-style:none;
  margin:0;
  padding:0;
  width:960px;
  float:left;
}

#list_properties li.property{
  width:430px;
  float:left;
  margin:0 50px 50px 0;
  min-height:16em; /* For FF, Safari, Opera, IE8 */
  _height:16em; /* For IE6 */
  *height:16em; /* For IE7 */
}

That recreates the columns - at this stage, if all of the boxes had a height less than 16em, this would be all you need.

Javascript polish

To make sure everything is rock solid, I'd use the equal heights plugin for jQuery: http://www.cssnewbie.com/equalheights-jquery-plugin/ Basically, you pass it your container (#list_properties) and it scans each of the children (li.property), and sets the height of each one to the height of the tallest item.

So, for instance, one item has extra information and needs 18ems of height, all of the other items are set to 18em too.

The linked site has documentation for getting it going, but once you've got jquery and the plugin ready, you need only do:

$(function(){
  $('#list_properties').equalHeights();
});

Once you get it set up this way, you can modify the number of items in the column just by changing the width of the li.property.

A last thought...

I'm just thinking, what is the advantage of displaying this list in columns, anyway? After all, the user can sort the list - wouldn't a single-column list be easier to scan? I'm assuming the user will be looking for a particular item, rather than just browsing through semi-randomly.

Beejamin
This is a great answer! I do want to say that the markup for this site is completely horrible. (I hate Joomla for this) I am merely helping out with a project that was already almost finished, so I am just tweaking things left and right and in the process learning how to do it the right way :) I chose to put them in columns since there was so much empty space on the right. This fills up the page much better and I dont think it detracts too much from "being able to scan the list". You think its best to leave it in one column?
Roeland
In a situation like this, I think it's less ambiguous to have a single column - otherwise, it's not quite clear how the order is (newspaper style top > bottom, then left > right, or web style left > right, then top > bottom).I know what you mean about the use of space. Personally, I would leave it in one column and perhaps use 'columns' within each item something like so:http://www.scrnshots.com/users/beejamin/screenshots/240706
Beejamin
I love how this answer says what I said in about 40 times the characters. Nice work for 1 vote.... I'll vote you up as well.
hunter