Site icon Brian Prom Blog

CSS equal height rows with dynamic column heights

I was working on a responsive website design layout and we were running into issues that for the category pages, the products weren’t aligning just right.  The page is 3 columns wide using li elements for each item.

The problem we were having is that some of the items have color swatches, and others don’t, some of the titles are longer than others and wrap to a second line, etc.  I guess the easy way to say it is that there is dynamic content.  So the height varies page to page.

The Solution

HTML

CSS

Live Example – Mockup

I put together a quick example of what this will look like.

https://promincproductions.com/blog/code/responsive-category-equal-height/responsive-category-equal-height.html

Live Example – Active Website

This is the site that I did this setup on.  The site is fully responsive and will go down to two columns for tablets and one column for phones, based on various width based media query break points.

http://www.luggagepros.com/supercat_Luggage/cat_Hardside–Luggage/index.shtml

 

Match the row height of your column elements in a category or list page are equal; even with borders!

Adding a Border

Many people want to add a border to add separation to the products on their category pages.  The method mentioned above won’t work exactly right for this though, as the borders don’t carry through from top to bottom.

This solution will ensure the border fits top to bottom, however there is one slight catch to this – you’ll need to set a height on each of the items in your column to the maximum height that it will ever be displayed.  While this is fine for the single row where the really tall element is, say 5 lines tall, the next row that has elements that are only 2 lines tall will still have the height of the 5 lines.  So this application is not perfect for all situations.  But depending on your content this might do the trick for you.

The HTML is the same as above, so please reference that.  The CSS is slightly different however:

Live Example – Mockup

I haven’t done full browser testing on this yet, but I’m fairly certain this should carry through to all browsers.

https://promincproductions.com/blog/code/responsive-category-equal-height/responsive-category-equal-height-with-border.html

Live Example – Active Website

I used essentially this same technique on this website to mock up the table using a <div> structure.

http://www.luggagepros.com/travel/baggage-fees.shtml

Still not solving your problem? 

The last option that I won’t go into here as it’s out of scope to the topic of this article is to use jQuery or Javascript to set the height.  The logic would be that it needs to read all of the items in a row, find the tallest item, then adjust the height for all of the items in that row, and move on to the next row.  In all honesty, this is the most reliable method in my mind as you wait for all of the content to load and then essentially go and hand set the heights for each item.  However, despite liking jQuery, I try to use it only when necessary.  If I can solve a problem with CSS (without jeopardizing SEO) then I’ll do that before I go into jQuery.  It just makes more sense as CSS is loaded in real time where jQuery has to wait till after the page loads, there can be compatibility issues with some devices/users/browsers, and sites are getting too dependent on Javascript/jQuery and it’s just not needed at times.

While I’m not going to provide the full method of this in this post, I did implement this solution on this site, so if you feel like digging through the source code you should be able to find it easy enough.

http://advice.annsbridalbargains.com/

 

I hope these solutions help you out in your programming.  I’m always looking for better ways to solve this issue.  Let me know if you have any other options in the comments below. 

 

 

Exit mobile version