.
3ss.cn

使用纯CSS画一具圆环(代码示例)

画圆环思想很简单:首先画两个圆,设置不同的背景色;然后让两个圆的圆心重合即可。

难度系数

☆☆

HTML

<div class="container">
    <span class="ring-style"></span>
</div>

解析:

此处有父容器

CSS

.container {
    position: relative;
    top: 0;
    left: 0;
    width: 300px;
    height: 300px;
    background-color: lightgrey;
}
.ring-style {
    display: inline-block;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: blue;
    width: 260px;
    height: 260px;
    border-radius: 260px;
}
.ring-style::before {
    content: ' ';
    display: inline-block;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: white;
    width: 200px;
    height: 200px;
    border-radius: 200px;
}

解析:

设置元素的宽高、圆角效果,即可画出一个圆通过 ::before 伪元素和本体元素,创建两个圆通过基于父容器的绝对定位,设置 top、left、translate 属性,让元素基于父容器水平、竖直居中,即可让两个圆的圆心重合

效果图

知识点

border-radius::before && ::after元素水平、竖直居中

赞(0)
未经允许不得转载:互联学术 » 使用纯CSS画一具圆环(代码示例)

评论 抢沙发