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

Concealing an element by using parentElement.getElementsByClassName to set the display property to none

I am attempting to hide a button that generates a new element upon being clicked (to then be replaced by a delete button), but I'm struggling to set the display property to "none" when trying to target the same element using parentElement and then ret ...

The HTML <select> element is currently styled with a locked CSS, but I am looking to have it displayed with the default

Unfortunately, I am unable to make changes to the CSS file at this time. The current styles in the CSS file are as follows: font-size: 13px; padding: 6px 4px; background-color: rgb(238, 238, 238); border: 1px solid rgb(204, 204, 204); clear: both; margin- ...

What is causing my mobile version not to resize and adjust to fit the screen properly?

Currently utilizing Bootstrap 4 along with the viewport meta tag, %meta{:content => "width=device-width, initial-scale=1, shrink-to-fit=no", :name => "viewport"}/ Even after hiding all images with .hidden-xs-up, the display on Chrome mobile appear ...

The html button adorned with a variety of CSS styles

I've been attempting to create a button for a message system that displays an orange dot when there's a new message, but I'm having trouble getting it to work. Do you think it's possible? Check out the button below: <input type="bu ...

What is the best way to combine a custom CSS class and a bootstrap class to style an element in next.js?

In the following example, the CSS class is imported and then applied to the element: import customStyles from "./Home.module.css"; <div className={(customStyles.collection, "card")}> Although they work individually, when combined as shown above, t ...

What is the reason behind the constraints of min/max width and height values preventing the element from fully filling its parent div?

HTML Code: <div class="content"> <div class="card"> </div> </div> CSS Code: .content { min-height: 350px; min-width: 320px; max-width: 350px; padding: 15px; } .card { width: 100%; height: 100%; ...

What is the technical process behind conducting A/B testing at Optimizely?

I'm currently experimenting with Google Analytics and Content Experiments for A/B testing on my website, but I'm encountering some challenges in making it seamless. To utilize the Google API properly, a few steps need to be taken. Firstly, I nee ...

What is the best way to incorporate a new attribute into a particular CSS class?

Just to provide some context, I am relatively new to all of this. Please bear with me. I have a unique CSS class that is specifically assigned to thumbnail images on my Wordpress website. Currently, I am using a script that adds a "Pin it" hover link to a ...

What could be the possible reason for the controls in my element getting disabled due to adding a JavaScript background

First and foremost, I want to share a link with you to a page that I am currently developing so you can better understand the situation: Additionally, here is a link to the background effect: https://github.com/jnicol/particleground If you visit the page ...

The table does not move up or down when using the mousewheel over the rows, but only when

I'm encountering an issue with a table inside a div that has overflow-y: scroll applied to it. While the scrollbar appears when there is content overflow, I am unable to scroll using the scrollwheel. It only works when the cursor is directly on top o ...

Tips for choosing multiple values from a dropdown menu in Bootstrap CSS version 3

I'm looking to implement a way to select multiple values from a dropdown menu without using the CTRL key. Currently, I am utilizing Bootstrap CSS for styling. Here is the code for my dropdown: <select multiple class="dropdown-menu"> ...

Tips for isolating html and js code?

After following a tutorial to write the program, I attempted to separate the HTML code from JS but encountered issues. Some lines of JS code are connected to the database as parameters, and when I split the HTML and JS codes, it no longer functions. Does ...

Split total based on checkboxes toggled with JavaScript

I'm facing a challenge. I have a total sum of donations collected, for example, 10€. Additionally, there are various NGOs that one can select to donate to. Individuals have the option to toggle on/off which NGO's they wish to contribute toward ...

The clearfix feature is ineffective when using AngularJS

<ul class="dropdown-menu wm_search_div" ng-show="searchDivShow"> <li ng-repeat="user in searchUserList"> <a href="javascript:void(0);" class="wm_clearfix3"> <img ng-src="{{user.faceIcon}}" class="pull-left wm_se ...

CORS blocked the JavaScript Image's request

I am encountering an issue with my code that involves capturing selected divs using the HTML2Canvas library. However, when I try to download the captured image file, it is not working as expected. The error message I keep receiving is "Access to Image at ...

Grouping and styling table data cells within a row: best practices

I am attempting to "group" a collection of cells/<td> elements within a row/<tr> in a <table> in order to apply styles to them. The code below accomplishes what I desire to some extent, but it does have its limitations: /* Issues wit ...

Troubleshooting: Vue.js Component template not displaying items with v-for loop

I recently implemented a method that calls an AJAX request to my API and the response that it returns is being assigned to an array. In the template section, I am using the v-for directive to display the data. Surprisingly, it only renders once after I mak ...

How can I prevent katex from overflowing?

Currently, I am facing a challenge in handling katex equations that overflow in a react native webview. I am attempting to dynamically break the equations into multiple lines to prevent scrolling and display them separately. Any assistance on this matter ...

Align the inner container in the outer container-fluid

I'm struggling to center a container inside a container-fluid. Any suggestions on how to make it work? /* Setup Buttons Specific Styling */ .setup-bg { background: url("https://image.ibb.co/geAGqy/setupbtns_bg.png"); background-repeat: no-repea ...

Tips for aligning words on a single line

Encountering an issue with long words being split up in the middle of a line, as shown in this photo: https://i.stack.imgur.com/IxQi6.png This is the code snippet causing the problem: ineligiblePointsTableRows() { return this.state[PointsTableType. ...