To fill the remaining white space, add an underline after the last line of text

I'm struggling to implement a SCSS/CSS styling concept where I need to add a solid line before or after the last line of a heading. The challenge is that the width of the last line varies based on the screen size. I am open to any suggestions.

Here's what I aim to achieve when the text is aligned either left or right:

|Here is some text on screen|     |Here is some text on screen|
|very cool -----------------|  or |----------------- very cool|
|                           |     |                           |
|                           |     |                           |

EDIT: Added code for clarity:

HTML

<h1>You're the painter, we just want to see you paint.</h1>

CSS (current progress)

h1{
  font-family: "doesntMatter";
  font-style: bold;
  font-size: 2rem;
  text-align: left;
}

h1::after{
  display: inline-block;
  line-height: 0;
  position: relative;
  bottom: 2.5rem;
  border-bottom: 10px solid red;
  width: 100%;
  content: "";
}

Answer №1

After experimenting with some code, I finally cracked the solution to my issue. By tweaking a few styling elements, I achieved the desired effect of having the last line struck through.

.container {
  width: 100%;
  padding-inline: 2rem;
}

.text {
  font-style: bold;
  font-size: 2.5rem;
  line-height: 2.5rem;
  position: relative;
  overflow-x:hidden;
}

.text::after {
  position: absolute;
  left:0;
  bottom: 0.9rem;
  width: 100%;
  border-bottom: 0.4rem solid #000;
  content: "";
}
<section class="container>
      <h1 class="text>You're the painter, we are just the paint, brushes and canvas</h1>
    </section>

Removing left:0; from the text::after styling made the line jump over to fill the space at the end. Adding margin-left: 1rem provided some spacing between elements but I'm still puzzled by the behavior.

The mystery behind how it works remains, but applying overflow-x: hidden to the .text{} element ensures that the effect stops at the width of the header.

This interesting technique involves manipulating CSS properties to create the visual effect. By adjusting the styling of the container and text elements, the line spills off the page as intended.

I discovered that the line adapts to changes in the width of the final line of text. However, there are some peculiar scenarios to consider, such as when the text nearly fills the entire header width, resulting in a small protrusion at the end.

In conclusion, I have resolved the issue and hope this detailed explanation benefits those who struggled to find a solution through online searches.

Answer №2

Expanding on your existing code, this snippet encases the text in a span element. By doing so, it creates a white padding that covers the portion of the red line that falls beneath the actual text.

h1 {
  font-family: "doesntMatter";
  font-style: bold;
  font-size: 2rem;
  text-align: left;
  position: relative;
}

h1>span::after {
  display: inline-block;
  position: absolute;
  border-bottom: 10px solid red;
  width: 100%;
  left: 0;
  margin-top: -11px;
  content: "X";
  color: transparent;
  z-index: -1;
}

h1>span {
  background: white;
  padding-bottom: 11px;
}
<h1><span>You're the artist, we simply admire your work.</span></h1>

Please note – the slight hack involves positioning the element 1px differently from the line's height. This is necessary to prevent color residue on modern screens that utilize more than one screen pixel for a single CSS pixel during positioning.

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

I need help figuring out how to showcase the following object in an Angular 5 HTML file

https://i.sstatic.net/XXm3W.png The console screenshot above shows an object with two values: users and tickers, each being an array of values. How can I display these values in an Angular 5 HTML template similar to the screenshot above? I attempted to ...

Looking for a seamless way to convert jsp code to html using sightly in AEM 6.1?

