CSS link underline animation

CSS link underline animation

Default underlining of links

Default underlining of links in HTML looks very boring and unattractive. Standard links are blue and underscore, and do not respond on mouse hover. In this lesson, you will learn how to make link underlines more interactive.


Стандартная ссылка

Animation of the appearance of text in CSS.

Remove link underline

Before making the underlining of links more creative, you need to remove the underlining from the links altogether. Add just one property text-decoration with the value of nonewhich defaults to underline.


// CSS
a {
    text-decoration: none;
}

Animation of the appearance of text in CSS.

Underline links on hover

Now let’s make a simple underlining of the links on mouse hover. When we hover the mouse, the link is instantly underlined, but as soon as we remove it, the underline disappears. Most of the links on the Internet are implemented using the usual hover-effect. That’s boring too, let’s make the underline process even more fun with animation elements.


a:hover {
    text-decoration: underline;
}

Link underline animation

Let our links be underlined on the left and right sides, as well as in the center. Let’s create HTML– markup for three links with different classes and place them in the parent container left-wing.


// HTML

Animation of the appearance of text in CSS.

General CSS styles


@import url("https://fonts.googleapis.com/css2?family=Roboto+Condensed:wght@700&display=swap");
body {
    font-family: "Roboto Condensed", sans-serif;
    font-weight: 300;
    font-size: 24px;
}
.links {
    margin: auto; /* автоматическое задание отступов */
    width: 80%; /* ограничение по ширине родителя */
}
a {
    text-decoration: none; /* ссылка без подчеркивания */
    position: relative; /* относительное позиционирование */
    color: darkred; /* цвет ссылок */
}

Animation of the appearance of text in CSS.

Next, we write CSS styles for each class separately. The trick is that we will not animate the links themselves, but their pseudo-elements before… For classes left и right, we will draw lines (underscore) with zero width. When you hover over the link, the line will take on a width of 100%, the effect of a smooth underline of the link will appear. The trick with underlining links is to change the line width from 0% to 100%.

Link underline to the left


.left a::before {
    content: ""; /* отображает псевдоэлемент */
    bottom: 0; /* позиция линии снизу */
    right: 0; /* позиция линии справа */
    position: absolute; /* абсолютное позиционирование */
    width: 0%; /* ширина линии */
    height: 4px; /* толщина линии */
    background-color: darkred; /* цвет линии */
    transition: 0.6s; /* плавный переход */
}
.left a:hover:before {
    width: 100%; /* новая ширина линии */
    left: 0; /* исчезновение линии слева */
}

Underline link to the right


.right a::before {
    content: "";
    bottom: 0;
    right: 0;
    position: absolute;
    width: 0%;
    height: 4px;
    background-color: darkred;
    transition: 0.6s;
}
.right a:hover:before {
    width: 100%;
}

Center underline

Drawing an underline from the center is fundamentally different from the previous two options. Immediately set the width of the underline to 100%. Property transform with the value of stairs will decrease the width of the element to 0, and on hover will increase the width of the element to its original size – 100% width along the horizontal axis.


.center a::before {
    content: "";
    bottom: 0;
    left: 0;
    position: absolute;
    width: 100%;
    height: 4px;
    background-color: darkred;
    transition: 0.6s;
    transform: scaleX(0);
}
.center a:hover:before {
    transform: scaleX(1);
}

Take a look at an example at CodePen

Previous article Next article

Copying of materials is allowed only with the indication of the author (Mikhail Rusakov) and an indexed direct link to the site (http://myrusakov.ru)!

Add to my friends VK: http://vk.com/myrusakov.
If you want to rate me and my work, then write it in my group: http://vk.com/rusakovmy.

If you do not want to miss new materials on the site,
then you can subscribe to updates: Subscribe to updates

If you still have any questions, or you have a desire to comment on this article, then you can leave your comment at the bottom of the page.

Recommend this article to your friends:

If you liked the site, then post a link to it (on your site, on the forum, in contact):

  1. Button:

    It looks like this: How to create your website

  2. Text link:

    It looks like this: How to create your website

  3. BB-code of the link for forums (for example, you can put it in the signature):

Related Posts

Property Management in Dubai: Effective Rental Strategies and Choosing a Management Company

“Property Management in Dubai: Effective Rental Strategies and Choosing a Management Company” In Dubai, one of the most dynamically developing regions in the world, the real estate…

In Poland, an 18-year-old Ukrainian ran away from the police and died in an accident, – media

The guy crashed into a roadside pole at high speed. In Poland, an 18-year-old Ukrainian ran away from the police and died in an accident / illustrative…

NATO saw no signs that the Russian Federation was planning an attack on one of the Alliance countries

Bauer recalled that according to Article 3 of the NATO treaty, every country must be able to defend itself. Rob Bauer commented on concerns that Russia is…

The Russian Federation has modernized the Kh-101 missile, doubling its warhead, analysts

The installation of an additional warhead in addition to the conventional high-explosive fragmentation one occurred due to a reduction in the size of the fuel tank. The…

Four people killed by storm in European holiday destinations

The deaths come amid warnings of high winds and rain thanks to Storm Nelson. Rescuers discovered bodies in two separate incidents / photo ua.depositphotos.com Four people, including…

Egg baba: a centuries-old recipe of 24 yolks for Catholic Easter

They like to put it in the Easter basket in Poland. However, many countries have their own variations of “bab”. The woman’s original recipe is associated with…

Leave a Reply

Your email address will not be published. Required fields are marked *