33

I have this code here:

echo "<u><font color='red'><br>$username</font></u>";

Firstly, as you can see, it's underlined (< u >). Second, all text is all red. Well is there anyway to leave the text ($username) red but the underline black?

friedemann_bach
  • 1,418
  • 14
  • 29
user1641508
  • 325
  • 1
  • 3
  • 4

14 Answers14

63

There's now a new css3 property for this: text-decoration-color

So you can now have text in one color and a text-decoration underline - in a different color... without needing an extra 'wrap' element

p {
  text-decoration: underline;
  -webkit-text-decoration-color: red; /* safari still uses vendor prefix */
  text-decoration-color: red;
}
<p>black text with red underline in one element - no wrapper elements here!</p>

Codepen

NB:

1) Browser Support is limited at the moment to Firefox and Chrome (fully supported as of V57) and Safari

2) You could also use the text-decoration shorthand property which looks like this:

<text-decoration-line> || <text-decoration-style> || <text-decoration-color>

...so using the text-decoration shorthand - the example above would simply be:

p {
  text-decoration: underline red;
}

p {
  text-decoration: underline red;
}
<p>black text with red underline in one element - no wrapper elements here!</p>
Danield
  • 121,619
  • 37
  • 226
  • 255
  • 1
    This is cool, but besides the lacking support - it's also nice to have a bit more control - which you can get with border-bottom, however - using inline-block on long stretches will surely break the magic... so this would be the best solution for that. – sheriffderek May 02 '14 at 18:26
  • it's 2016 and still lacking support in Chrome. sigh. – Jason S Sep 23 '16 at 16:20
  • Two years later, and this property still has virtually no browser support. Please, see [my method outlined below](http://stackoverflow.com/questions/12557707/changing-underline-color/28917336#28917336) for single-line underlines. – Huski Jan 11 '17 at 13:42
  • 2
    @JasonS - Chrome now fully supports this :) – Danield Mar 22 '17 at 09:19
23

Update from author:
This answer is outdated since text-decoration-color is now supported by most modern browsers.


:pseudo + em

In order to accurately replicate the size, stroke width, and positioning of the native text-decoration:underline without introducing extra HTML markup, you should use a pseudo-element with em units. This allows for accurate scaling of the element and native behavior without additional markup.

CSS

a {
  text-decoration: none;
  display: inline-table;
}

a:after {
  content: "";
  border-bottom: 0.1em solid #f00;
  display: table-caption;
  caption-side: bottom;
  position: relative;
  margin-top:-0.15em;
}

By using display:table-caption and caption-side on the pseudo-element and display inline-table, we can force the browser to vertically-align both line and link accurately, even when scaled.

In this instance, we use inline-table instead of inline-block to force the pseudo to display without the need to specify height or negative values.

Examples

JSFIDDLE: https://jsfiddle.net/pohuski/8yfpjuod/8/
CODEPEN: http://codepen.io/pohuski/pen/vEzxPj | (example with scaling)

Successfully Tested On:
Internet Explorer: 8, 9, 10, 11
Firefox: 41, 40, 39, 38, 37, 36
Chrome: 45, 44, 43, 42
Safari: 8, 7, 6.2
Mobile Safari: 9.0, 8.0
Android Browser: 4.4, 2.3
Dolphin Mobile: 8, 11.4

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
Huski
  • 958
  • 1
  • 7
  • 14
  • +1 for use of peudo elements, which offers a pure-css approach with great control over the properties of the 'underline' – CodeToad May 20 '15 at 12:52
  • Great solution but I can only get this to look good in Chrome, is it suppose to work in other browsers as well? It's completely broken in Firefox. – Richard B Sep 19 '15 at 11:39
  • @RichardB I have updated the answer to use the `caption-side` property, which should account for those browser quirks. I updated the fiddle and codepen links. – Huski Sep 26 '15 at 03:27
  • 2
    Behavior for multi line text/link is not same as with text-decoration. Still it's better to use one more element/span. – Jonáš Krutil Mar 26 '16 at 11:40
  • This doesn't work with elements that wrap on to multiple lines, or if you have a 1 line element that scales down and wraps for mobile devices. If you add enough content to your tests they will break. – iain Feb 07 '17 at 13:29
  • 1
    In addition to what has been said, this renders underlines incorrectly with descender letters, e.g. `g`. – Jari Keinänen Mar 21 '18 at 10:05
12

No. The best you can do is to use a border-bottom with a different color, but that isn't really underlining.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
12

In practice, it is possible, if you use span element instead of font:

<style>
u { color: black; }
.red { color: red }
</style>
<u><span class='red'><br>$username</span></u>

See jsfiddle. Appears to work on Chrome, Safari, Firefox, IE, Opera (tested on Win 7 with newest versions).

The code in the question should work, too, but it does not work for some reason on WebKit browsers (Chrome, Safari).

By the CSS spec: “The color(s) required for the text decoration must be derived from the 'color' property value of the element on which 'text-decoration' is set. The color of decorations must remain the same even if descendant elements have different 'color' values.”

Jukka K. Korpela
  • 195,524
  • 37
  • 270
  • 390
  • Note for present-day readers: `` is deprecated, but you can achieve the same style with other elements (e.g. div or span) and CSS as well. – Jari Keinänen Mar 21 '18 at 10:23
  • 1
    `` was deprecated in HTML 4, but HTML 5 gave it the status of a normal element (and HTML 5 doesn’t use the term “deprecated”; instead, it uses “obsolete”). HTML 5 redefines the meaning of `` in a weird way, but that’s a different story. Regarding this question, the only thing that matters in `` is that it (by default) causes its content to appear as underlined. https://www.w3.org/TR/html/textlevel-semantics.html#elementdef-u – Jukka K. Korpela Mar 26 '18 at 10:34
