How to use CSS to achieve gradient animation border effects (with code)

Source: Internet
Author: User
This article brings you the content is about how to use CSS to achieve the effect of gradient animation border (with code), there is a certain reference value, the need for friends can refer to, I hope to help you.

Effect Preview

Source code Download

Https://github.com/comehope/front-end-daily-challenges/tree/master/016-colorful-gradient-animated-border

Code interpretation

Defines the DOM, a container that contains some text:

<div class= "box" > You is    my<br>    favorite</div>

Center display:

Html,body,.box {    height:100%;    Display:flex;    Align-items:center;    Justify-content:center;}

Set Page background color:

Body {    background: #222;}

To set the container and text style:

. box {    color:white;    font-size:2.5em;    Width:10em;    height:5em;    Background: #111;    Font-family:sans-serif;    line-height:1.5em;    Text-align:center;    Border-radius:0.2em;}

Add a backplane with pseudo-elements:

. box {    position:relative;}. Box::after {    content: ';    Position:absolute;    width:102%;    height:104%;    Background-color:orange;    Z-index:-1;    Border-radius:0.2em;}

To set the backplane to a gradient:

. box::after {    /*background-color:orange;*/    background-image:linear-gradient (60deg, Aquamarine, Cornflowerblue, Goldenrod, Hotpink, Salmon, lightgreen, sandybrown, violet);}

Animate the Backplane:

. box::after {    background-size:300%, 300%;    ANIMATION:ANIMATE_BG 5s ease infinite alternate;} @keyframes ANIMATE_BG {    0% {        background-position:0%, 50%;    }    50% {        background-position:100%, 50%;    }    100% {        background-position:0%, 50%;    }}

Finally, add color effects to the text:

. box {    animation:animate_text 2s linear infinite alternate;} @keyframes Animate_text {from    {        color:lime;    }    to {        color:yellow;    }}

Done!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.