Tips for shortening block text on your website with a link at the end

I'm currently working on implementing a truncating block of text with ellipses that can expand and collapse using a button in a React project. Individually, I've been able to truncate the text correctly with ellipses, and my coworker has positioned the link correctly relative to the block. However, I'm facing an issue where the 'View More' button is overlapping with the ellipses. I've tried different positioning and HTML adjustments, but haven't found a solution yet.

https://i.sstatic.net/DcyfW.png

https://i.sstatic.net/RAXBH.png

Here is the CSS code I'm currently using:

.all-text {
    @include typ-body-1-16-regular;
    margin-bottom: calc(1.5 * #{$default-padding});

    @include xs-mobile {
      @include typ-body-2-14-regular;
      text-align: left;
    }

    & .overflow {
      @include max-width-for-ingreients;
      display: block;

      @include xs-mobile {
        text-overflow: clip;
        overflow: hidden;
        display: -webkit-box;
        -webkit-line-clamp: 4;
        -webkit-box-orient: vertical;
      }
    }

    & .text-expander {
      display: none;
      text-decoration: underline;

      @include xs-mobile {
        display: initial;
        cursor: pointer;
        background-color: $white;
        position: relative;
        float: right;
        top: -18px;

        &.expanded {
          top: 0px;
          float: revert;
        }
      }
    }
  }

In the JSX code snippet below, I am trying to get the text to truncate with ellipses while keeping the trailing link inline:

<div className={styles['all-text']}>
  <span
    className={`${isExpanded ? '' : styles['overflow']}`}
  >
    <b>{t('subtitle')}&nbsp;</b>
    {ingredients}
    {isExpanded && expander}
  </span>
  {!isExpanded && expander}
</div>

If you have any suggestions on how I can achieve this truncation without affecting the button alignment, I would greatly appreciate your input!

Answer №1

For the best result, I recommend utilizing a dual approach: embedding the link within the truncated text and also incorporating the link as a separate entity that will only appear if the text is shortened.

To determine real-time truncation of text, you can refer to my detailed response here.

When the text comfortably fits within the container, the link will be visible (typically at the end). However, when the text exceeds the container's boundaries, it is important to conditionally display the additional link component.

In cases where the text is truncated, effectively conceal the link using display:none, calculate the width of the link, and implement a shape-outside property to truncate the text just before the position of the link. Subsequently, place the link component directly after the ellipsis in the text.

Ensure that the shape-outside element accommodates enough space for both the ellipsis and the link.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Utilizing AngularJS: Executing directives manually

As a newcomer to AngularJS, I am facing a challenge that requires creating a 3-step workflow: The initial step involves calling a web service that provides a list of strings like ["apple", "banana", "orange"]. Upon receiving this response, I must encap ...

Utilize API information to populate a dynamic selection dropdown menu

Objective: Utilize an API to fetch data and then implement it in a dynamic select dropdown menu. The value should be set as the id and the label should be displayed as the name. Issue: How can you retrieve and incorporate the data into the select dr ...

I'm interested in creating a unique sidebar layout with Bootstrap 5 - how can

I'm struggling with creating a sidebar for my application and need guidance on using col to organize content into left, right, and center. Here is my navbar: <nav class="navbar navbar-expand-lg navbar-dark bg-dark"> <div class=&quo ...

Angular formly - Enhancing User Input Focus

I have created a field wrapper that converts normal text into an input when hovered over with the mouse. Additionally, I have a directive that sets focus on the field when it is activated. Now, I am looking for a solution where the field remains open as l ...

Executing a JavaScript function within a PHP echo statement

I am completely new to working with AJAX and PHP echo variables or arrays. However, I have managed to successfully display images on my website by passing variables from AJAX to PHP and back. The only problem I am encountering is when I try to call a Javas ...

AngularJS: Blocking access to specific state for users

I am currently in the process of developing an application using sails.js for the backend and Angular for the frontend. My goal is to restrict access to the admin control page for unauthorized users. I have come across several solutions, but none of them s ...

Retrieve the product ID and permalink utilizing Commerce.js within a Next JS environment

Looking for guidance on Next JS dynamic routes? Check out the documentation at https://nextjs.org/docs/routing/dynamic-routes Currently, I have a page that renders all products using getServersideProps to make API calls. Here is the structure of the produ ...

Exploring Text Color Verification with CSS in Selenium IDE

I am working on a project and want to ensure that my link is styled properly: <a class="title">My link</a> The CSS code used to style my link is as follows: a.title { color: #CC3333; } How can I confirm that the text "My link" is displayi ...

fluctuating random percentage in JavaScript/jQuery

I am currently faced with the challenge of selecting a random number based on a given percentage ranging from 0 to 5. 0 - 25% (25/100) 1 - 25% (25/100) 2 - 20% (20/100) 3 - 15% (15/100) 4 - 10% (10/100) 5 - 5% (5/100) However, there are instances where ...

Broken link in navigation bar

I encountered an issue with the navigation bar on this website. When I click on "Leistungsspektrum" in the navigation bar, it returns a message stating that The requested URL /leistunsspektrum.html was not found on this server. However, the actual file nam ...

The route node attempted to send the response twice, resulting in an ERR_HTTP_HEADERS_SENT error indicating that the headers have already been set

On my API, users can delete a specified collection by providing the collection id. The router reads a JSON file, iterates through the collections, and deletes the record if found: router.delete('/todos/:id', (req,res)=>{ const id = req.pa ...

The adjustment of the Three.js Orbit Controls target based on camera movement is not functioning effectively

Currently, I am working on a demo for a "First Person Controller" where the camera moves forward in response to a TOUCH event being pressed. To achieve this movement, I am using setInterval and addScaledVector for the camera's motion. In addition to ...

Do we really need to use the eval function in this situation?

Just wondering, is it reasonable to exclude the eval() function from this code? Specifically how <script> ... ... function addGeoJson (geoJsonPath, iconPath = "leaflet-2/images/marker-icon.png", iconSize = [30,50], popUpContent, ...

Is there a reason why angularJS doesn't provide the exact error location directly, opting instead to just offer a link to their website that provides a generic explanation?

Why does AngularJS not provide the specific error location directly, such as which file the error is in, instead of just giving a link to their website with a generic explanation? This makes debugging very challenging! Whenever there is an error, it becom ...

Why is it that I cannot use the .get method on JSON data? And what is the best way to convert JSON into a map?

I'm currently in the process of creating a test for a function that accepts a map as an argument, Under normal circumstances when the code is executed outside of a test, the parameter would appear like this (when calling toString): Map { "id": "jobs ...

Customizing styles based on screen size in Emotion Styled Components

The Emotion documentation provides guidance on creating reusable media queries that can be used within the css prop. With this feature, we can apply different styles based on various query breakpoints directly in the css prop: <div css={{ color: ...

Determine the aspect ratio with a constant adjustment using CSS/SCSS

I am trying to adjust the aspect ratio of a responsive div to create a square shape with an offset. I want the height to be equal to the width plus the specified offset. Here is an example of what I am attempting: .div { width: 100%; aspect-ratio: 1 / ...

Creating an image gallery with animated effects: A step-by-step guide

I am looking to develop a creative image gallery with animated effects. I also want to include panels and hyperlinks for each image, similar to the functionality on this website: http://www.microsoft.com/en-lk/default.aspx Can anyone guide me on how to ac ...

Element in the Middle

I have just started learning HTML and CSS, and I'm curious to know how I can center the links Home, About, and Contact in the header. HTML: <body> <header> <h1><a href="#">HBT</a> </h1& ...

Load pages seamlessly with AJAX

I'm facing a challenge: Is there a way to dynamically refresh a <div> element on a webpage using AJAX, without displaying loading bars and without causing the existing content to disappear while new content is being loaded? I would like the pr ...