Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create custom underline or highlight for text in html or css?

I'm trying to figure out how to create a custom background effect for text. In other words, how can I make something like this: enter image description here

like image 596
Luka Gavasheli Avatar asked Apr 28 '17 14:04

Luka Gavasheli


People also ask

How do I create a custom underline in CSS?

Steps: Create background image with linear-gradient() . Adjust its size with css background-size property. Place it at the bottom left position of the element with background-position css property.

How do you highlight text in HTML CSS?

How Do I Highlight Text In CSS? To Highlight text in HTML you have to use an inline element such as the <span> element and apply a specific background style on it. This will create the highlighting effect, which you can tweak in many different ways to create different looks.

How do I make text underlined in HTML?

To underline a text in HTML, use the <u> tag. The <u> tag deprecated in HTML, but then re-introduced in HTML5. Now it represents a text different from another text stylistically, such as a misspelled word. To underline a text, you can also use the style attribute.

How do you put underline under text in CSS?

In the horizontal text we use text-underline-position: under; to put the underline below all the descenders. In the text with a vertical writing-mode set, we can then use values of left or right to make the underline appear on the left or right of the text as required.


4 Answers

Use the <mark> element and tweek the line-height

mark {
  display: inline-block;
  line-height: 0em;
  padding-bottom: 0.5em;
}
<h1><mark>Lorem ipsum</mark></h1>
like image 75
caramba Avatar answered Oct 19 '22 04:10

caramba


A very good article explains a nice way to do that with gradients: https://thirtyeightvisuals.com/blog/low-highlight-heading-links-squarespace

.highlight {
  background: linear-gradient(180deg,rgba(255,255,255,0) 50%, #FFD0AE 50%);
}
like image 25
Qortex Avatar answered Oct 19 '22 03:10

Qortex


For these I usually use an SVG pixel (a 1x1 stretchable HTML-encoded SVG with a fill color) that we can manoeuvre anyway we want:

h1 {
  background: url("data:image/svg+xml;charset=utf8,%3Csvg version='1.1' xmlns='http://www.w3.org/2000/svg' x='0px' y='0px' width='1px' height='1px' viewBox='0 0 1 1' preserveAspectRatio='none'%3E%3Crect x='0' y='0' width='1' height='1' fill='red' /%3E%3C/svg%3E") no-repeat 100% 100%;
  background-size: 100% 50%;
}
<h1>My Text</h1>

This also allows for animations to be easily added. This only works on single-line items, however. You can change the color inside the svg fill property. If encoded it works on IE9+, so it's pretty compatible! Just remember that the hash sign in front of hex colors needs to be encoded as well - its %23 (personally I use sass to encode it for me).

like image 2
somethinghere Avatar answered Oct 19 '22 03:10

somethinghere


You can easily achieve this using a linear-gradient set to the background property.

CSS

.low-highlight {
  background:linear-gradient(180deg, transparent 60%, yellow 60%);
}

HTML

<p>You can easily <span class="low-highlight">do underline</span> me.</p>

p {
font-size:68px;
  font-weight:600;
  font-family:'Merriweather';
}


.low-highlight {
  background:linear-gradient(180deg, transparent 60%, yellow 60%);
}
<p>You can easily <span class="low-highlight">do underline</span> me.</p>
like image 1
Muneeb Ejaz Avatar answered Oct 19 '22 03:10

Muneeb Ejaz