What is the method for incorporating this effect into the final sentence of a text?

I am trying to create a unique design effect by applying a custom border to only the last line of text. You can see an example here.

In this scenario, the target text is "2000 years old." (with that specific length) and not the entire width of the parent div which is set at 800px:

span
{
    color: #000000;
    display:inline-block;
    line-height:20px;
    position: relative;
    border-bottom: 10px solid red;
    padding-bottom: 0px;
    text-decoration: none;
    font-size:52px;
}   

To implement this effect, I have inserted a span, however, in a real text scenario there might not be any clear delimiter. How would you achieve this using CSS?

Answer №1

To achieve this effect, you can utilize the ::after pseudo-element in combination with the CSS property position:relative/absolute:

body {
  line-height: 40px;
}
div {
  width: 800px;
  font-size: 52px;
  color: #000000;
  position: relative
}

div::after {
  display: inline-block;
  line-height: 20px;
  position: absolute;
  bottom: -20px;
  border-bottom: 10px solid red;
  width: 315px;
  left: 0;
  content: "";
}
<div>
  Contrary to popular belief Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old.
</div>

Answer №2

Unfortunately, there is no direct CSS selector for styling the last line of text.

However, with a little bit of creativity, we can still achieve this effect using a clever workaround.

One way to accomplish this would be to add a bottom border to the text element and then overlay it with a pseudo-element to create the appearance of underlining the last line of text.

There are some restrictions to keep in mind:

  1. The element must have overflow:hidden set,
  2. You will need to manually define the background color of the pseudo-element.

This method will effectively underline the last line of text, regardless of its length, without the need to specify an exact width for the pseudo-element.

Check out the JSfiddle demo for a visual example.

body {
  font-size: 16px;
  line-height: 1.2;
}
div {
  width: 80%;
  margin: auto;
}
p {
  border-bottom: 1px solid red;
  position: relative;
}
p::after {
  content: ' ';
  position: absolute;
  width: 2000px; /* any really large number */
  height: 1px; /* same height as border */
  background: white; /* to cover the border */
  top: 100%;
}
<div>
  <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s.</p>
</div>

Answer №3

Here is another approach using the :before and :after selectors.

The :before selector acts as a border, while the :after selector covers the remaining space on the line. This method also allows for transitions to be applied when hovering over the element.

ul{width: 180px}

li{
  overflow:hidden;
  position: relative;
  padding-bottom: 20px;
  display: block;
}

a{
  display: block;
  border-bottom: 0px;
  color: black;
  text-decoration: none;
  position: relative;
}

a:after{
  content: ' ';
  position: absolute;
  width: 2000px; /* any really large number */
  height: 1px; /* same height as border */
  background: white; /* to cover the border */
  top: 100%;
}

a:before{
  content: ' ';
  position: absolute;
  height: 1px; /* same height as border */
  background: red; /* to cover the border */
  left: 0;
  top: 100%;
  width: 100%;
  max-width: 500px;
  transition: all 0.3s ease-in-out;
}

li:hover > a:before{
    max-width: 0;
}
<ul>

  <li>
     <a href="#">Short link</a>
  </li>

  <li>
     <a href="#">This link  is longer and it goes to a new line </a>
  </li>

  <li>
     <a href="#">This link is longer as well and it also goes to a new line</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

A DIV element may not have any visible height, even when it contains content

My <div> contains multiple <img> elements, each wrapped in its own inner <div>. Despite setting the outer div's height to auto, it fails to adjust dynamically based on the content. To display the inner divs inline, I've applied ...

What is the best way to shift a link to the right within a "div" element?

I've been working on setting up a navigation menu for my website, and I'm having trouble getting the "quit" link to align to the right while keeping the other links aligned to the left. I've tried various methods but just can't seem to ...

JQuery Mobile Navigation with Bootstrap 3 Sidebar on the Go

While using RoR, I encountered a baffling issue. I have created a new navbar with a sidebar that only displays on mobile and not on desktop. The Turbolink is functioning properly. <%= javascript_include_tag 'application', 'data-turboli ...