I need help transforming JSP code to HTML using Sightly in AEM. In the JSP code, we have a scriptlet that stores a value from the dialog into a JSP variable called "coltype" using the code pageContext.setAttribute("coltype", xssAPI.filterHTML(properties. ...

What is the best way to eliminate duplicate items in JavaScript?

Note: I am aware that we use Set to eliminate duplicates in an array. I have a date dropdown. When I select a date, it displays the date correctly in a list. However, the problem arises when I select an already chosen date, it still shows up in the list. ...

Tips for retrieving values from numerous checkboxes sharing the same class using jQuery

Currently, I am struggling with getting the values of all checkboxes that are checked using jquery. My goal is to store these values in an array, but I am encountering difficulties. Can anyone provide me with guidance on how to achieve this? Below is what ...

What is the best way to create a dynamic sitemap in Next.js version 14?

I've encountered an issue with the code snippet I'm using for a dynamic sitemap on my blog post website built with Next.js 14. While it works perfectly fine in development, it fails to generate a dynamic sitemap in the build or production environ ...

Concealing a child component when hovering over its parent element with styled-components

I have a react component structured like this - const MyComponent = () => ( <ContainerSection> <DeleteButtonContainer> <Button theme="plain" autoWidth onClick={() = ...

Create a dialog box with rounded triangle corners

In the process of developing an application, we are incorporating a text display feature within a div that resembles a chat box direction, but with a curved style as depicted in the screenshot linked below. Desired design exemplified in purple Target des ...

Display issues with React styled components preventing proper rendering on the screen

Having issues with my style components displaying in a web application I'm developing using React and Ruby on Rails. Everything was working fine until I added a third style component, now even after removing it, nothing shows up on the screen after ru ...

What steps can I take to ensure that my content from the ReactJS for WordPress plugin is easily discoverable by Google

After developing a Wordpress plugin using react and incorporating it as a shortcode, I discovered that the content inside the react app isn't visible through view source or by search engines like Google for SEO purposes. Is there a way to make this c ...

how to combine a string in JavaScript using a prefix and suffix

Anion Gap Body Surface Area (BSA) Creatinine Clearance Stack Overflow i want to add "String" Before Text and "String" after end of text. I have Following expression which runs perfectly in excel:- =CONCATENATE("",B1,"") Output:- <string>Anion Ga ...

Combining Date and Time in Javascript: A Guide

As a JavaScript beginner, I am struggling with combining date and time pickers. Despite finding similar questions, I have not yet found the solution I need. I have two inputs: one for the datePicker and another for the timePicker. <form> <div clas ...

Accessing state property of a different component in ReactJS: A comprehensive guide

I have a main component that incorporates a menu component. The menu component utilizes a state property to store information about the selected menu item. However, I am now faced with the challenge of retrieving the selected module in the main component. ...

Automatically add shimmer with CSS styling

Is it possible to make the shine effect work automatically (without needing hover) every 5 seconds? http://jsfiddle.net/AntonTrollback/nqQc7/ .icon:after { content: ""; position: absolute; top: -110%; left: -210%; width: 200%; height: 200%; ...

Unreachable prevState when utilizing the useState hook

I am currently working on a component where I need to capture the previousState of an element. However, no matter what I try, it keeps returning the initial value. This suggests that there is some re-rendering happening, causing it to constantly default to ...

Google Map in React fails to refresh its styles upon theme change (component re-render)

I have implemented a custom theme on my website, allowing users to switch between light and dark modes. I have also created a custom hook for this purpose. Within my website, I have included a Google Map using the `react-google-maps` library. I have used t ...

Interactive JS chart for visually representing values within a preset range in Vue.js

I was in need of a custom JavaScript chart specifically designed to display a value within a specified upper and lower limit. The main application for this would be illustrating the current stock price between its 52-week high and low ranges. It was essent ...

Is there a way to access the value of a variable within a loop inside a function and store it in a global variable?

I am struggling to retrieve the value of a variable after it has passed through a loop. I attempted to make it a global variable, but its value remains unchanged. Is there any way to achieve this? Below is my code snippet where I am attempting to access t ...

What's the best way to ensure a div's height and width are equal when implementing responsive design?

I am currently working on a responsive design and facing challenges in making div's height and width equal. Initially, I set the div's width as a percentage and height as auto, but this caused the divs to not display properly. To resolve this iss ...

Send properties to the styles of material-ui

I am working on a function that iterates through an array of objects and eventually displays the data as a chart. The issue I'm facing is that I want to pass the item being processed by the map function to Material-UI makeStyles in order to style the ...

Managing browser back button functionality

I've been struggling to find a solution for handling the browser back button event. I would like to prompt the user with a "confirm box" if they click on the browser back button. If they choose 'ok', I need to allow the back button action, ...