Tips on how to maintain the underline when text is split into two sections

Revised

This question is distinct from the duplicate one. I am specifically addressing the issue of ensuring the underline continues until the end of the text, regardless of how many lines it spans.

The challenge I am facing is with displaying the underline below the anchor tag when the text is split into multiple lines. Despite setting the width of the ul tag to 300px and dividing the anchor text into two parts, the underline does not extend across both parts as desired.

Current Output

Desired Output

I need to display the underline beneath the phrase "of mentoring" as well.

ul {
  width: 300px
}

ul,
li {
  list-style: none;
}

a {
  text-decoration: none;
}

ul li a {
  position: relative;
  font-size: 28px;
  line-height: 32px;
  color: #000;
}

ul li a:after {
  content: "";
  background-color: red;
  width: 100%;
  height: 10px;
  display: block;
  position: absolute;
  bottom: 5px;
  z-index: -1;
}
<ul>
  <li>
    <a href="">The role of mentoring</a>
  </li>

  <li>
    <a href="">Research backed benefits of mentoring</a>
  </li>

</ul>

Answer №1

One way to accomplish this is by utilizing the properties text-decoration and text-underline-offset 👇:

ul {
  width: 300px;
  list-style: none;
}

ul li {
  list-style: none;
}

ul a {
  font-size: 28px;
  line-height: 32px;
  color: black;
  text-decoration-line: underline;
  text-decoration-thickness: 10px;
  text-decoration-color: red;
  text-underline-offset: -7px;
  text-decoration-skip-ink: none;
}
<ul>
  <li>
    <a href="">The importance of mentorship</a>
  </li>
  <li>
    <a href="">Benefits supported by research<br> in mentorship</a>
  </li>
</ul>

Answer №2

If you have the underline set to absolute, this code snippet should get the job done quite nicely. Keep in mind that while this solution works, it might not be the most efficient as it removes any HTML tags present inside the a element.

document.querySelectorAll(".underline").forEach((block) => {
  block.innerHTML = block
    .innerText
    .split(' ')
    .map((word) => `<span class="underline-wrap">${word}</span>`)
    .join(' ');
});
    ul {
      width: 300px
    }

    ul,
    li {
      list-style: none;
    }

    a {
      text-decoration: none;
    }

    ul li a {
      position: relative;
      font-size: 28px;
      line-height: 32px;
      color: #000;
    }
    ul li a.underline {
      display: inline-block;
      position: relative;
      overflow: hidden; /* hide excess of line produces by left: -10px; right: -10px;*/
    }
    ul li .underline-wrap { position: relative; }
    ul li .underline-wrap:after {
      content: "";
      background-color: red;
      height: 10px;
      display: block;
      position: absolute;
      bottom: 5px;
      left: -10px; /* fill gaps between words */
      right: -10px; /* fill gaps between words */
      z-index: -1;
    }
    <ul>
      <li>
        <a href="" class="underline">The power of positivity</a>
      </li>

      <li>
        <a href="" class="underline">How to stay motivated</a>
      </li>

    </ul>

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

Problem with CSS creating the Typewriter effect when the third line of text is added

I've been working on a website for my new company and I want to incorporate a typewriter effect on the main page. I came across a tutorial on Medium.com but I'm having trouble adding an additional span to the provided code. Take a look at the lin ...

Sliding the container with a width adjustment and left margin fails to display all images

