Like our free content? Then you'll love our exclusive Club!
Divi Academy Membership gives you exclusive access to over 110 resources, including child themes, layouts, cheatsheets, tutorials, live training and member perks. And new content is added EVERY Monday!
I have previously written about how to change the excerpt length of Divi blog posts using PHP and creating your own custom module. However, with the continuous updates, like many things involving editing the modules, this method no longer works.
It is possible to make it work with some changes and I was considering updating that recipe but to be honest, it’s just going to break again when Elegant Themes change something else, and I know editing PHP can sometimes be daunting for beginners.
So with that in mind, let’s bypass all the complex coding and use a purely CSS way to limit your excerpts in the Divi blog module which is not only future proof (or should be ), it works across all browsers as it uses just three CSS properties which are supported across the board!
It also results in a pretty damn neat and tidy layout.
So let’s get cooking!
Cooking time
Literally 2 minutes!
Preparation
For our prep we simply need to add a Blog module to our page. You can use the Grid or Fullwidth layout as this works with both formats.
Once you have added your Blog module and selected the settings you want displayed, click on the Design tab. You can use any settings you like here but there is one we need to define and make a note of and that is the line height for the body text. Set it to something that works with the font and font size you are using. I find 1.5em works well.
Then click on the Advanced tab and give the module a CSS class of ds-limit-excerpt then Save & Exit.
Method
Now, let’s add our CSS. This is all you need:
.ds-limit-excerpt .post-content p { display: block; max-height: 3em; overflow: hidden; }
We are doing three things here:
1 – We are setting the excerpt to display as a block element, you don’t even need this and can leave it out. However, if you are going to be showing the ‘read more’ link and want to style it like a button, you are probably going to need some extra space above it and by setting the excerpt as a block element, we can add a bottom margin to give the read more link the extra space it needs.
2 – We set the maximum height of the excerpt as a multiple of our line height. So, if we set the line height in the module to 1.5em, and we want to display 2 lines of our excerpt, then we multiply the line height by the number of lines : 1.5em * 2 = 3em. Want four lines? No problem, 1.5em * 4 = 6em. And so on.
3 – We set the overflow to hidden, this is what hides the part of the excerpt we don’t want displayed.
Copy the CSS and paste it into your child theme stylesheet or Divi theme options custom CSS box and that’s it!.
The great thing about this is you can even duplicate that CSS, change the class and apply it to another blog module and have a completely different excerpt length. Cool eh?
And that’s it, pretty simple eh?
If you found this helpful please leave a comment and subscribe to my newsletter for all my latest Divi related content.
Michelle X
Hi there Michelle,
Thanks so much for the great article and for replying to all comments.
I want to know if it is possible to have the “blocks” of the different posts be exactly the same height regardless of the screen size. Currently when I drag my screen smaller, the box heights changes depending on the number of words in either the title, the meta or the excerpt. See the difference between two boxes on a tablet for e.g.:?dl=0
Please advise
You can use flex on the blog pagination container to have all posts equal height. https://css-tricks.com/snippets/css/a-guide-to-flexbox/
Thank you Michelle. This trick worked for me. Now, I have removed excerpt from my blog.
Really brilliant, thanks Michelle
Thanks so much for this – works like a charm!
Thanks for this post. I also want to ask for help on how to add padding around excerpt. I have added border lines around my excerpt; the problem is the excerpt is touching the borderlines. How do I add padding around the excerpts, preventing it from touching the borderlines.
Just add padding to the CSS that is already there:
.ds-limit-excerpt .post-content p {
display: block;
max-height: 3em;
overflow: hidden;
padding: 20px;
}
You may need to adjust the max-height value
Thanks so much for the help. But just so you know, as an extreme newbie it took me 45 minutes to figure out where to put the CSS.
For those that don’t know, click on DIVI, then Theme Customizer, go all the way to the bottom to Additonal CSS and there it is.
Thanks again for the help. I’m definitely bookmarking!
Always read the instruction manual
https://www.elegantthemes.com/documentation/divi/theme-options/
Thank you for this!! I’ve been looking for an “instruction manual”!
Thank you for this post – is there a way to prevent any content being seen at all? So I just have the title showing – I tried using 0.0em but when previewed the content is overlapped in one line rather than no content at all.
Thanks
Sure, you can use this in your custom CSS:
.post-content p, p.post-meta {
display: none;
}
you are the great man
The post display is limited save for search where the full body of the excerpt displays. Can the search excerpt be managed via settings or CSS?
Easiest (and probably best) way is to just include that in the post.
Hi Michelle, I appreciate all your grate tips. I have just to add here a side-comment about this comments section. There seem to be a color issue here where your answers are written in white on white background making it not so readable until we select the text.
I hope you’ll be able to fix this for our best experience.
One more THX.
Thanks for letting us know! It has been fixed.
Thanks Michelle That worked!
This worked perfectly for me! My only concern is that the excerpt seems to end abruptly in the middle of the sentences since I have a “Read More” button below the excerpt rather than as part of the text in the excerpt. Is there a way to use CSS to add an ellipsis after the last word?
You can try using a pseudo element to do this. However, it would be a better habit to enter excerpts for posts and adjust the value so complete sentences are shown.
Hi Michelle,
How do I go about hiding unwanted content in the first 2-3 lines of the excerpt? I currently have text in the header for an email opt-in that shows up in the blog excerpts. Thanks
Sounds like you are referring to something different – open graph data. You need to use an SEO plugin or code that into the header of each post manually.
Hi Shay,
Thanks for following up. I’ve resolved the issue since posting. What worked for me is to manually enter excerpt content using the field just below the default post editor in WordPress. Doing this allows me to select the excerpt content I want posted vs. the content automatically populating to an archived list.
Thanks Shay – you may be hearing from me soon
Большое спасибо, Michelle! Очень помогают ваши статьи. Привет из России : )
Thanks Vladimir, greetings from the UK
Hi Michelle,
thanks for this.
Question: How can I force Divi to only show excerpts in the search results? Currently, my search results display the full article…
Any help is much appreciated.
Thanks!
Best regards,
Chris
consultingkaiser.com
Go to your reading settings within WordPress and change ‘For each article in a feed, show’ to summary.
Thanks for your help, Michelle. For some reason, this doesn’t seem to work… the search results still show full length articles…
Would you have another hack I could try?
Have you added text in the excerpt field in the posts? If you do this it should show that text rather than the full article
Thanks Michelle. Yes, I tried adding text in the excerpt field… unfortunately without success…
Do you have a link Chris and I will tae a look
Sure. On http://www.consultingkaiser.com basically any search term, e.g. http://consultingkaiser.com/?s=make+money.
Thanks for your help, Michelle. Much appreciated!
This is not normal behaviour Chris, the search results and archive page show an excerpt by default, so the way yours are showing with the full text leads me to the assumption you have a setting wrong somewhere or a plugin conflict.
Try disabling all plugins and clearing all caches
Thanks Michelle. Fully agree that this is not normal behaviour…
Deactivating all plugins doesn’t help, and regards wrong setting I’m out of ideas where to look.
Strange fact: In mobile the search works just fine…
Sorry for all the bothering, Michelle… was hoping maybe you have some cool tricks up your sleeve.
Michelle, I love the simplicity and effectiveness of your solutions and tutorials. Like any person that knows their craft really well, that truly reflects how well you understand css and web! Thanks for this super easy solution!
Thanks for the kind words Jonathan and you are very welcome
Hello, brilliant post. However, the excerpts do not have (…) at the end. Any solution? Thanks!!
Not perfectly as you cannot determine the length of the last work on the bottom line, however, this fade out with dots gets pretty close: .ds-limit-excerpt .post-content p:before { content: ‘…’; position: absolute; right: 0; bottom: 0; text-align: center; width: 60px; background: -webkit-gradient(linear, left top, right top, from(rgba(255, 255, 255, 0)), color-stop(1%, rgba(255, 255, 255, 0.02)), color-stop(30%, rgba(255, 255, 255, 1)), to(rgba(255, 255, 255, 1))); background: linear-gradient(to right, rgba(255, 255, 255, 0) 0%, rgba(255, 255, 255, 0.02) 1%, rgba(255, 255, 255, 1) 30%, rgba(255, 255, 255, 1) 100%); } }
thanks michelle, this is just what I was looking for … couldn’t find truncate_post( 270 ); in any of my divi files!
Yes, structure has changed somewhat since that other recipe was posted. Hopefully this will be much simpler for people to manage
Thanks Michelle, this is brilliant. I’m working on a magazine style site and the ability to customise the excerpt length for individual blog modules is something I was looking to figure out. Cheers
That’s great Jason, exactly what I am doing too
Nice! Any way to do this with text blocks on pages, not blog posts?
Yes same principle but you would change the class from the post content to whatever module you are using
Great, thanks!
Thanks. So helpful especially since I’m not familiar with writing code yet.
Most welcome Dina
Slick! Thanks for your tips.
Welcome Sander