Facebook Twitter Instagram
    Trending
    • Build in 3D for iOS Using LibGDX and Kotlin | by Federico Jordán | Aug, 2022
    • Lake Gazebo / Bach Mühle Fuchs + After Apricots
    • Manchester United 1-2 Brighton: Three Things We Learned
    • Beyoncé’s Renaissance hits No. 1 spot on Billboard 200, Hot 100
    • Ellen Pompeo Will Star in Bizarre True Crime Adoption Series for Hulu; Will Have Reduced Role on GREY’S ANATOMY — GeekTyrant
    • Ben Godfrey out for three months with fractured fibula
    • Jayson Tatum: ‘I learned that I’m 24 and one of the best players in the world’
    • “GOAT issue influenced by decisions on vaccines”
    Facebook Twitter Instagram YouTube
    Swave Digest
    • Home
    • World News
    • Technology
      • Smartphones
      • Computers
      • Programming
      • Automobiles
    • Entertainment
      • Music
      • Anime
      • Movies
    • Sports
      • Football
        • Premier League
      • Basketball
      • Tennis
    • Business
      • Crypto
      • Stocks
      • NFT
    • Lifestyle
      • Fashion
      • Health
      • Travel
    • Shop
    Swave Digest
    Home»Technology»Programming»Recreating MDN’s Truncated Text Effect | CSS-Tricks
    Programming

    Recreating MDN’s Truncated Text Effect | CSS-Tricks

    Swave DigestBy Swave DigestJuly 28, 2022No Comments5 Mins Read
    Facebook Twitter Pinterest LinkedIn Tumblr Email
    Recreating MDN's Truncated Text Effect | CSS-Tricks 367170?resize=1024&w=1024
    Share
    Facebook Twitter LinkedIn Pinterest Email

    It’s no secret that MDN rolled out a new design back in March. It’s gorgeous! And there are some sweet CSS-y gems in it that are fun to look at. One of those gems is how card components handle truncated text.

    Recreating MDN's Truncated Text Effect | CSS-Tricks ShareASale 336x260 A

    Recreating MDN's Truncated Text Effect | CSS-Tricks Screen Shot 2022 07 27 at 2.44.36 PM

    Pretty cool, yeah? I wanna tear that apart in just a bit, but a couple of things really draw me into this approach:

    • It’s an example of intentionally cutting off content. We’ve referred to that as CSS data loss in other places. And while data loss is generally a bad thing, I like how it’s being used here since excerpts are meant to be a teaser for the full content.
    • This is different than truncating text with text-overflow: ellipsisa topic that came up rather recently when Eric Eggert shared his concerns with it. The main argument against it is that there is no way to recover the text that gets cut off in the truncation — assistive tech will announce it, but sighted users have no way to recover it. MDNs approach provides a bit more control in that department since the truncation is merely visual.

    So, how did MDN do it? Nothing too fancy here as far the HTML goes, just a container with a paragraph.

    <div class="card">
      <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Inventore consectetur temporibus quae aliquam nobis nam accusantium, minima quam iste magnam autem neque laborum nulla esse cupiditate modi impedit sapiente vero?</p>
    </div>

    We can drop in a few baseline styles to shore things up.

    Again, nothing too fancy. Our goal is cut the content off after, say, the third line. We can set a max-height on the paragraph and hide the overflow for that:

    .card p 
      max-height: calc(4rem * var(--base)); /* Set a cut-off point for the content */
      overflow: hidden; /* Cut off the content */
    

    Whoa whoa, what’s up with that calc() stuff? Notice that I set up a --base variable up front that can be used as a common multiplier. I’m using it to compute the font-size, line-height, padding for the card, and now the max-height of the paragraph. I find it easier to work with a constant values especially when the sizing I need is really based on scale like this. I noticed MDN uses a similar --base-line-height variable, probably for the same purpose.

    Getting that third line of text to fade out? It’s a classic linear-gradient() on the pargraph’s :after pseudo-element, which is pinned to the bottom-right corner of the card. So, we can set that up:

    .card p:after 
      content: ""; /* Needed to render the pseudo */
      background-image: linear-gradient(to right, transparent, var(--background) 80%);
      position: absolute;
      inset-inline-end: 0; /* Logical property equivalent to `right: 0` */
    

    Notice I’m calling a --background variable that’s set to the same background color value that’s used on the .card itself. That way, the text appears to fade into the background. And I found that I needed to tweak the second color stop in the gradient because the text isn’t completely hidden when the gradient blends all the way to 100%. I found 80% to be a sweet spot for my eyes.

    And, yes, :after needs a height and width. The height is where that --base variables comes back into play because we want that scaled to the paragraph’s line-height in order to cover the text with the height of :after.

    .card p:after 
      /* same as before */
      height: calc(1rem * var(--base) + 1px);
      width: 100%; /* relative to the .card container */
    

    Adding one extra pixel of height seemed to do the trick, but MDN was able to pull it off without it when I peeked at DevTools. Then again, I’m not using top (or inset-block-start) to offset the gradient in that direction either. 🤷‍♂️

    Now that p:after is absolutely positioned, we need to explicitly declare relative positioning on the paragraph to keep :after in its flow. Otherwise, :after would be completely yanked from the document flow and wind up outside of the card. This becomes the full CSS for the .card paragraph:

    .card p 
      max-height: calc(4rem * var(--base)); /* Set a cut-off point for the content */
      overflow: hidden; /* Cut off the content */
      position: relative; /* needed for :after */
    

    We’re done, right? Nope! The dang gradient just doesn’t seem to be in the right position.

    I’ll admit I brain-farted on this one and fired up DevTools on MDN to see what the heck I was missing. Oh yeah, :after needs to be displayed as a block element. It’s clear as day when adding a red border to it.🤦‍♂️

    .card p:after 
      content: "";
      background: linear-gradient(to right, transparent, var(--background) 80%);
      display: block;
      height: calc(1rem * var(--base) + 1px);
      inset-block-end: 0;
      position: absolute;
      width: 100%;
    

    All together now!

    And, yep, looks sounds like VoiceOver respects the full text. I haven’t tested any other screen readers though.

    Recreating MDN's Truncated Text Effect | CSS-Tricks Screen Shot 2022 07 27 at 5.07.34 PM

    I also noticed that MDN’s implementation removes pointer-events from p:after. Probably a good defensive tactic to prevent odd behaviors when selecting text. I added it in and selecting text does feel a little smoother, at least in Safari, Firefox, and Chrome.

    The Ultimate Managed Hosting Platform
    css-tricks effect mdns programming recreating text truncated
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Swave Digest
    • Website
    • Twitter
    • Pinterest

    WP Rocket - WordPress Caching Plugin

    Related Posts

    Build in 3D for iOS Using LibGDX and Kotlin | by Federico Jordán | Aug, 2022

    August 8, 2022

    Entry Level Software Developer Salary Expectations

    August 8, 2022

    The History of DOM Manipulation Performance in a Nutshell | by Davi Albuquerque Vieira | Aug, 2022

    August 8, 2022

    How to Prepare for a Coding Bootcamp (5 Easy Ways)

    August 8, 2022
    Add A Comment

    Leave A Reply Cancel Reply

    Twitter Instagram Pinterest
    • Home
    • Privacy Policy
    • Terms & Conditions
    • Contact Us
    © 2022 Swave Digest. All Rights Reserved.

    Type above and press Enter to search. Press Esc to cancel.

    We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies.
    In case of sale of your personal information, you may opt out by using the link Do not sell my personal information.
    Cookie settingsACCEPT
    Manage consent

    Privacy Overview

    This website uses cookies to improve your experience while you navigate through the website. Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may have an effect on your browsing experience.
    Necessary
    Always Enabled
    Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
    CookieDurationDescription
    cookielawinfo-checkbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
    cookielawinfo-checkbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
    cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
    cookielawinfo-checkbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
    cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
    viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
    Functional
    Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
    Performance
    Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
    Analytics
    Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
    Advertisement
    Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
    Others
    Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.
    Save & Accept