What is the best way to ensure that a specified number of list items (li) will align properly within the width of an unordered list (ul

I'm attempting to make all the li elements' width match that of my ul element. Here is the code I am using: http://jsfiddle.net/4XR5V/2/ I have looked at some similar questions on the website and tried adding: ul { width: 100%; display: tab ...

Center align a font-awesome icon vertically next to a paragraph of text

Is there a way to align a font-awesome icon on the left side of a text paragraph while keeping the text on the right side, even if it's longer and wraps underneath the icon? I've tried various code snippets but haven't found a solution yet. ...

Turn off drag and drop functionality and activate selection in HTML

Recently, I encountered a strange issue where selected text became draggable and droppable onto other text, causing it to concatenate. To resolve this problem, I added the following code: ondragstart="return false" onmousedown="return false" However, thi ...

Combining Images Together in JavaScript

In my current javascript project, I've encountered an issue. Specifically, I'm trying to overlay one image on top of another image using the following code: <div> <img src="1.jpg" style="z-index:1;"/> <img src="2.png" style="z ...

What is the best way to eliminate the text-decoration underline from a flex child when the parent is designated as the anchor?

I need help removing the text-decoration of a flex child when the parent is the anchor. Despite trying various CSS code, it doesn't seem to work as expected. On my WordPress site, the underline only shows on hover, but in the jsFiddle example I create ...

Equal dimensions for all cards in the TailwindCSS gallery display

I am currently working on an image gallery in React with Tailwind CSS. Here's a glimpse of how it looks: https://i.stack.imgur.com/ABdFo.png Each image component is structured like this: <div className="flex items-center"> < ...

Ways to conceal a dynamically generated div upon page load?

I am currently facing a scenario where I need to dynamically create a div. My initial approach was to create it on the document ready event, but the requirement is for it to be displayed only upon selection. The problem I am encountering is that the empty ...

Tips for properly formatting a fixed table header

I'm currently facing an issue with the sticky header style in my data table. I have created a simple Angular component along with a specific directive: sticky.directive.ts @Directive({ selector: '[sticky]' }) export class StickyDirecti ...

Tips for personalizing your Magento 2 theme

As someone new to Magento 2 and front-end development with a basic knowledge of HTML and CSS, I am eager to customize the blank theme in Magento 2 to gain a deeper understanding of how things work. However, despite reading through the documentation, I am u ...

When attempting to retrieve the innerHTML of a <div> element within a <Script> tag, the value returned is undefined

Utilizing a masterpage to create a consistent header across all content pages, I encountered an issue on one page. I needed to retrieve the innerHTML of a div element and pass it to an input control on the same page in order to access the updated innerHTML ...

Require assistance with arranging font-awesome icons in a stacked formation

After upgrading to the latest version of font-awesome, I encountered some stacking issues with my icons. In the previous version, everything was working perfectly. <li><span class="icon-stack stacked"><i class="icon-circle icon-stack-base" ...

Why isn't Sequence.js Slider automatically playing?

Issue: The sequence.js slider I implemented is not animating. Despite adding the following options: animateCanvas: true, fadeStepWhenSkipped: false, autoPlay: true, autoPlayInterval, 2000 It still does not work. Is there something essential t ...

Element failing to adhere to CSS grid layout

I am currently working with grid layout and aiming to position the following content at the bottom and center, but it is currently aligning to the left: .body { margin: 0; background-color: rgb(255, 237, 237); display: grid; grid-template-rows: ...

Struggling to animate the selector of a nested div in Jquery?

Despite trying numerous variations, I am struggling to identify the correct selector of a nested div. The goal is to animate this particular div, but since no animations are taking place, I suspect that I have not selected the right element. Here is the a ...

Ways to maneuver the vehicle by using the left or right key inputs

I am a beginner with canvas and game development. I am currently working on a project where a car moves in a straight line, but I want to add functionality for the car to turn anti-clockwise when the left key is pressed, and clockwise when the right key is ...

I am looking to remove the target attribute from an anchor tag if it does not have a value assigned

To ensure W3C validation, I must remove the target attribute from all anchors where the target value is null. Here is the code snippet inside the body: <div> <a href="#" target="">home empty</a> <a href="#" target="blank">home&l ...

The sorting icon in jQuery Data Table's search option is not functioning

I am having an issue with jQuery DataTables. When using jQuery DataTables, it provides a default search option. However, the problem arises when I search for a particular record and if the content does not match or if I find a single record, then I need to ...