使用伪元素来创建圆形背景,避开背景不遵循边框半径的问题。具体方法如下所示:
HTML 代码:
CSS 代码:
.circle {
position: relative;
width: 100px;
height: 100px;
border-radius: 50%;
overflow: hidden;
background-color: #f00; /* 这里是背景颜色 */
}
.circle::before {
content: "";
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
border-radius: inherit;
background-color: #f00; /* 这里是背景颜色 */
transform: scale(1.2); /* 调整伪元素大小以实现圆形背景 */
}
这里使用了伪元素 ::before
来创建圆形背景。通过设置 border-radius
属性为 inherit
,使得伪元素也继承了父元素 .circle
的圆角半径。同时,通过设置 transform
属性来调整伪元素的大小以达到要求的圆形背景效果。