Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Breakable colored underline in CSS

I would like to have a colored underline that looks like this when it breaks:
Heading with colored underline in one and two lines

text-decoration-color seems to be not supported widely enough.

I tried this:

.underline {
  position: relative;
 }

.underline:after {
  position: absolute;
  content: '';
  height: 1px;
  background-color: #ffc04d;
  bottom: .1rem;
  right: 0;
  left: 0;
  z-index: -1;
}
 <h1><span class="underline">Sprouted Bread</span></h1>

and this is what it looks like when it breaks.

like image 898
t3ol5 Avatar asked Mar 20 '18 22:03

t3ol5


People also ask

How do I color underline text in CSS?

By default, the color of the underline is black. In CSS, we will use text-decoration property to style underline. CSS text-decoration-color Property: This property is used to specify the color of decorations (overlines, underlines, and line-throughs) over the text.

How do you make a dotted underline in CSS?

Adding a dotted or double underline The text-decoration property does not have a “double” or “dotted” value. Instead, you can use the border-bottom property to add double or dotted underlining. You can remove a link's default underline by setting the text-decoration proeprty to “none.”

How do you style underline in CSS?

How to Underline a Title in CSS. To underline a title, you can use text-decoration: underline; but you can make it prettier if you use the border-bottom property. In the latter case, however, you need to add display: inline; so that the underline wouldn't be longer than the word itself.

How do I change the color of a tag underline?

Change the underline color by typing a { text-decoration: none; border-bottom:1px solid red; }. Replace solid red with another color.


2 Answers

What about a linear-gradient where it will be easy to control color, size and distance within a single element:

.underline {
  position: relative;
  font-size:28px;
  background:
    linear-gradient(yellow,yellow) /* Color */
    left 0 bottom 2px/ /* Position */
    100% 2px  /* Size (width height)*/
    no-repeat;
 }
<div style="width:150px;text-align:center"><span class="underline">Sprouted Bread</span></div>

As a side note, border-bottom works fine used with inline element but of course you cannot easily control the distance to make it behave as a text-decoration:

.underline {
  position: relative;
  font-size:28px;
  border-bottom:2px solid yellow;
 }
<div style="width:150px;text-align:center"><span class="underline">Sprouted Bread</span></div>
like image 89
Temani Afif Avatar answered Sep 29 '22 22:09

Temani Afif


Try this JSFiddle

By wrapping the elements like you have in a span. You can put the text decoration on the parent element and the text color on the span.

HTML:

<h1><span class="underline">Some Text</span></h1>

CSS:

h1 {
  text-decoration: underline;
  color: red;
}

.underline {
  color: blue;
}
like image 28
Jay Avatar answered Sep 29 '22 22:09

Jay