Recently I was working on a WordPress theme in which I needed a grid of posts in column boxes of equal height. This is easy to do when a site is fixed width; you would just set a fixed height. When the theme is responsive, the height of the grid column will increase and each column will be a different height depending on the content. If you don’t want the stair-step look that you see in the last image this post, then you can try using this CSS-only method to create posts of equal height.
Themes and Plugins Use Image Thumbnails and Limit Excerpt Length
If your theme has a responsive grid, your theme or posts plugin will have thumbnail images (featured images) with a uniform size, and you can limit the WordPress content or excerpt to a specific number of characters, so the text doesn’t run over the space you allow for it, as the width of the posts gets narrower. Usually the plugin that you use for this or your theme will let you limit the excerpt length.
But What To Do With Long Post Titles
Long post or entry titles on responsive themes can be problematic, going to multiple lines and creating a taller post column, so that you get a stair-step effect.
I recently ran across a CSS declaration, new to me, that can limit any text to one line and add an ellipsis at the end, so you know there is more to it or more coming.
The declaration is: text-overflow: ellipsis;
How to Make It Work for Most Browsers
The CSS for your post or entry may be something like this:
.home .entry {
border: 1px solid #aaa;
float: left;
margin: 0 1%;
overflow: hidden;
text-align: center;
width: 23%;
}
Note that .home is there because I was doing this for my home page, and .entry refers to the post; some themes may use .post.
This is just a normal grid container with a 1px gray border. If your theme doesn’t have the overflow: hidden;
declaration, you may want to add it, so that browsers that don’t add the ellipsis will at least cut off the text.
To truncate the post title to one line you would just add the following to the CSS for your post or entry title.
For my theme, it’s
.home .entry .entry-title {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
The entry-title refers to the post title; some themes may use an h2 or .post-title. The white-space declaration turns the title into one long line that essentially extends outside the post container. The overflow line hides the text that overflows the post container. The text-overflow line adds the ellipsis.
When you add this, you get the one-line titles shown in the image at the top of the post. This method will work in recent versions of Firefox, Chrome, Safari, and Internet Explorer 11. It will also just cut off the title in browsers that don’t use the ellipsis this way.
Stair-step Effect
Here is an example of the stair-step effect when the height of the posts increases as the titles go from one to two to three lines of text.
So that’s it, just a few line of CSS to help keep your post grid columns equal heights.
Leave a Reply