In the provided HTML code below, there is a functionality to move images horizontally by clicking on buttons: $(document).ready(function() { calculate_width(); $('#moveleft').click(function() { var loga = $('#marki #loga'); ...

Utilizing Bootstrap datepicker to set maximum and minimum dates based on another date input

I have been attempting to create a restriction on the date selection, where datepicker1 cannot exceed datepicker2, and vice versa. Initially, I tried implementing this feature using (Linked Pickers). Unfortunately, this did not achieve the desired date in ...

Verification of the data entered in the input box

I am looking to develop an Input Box where users can enter the private details of a person. The first character must be either A or E, and the rest can be alphanumeric with no special characters allowed. I need to implement validation on the client side to ...

I'm having trouble displaying the Facebook page plugin on my HTML website

I encountered a minor issue. I attempted to add the "Facebook page plugin" on a test website. I copied the code from the official Facebook plugin page: <!-- 1. Include the JavaScript SDK on your page once, ideally right after the opening <body> t ...

adjust highcharts chart size to fit screen width

Has anyone experienced issues with setting an explicit width of 400px on a chart? I am facing difficulties as the chart gets cut off instead of scaling down to that size. My main concern is having it resized properly for an iPhone mobile site. Can anyone ...

The CSS styles can vary depending on the web browser being used

Require assistance with my CSS. I designed an HTML/CSS widget on Blogger using my own expertise, but now facing some issues. The trouble arises with the image on the front left in the code. While it displays correctly on Google Chrome, aligned with the te ...

Adjusting the dimensions of a Twitter widget to fit the screen size

My website is designed to resize based on screen size, but I encountered an issue when adding a Twitter widget. Despite setting the attribute width:'auto', the widget did not resize properly. Below is the code for the widget: <script charset= ...

Stop users from being able to copy text on their smartphones' internet browsers

I am currently working on creating a competitive typing speed challenge using JavaScript. Participants are required to type all the words they see from a div into a textarea. In order to prevent cheating, such as copying the words directly from the div, o ...

Next.js production mode prevents CSS from loading properly

Issue Upon building and launching a production build of our application, the CSS fails to load. Inspecting the devtools reveals a multitude of errors and warnings: Possible Causes The problems do not occur when running the app in development mode. Othe ...

Animate a box moving continuously from the right to the left using jQuery

I'm trying to create a continuous motion where a box moves right, then left, and repeats the cycle. However, I can only get it to complete one cycle. $(document).ready(function() { function moveBox() { $('#foo').css({ 'ri ...

The functionality of onClick or onChange seems to be malfunctioning in the preline user interface select component

I recently integrated the preline UI library into my React project to enhance the design of my website. However, I encountered an issue where using the "select" element with the data-hs-select attribute as suggested by preline UI seemed to cause problems w ...

What is the formula for determining the REAL innerWidth and innerHeight?

I am currently working on a responsive website using Bootstrap and I am looking to create an element that perfectly fits the screen size. When I set the height/width to 100%, the browser includes the toolbar, other tabs, and the Windows taskbar in the cal ...

Conceal the div element if the screen size drops below a certain threshold

Is it possible to hide a div element when the browser width is less than or equal to 1026px using CSS, like this: @media only screen and (min-width: 1140px) {}? If not, what are some alternative solutions? Additional information: When hiding the div eleme ...

Unable to post form data into database due to Javascript undefined data situation

I've been working on an HTML form that can interact with a PHP API to retrieve client data and then update user stats in a MySQL database. Currently, I am converting the data into a JSON object and using JSON.stringify to create a string for sending ...

Tips for incorporating HTML elements into XML emails

I'm facing an issue with my email template where the body of the email is being pulled as a String, resulting in raw html appearing in the email. Does anyone know how to insert an html tag into the body of an XML email using a String? <body> ...

Refresh a function following modifications to an array (such as exchanging values)

Looking to re-render a function after swapping array values, but the useEffect hook is not triggering it. I need assistance with this as I plan to integrate this code into my main project. Below are the JSX and CSS files attached. In App.js, I am creating ...

Managing all mouse interactions simultaneously in ReactJS

I've successfully created a button: <button className='collapse-button' onClick={() => {setTopNavigationBarCollapse(!topNavigationBarCollapse)}}>&#9776;</button> Now, I'm wondering if there's a way to call the s ...

How do I enable automatic playback for a vimeo video using the embed tag?

Currently, I am facing a challenge with the following embed tag in my HTML file: <embed src='{{"https://player.vimeo.com/video/{$videouri}?autoplay=true"}}'width="300" height="361" /> I am experiencing difficulties when trying to use the ...

Execute functions during jquery's .animate() operation

This is a snippet of code I use to smoothly scroll the browser back to the top: $('html, body').animate({scrollTop: 0}, 500, "easeOutQuart"); Now, I am looking for a way to prevent the user from scrolling while this animation is running. Once t ...