是否可以仅使用CSS来绘制圆圈,并在大多数浏览器(IE,Mozilla,Safari)上实现?
是否可以仅使用CSS来绘制圆圈,并在大多数浏览器(IE,Mozilla,Safari)上实现?
画一个框,并给它一个边框半径,其大小为框的一半:
#circle {
background: #f00;
width: 200px;
height: 200px;
border-radius: 50%;
}
演示效果:
#circle {
background: #f00;
width: 200px;
height: 200px;
border-radius: 50%;
}
<div id="circle"></div>
.circle:before {
content: ' \25CF';
font-size: 200px;
}
<span class="circle"></span>
在IE8及以下版本中,border-radius不起作用,因此我建议使用此方法(我知道这个建议有点疯狂)。
:before
在IE7及更早版本中无效,因此这种方法仅在IE8上获得支持,但使得正确定位圆形非常困难。例如,200px的字体大小并不等同于直径为200px的圆,某些系统上会失去抗锯齿效果。 - Tatu Ulmanenmargin: -0.5em -0.3em -0.3em -0.1em
。 - Pierre de LESPINAYborder-radius
属性可以使其呈圆形。 (注意:很长一段时间以来,不需要为前缀)background-color
/渐变/(甚至是pseudo elements
)进行调整,以创建像这样的效果:.red {
background-color: red;
}
.green {
background-color: green;
}
.blue {
background-color: blue;
}
.yellow {
background-color: yellow;
}
.sphere {
height: 200px;
width: 200px;
border-radius: 50%;
text-align: center;
vertical-align: middle;
font-size: 500%;
position: relative;
box-shadow: inset -10px -10px 100px #000, 10px 10px 20px black, inset 0px 0px 10px black;
display: inline-block;
margin: 5%;
}
.sphere::after {
background-color: rgba(255, 255, 255, 0.3);
content: '';
height: 45%;
width: 12%;
position: absolute;
top: 4%;
left: 15%;
border-radius: 50%;
transform: rotate(40deg);
}
<div class="sphere red"></div>
<div class="sphere green"></div>
<div class="sphere blue"></div>
<div class="sphere yellow"></div>
<div class="sphere"></div>
如果遇到旧版IE的困扰,可以尝试使用HTML代码,而边框半径则是一个不错的选择。
•
使用CSS更改颜色。输出:
•
这将在所有浏览器中正常工作
#circle {
background: #f00;
width: 200px;
height: 200px;
border-radius: 50%;
-moz-border-radius: 50%;
-webkit-border-radius: 50%;
}
<style>
.circle{
width: 100px;
height: 100px;
border-radius: 50%;
background-color: blue
}
</style>
<div class="circle">
</div>