Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS transition not working with underline

Tags:

html

css

I am using css to make an underline come under a span:

CSS:

.un{     text-decoration:none;     transition: all .5s ease-in; } .un:hover{     text-decoration:underline; }   

HTML:

<span class="un"> Underlined Text - Or to be underlined </span> 

The underline simply appears, it doesn't move in over .5 seconds, like the transition should apply. Why not? How can I make this work?

like image 346
yainspan Avatar asked May 20 '15 14:05

yainspan


People also ask

How do you add an underline to a transition in CSS?

Use display: inline-block to make the underline span just the width of the text content. Use the :after pseudo-element with width: 100% and position: absolute to place it below the content. Use transform: scaleX(0) to initially hide the pseudo-element.

Can text-decoration be transitioned?

As it turns out, text-decoration is not one of the properties that can be animated at all, including transitions.

How do I remove the underline from a hyperlink in CSS?

By setting the text-decoration to none to remove the underline from anchor tag. Syntax: text-decoration: none; Example 1: This example sets the text-decoration property to none.

How do you underline the position of text?

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.


1 Answers

Updated for 2021:

The support for text-decoration-color has come a long way, and common browser support requirements have loosened making it a viable option for most new projects. If you are only seeking a color transition, and can do without IE support, see this answer below.


Original answer:

You cannot change the color of the text-decoration independent of the color. However, you can achieve a similar effect with pseudo elements:

.un {   display: inline-block; }  .un::after {   content: '';   width: 0px;   height: 1px;   display: block;   background: black;   transition: 300ms; }  .un:hover::after {   width: 100%; }
<span class="un">Underlined Text - Or to be underlined</span>

That is the most customizable way to do it, you can get all sorts of transitions. (Try playing around with the margins/alignment. You can make some awesome effects without adding to your HTML)
But if you just want a simple underline, use a border:

.un {   transition: 300ms;   border-bottom: 1px solid transparent; }  .un:hover {   border-color: black; }
<span class="un"> Underlined Text - Or to be underlined </span>
like image 88
Jacob Gray Avatar answered Sep 21 '22 16:09

Jacob Gray