CSS实现一个动态渐变边框

在这里插入图片描述
html

<div class="box">
    动态渐变边框
</div>

css

<style>
    html,
    body {
        height: 100vh;
        width: 100vw;
        display: flex;
        align-items: center;
        justify-content: center;
    }

    body {
        background-color: #313131;
    }

    .box {
        width: 400px;
        height: 300px;
        background-color: #222;
        border-radius: 10px;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 20px;
        color: #fff;
        font-weight: 800;
    }

    .box {
        position: relative;
        /* 设置相对定位以后,子元素中的绝对定位范围会在该div内 */
    }

    .box::before {
        content: "";
        position: absolute;
        left: -20px;
        top: -20px;
        width: calc(100% + 40px);
        /* calc方法可以用来动态计算长度 */
        height: calc(100% + 40px);
        border-radius: 10px;
        z-index: -1;
        background-image: linear-gradient(135deg,
                #ff0000,
                #ff7f00,
                #ffff00,
                #00ff00,
                #00ffff,
                #0000ff,
                #8b00ff);
        background-size: 400%;
        /* 背景放大,动画更明显 */
        animation: animate_box 5s linear infinite;
    }

    @keyframes animate_box {

        0%,
        100% {
            background-position: 0%, 50%;
        }

        50% {
            background-position: 100%, 50%;
        }
    }
</style>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值