6

The easiest way I've tackled this is with CSS:

<style>
.redUnderline {
    color: #ff0000;
    border-bottom: 1px solid #000000;
}
</style>
<span class="redUnderline">$username</span>

Also, for an actual underline, if your item is a link, this works:

<style>
a.blackUnderline {
   color: #000000;
   text-decoration: underline;
}
.red {
    color: #ff0000;
}
</style>
<a href="" class="blackUnderline"><span class="red">$username</span></a>
AE Grey
  • 368
  • 4
  • 14
3

You can also use the box-shadow property to simulate an underline.

Here is a fiddle. The idea is to use two layered box shadows to position the line in the same place as an underline.

a.underline {
    text-decoration: none;
    box-shadow: inset 0 -4px 0 0 rgba(255, 255, 255, 1), inset 0 -5px 0 0 rgba(255, 0, 0, 1);
}
Berry Blue
  • 15,330
  • 18
  • 62
  • 113
3

Another way that the one described by danield is to have a child container width display inline, and the tipography color you want. The parent element width the text-decoration, and the color of underline you want. Like this:

div{text-decoration:underline;color:#ff0000;display:inline-block;width:50px}
div span{color:#000;display:inline}
<div>
  <span>Hover me, i can have many lines</span>
</div>
Samuel Tesler
  • 345
  • 2
  • 9
  • +1 This is the modern version of what [Jukka](https://stackoverflow.com/a/12558784/216129) described in his answer (as `` is deprecated). – Jari Keinänen Mar 21 '18 at 10:09
2

A pseudo element works best.

a, a:hover {
  position: relative;
  text-decoration: none;
}
a:after {
  content: '';
  display: block;
  position: absolute;
  height: 0;
  top:90%;
  left: 0;
  right: 0;
  border-bottom: solid 1px red;
}

See jsfiddle.

You don't need any extra elements, you can position it as close or far as you want from the text (border-bottom is kinda far for my liking), there aren't any extra colors that show up if your link is over a different colored background (like with the box-shadow trick), and it works in all browsers (text-decoration-color only supports Firefox as of yet).

Possible downside: The link can't be position:static, but that's probably not a problem the vast majority of the time. Just set it to relative and all is good.

Johnny5k
  • 458
  • 5
  • 9
1

You can use this CSS to "simulate" an underline:

text-decoration: none;
border-bottom: 1px solid #000;
alex
  • 479,566
  • 201
  • 878
  • 984
Gung Foo
  • 13,392
  • 5
  • 31
  • 39
1

You can wrap your <span> with a <a> and use this little JQuery plugin to color the underline. You can modify the color by passing a parameter to the plugin.

    (function ($) {
        $.fn.useful = function (params) {

            var aCSS = {
                'color' : '#d43',
                'text-decoration' : 'underline'
            };

            $.extend(aCSS, params);

            this.wrap('<a></a>');
            var element = this.closest('a');
            element.css(aCSS);
            return element;
        };
    })(jQuery);

Then you call by writing this :

    $("span.name").useful({color:'red'});

$(function () {

        var spanCSS = {
            'color' : '#000',
            'text-decoration': 'none'
        };
        
        $.fn.useful = function (params) {
            
            var aCSS = {
                'color' : '#d43',
                'text-decoration' : 'underline'
            };

            $.extend(aCSS, params);
            this.wrap('<a></a>');
            this.closest('a').css(aCSS);
        };

        // Use example:
        $("span.name").css(spanCSS).useful({color:'red'});



    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<section class="container">

    <div class="user important">
        <span class="name">Bob</span>
        -
        <span class="location">Bali</span>
    </div>

    <div class="user">
        <span class="name">Dude</span>
        -
        <span class="location">Los Angeles</span>
    </div>


    <div class="user">
        <span class="name">Gérard</span>
        -
        <span class="location">Paris</span>
    </div>

</section>
Tristan C
  • 31
  • 5
1

here we can create underline with color in text

<u style="text-decoration-color: red;">The color of the lines should now be red!</u>

or

The color of the lines should now be red!

<h1 style=" text-decoration:underline; text-decoration-color: red;">The color of the lines should now be red!</u>
1

I think the easiest way to do this is in your css, use:

text-decoration-color: red;

This will change the color of the underline without changing the color of your text. Good luck!

Cassie H.
  • 373
  • 2
  • 7
  • 24
1

Best way I came across doing is like this:

HTML5:

<p>Initial Colors <a id="new-color">Different Colors</a></p>

CSS3:

p {
    color: #000000;
    text-decoration-line: underline;
    text-decoration-color: #a11015;
}

p #new-color{
    color: #a11015;
    text-decoration-line: underline;
    text-decoration-color: #000000;
}
0

Problem with border-bottom is the extra distance between the text and the line. Problem with text-decoration-color is lack of browser support. Therefore my solution is the use of a background-image with a line. This supports any markup, color(s) and style of the line. top (12px in my example) is dependent on line-height of your text.

 u {
    text-decoration: none;
    background: transparent url(blackline.png) repeat-x 0px 12px;
}
JanP
  • 1,579
  • 2
  • 16
  • 27