创建圆形div比使用图像更简单吗?

时间:2011-01-30 02:19:17

标签: html css geometry css-shapes

我想知道创建循环div的方法是否比我现在所做的更容易。

目前,我只是为每个不同的尺寸制作图像,但这样做很烦人。

无论如何使用CSS制作圆形的div,我可以指定半径吗?

15 个答案:

答案 0 :(得分:277)

以下是演示:http://jsfiddle.net/thirtydot/JJytE/1170/

<强> CSS:

.circleBase {
    border-radius: 50%;
    behavior: url(PIE.htc); /* remove if you don't care about IE8 */
}

.type1 {
    width: 100px;
    height: 100px;
    background: yellow;
    border: 3px solid red;
}
.type2 {
    width: 50px;
    height: 50px;
    background: #ccc;
    border: 3px solid #000;
}
.type3 {
    width: 500px;
    height: 500px;
    background: aqua;
    border: 30px solid blue;
}

<强> HTML:

<div class="circleBase type1"></div>

<div class="circleBase type2"></div><div class="circleBase type2"></div>

<div class="circleBase type3"></div>

要在IE8 and older中完成此工作,您必须下载并使用CSS3 PIE。我上面的演示在IE8中不起作用,但这只是因为jsFiddle没有托管PIE.htc

我的演示看起来像这样:

答案 1 :(得分:21)

将元素每边的边框半径设置为50%将创建任意大小的圆形显示:

.circle {
  border-radius: 50%;
  width: 200px;
  height: 200px; 
  /* width and height can be anything, as long as they're equal */
}

答案 2 :(得分:13)

试试这个

.iphonebadge {
  border-radius:99px;
 -moz-border-radius:99px;
 -webkit-border-radius:99px;
  background:red;
  color:#fff;
  border:3px #fff solid;
  background-color: #e7676d;
  background-image: -webkit-gradient(linear, left top, left bottom, from(#e7676d), to(#b7070a)); /* Saf4+, Chrome */
  background-image: -webkit-linear-gradient(top, #e7676d, #b7070a); /* Chrome 10+, Saf5.1+, iOS 5+ */
  background-image: -moz-linear-gradient(top, #e7676d, #b7070a); /* FF3.6 */
  background-image: -ms-linear-gradient(top, #e7676d, #b7070a); /* IE10 */
  background-image: -o-linear-gradient(top, #e7676d, #b7070a); /* Opera 11.10+ */
  background-image: linear-gradient(top, #e7676d, #b7070a);
  filter: progid:DXImageTransform.Microsoft.gradient(startColorStr='#e7676d', EndColorStr='#b7070a'); 
 -webkit-box-shadow: 0px 2px 4px #000000; /* Saf3-4 */
 -moz-box-shadow: 0px 2px 4px #000000; /* FF3.5 - 3.6 */
  box-shadow: 0px 2px 4px #000000; /* Opera 10.5, IE9, FF4+, Chrome 10+ */
  display:inline-block;
  padding:2px 2px 2px 2px ;
  margin:3px;
  font-family:arial;
  font-weight:bold;
   }

答案 3 :(得分:6)

实际上是可能的。

请参阅:CSS Tip: How to Make Circles Without Images。请参阅demo

但要注意,它在兼容性方面存在严重缺点,你正在制造猫吠。

看到它正常工作 here

您将看到,您必须将heightwidth设置为border-radius的一半

祝你好运!

答案 4 :(得分:3)

还有[不好的想法]使用几个(20+)水平或垂直1px div来构造一个圆圈。 This jQuery plugin使用此方法构造不同的形状。

答案 5 :(得分:3)

根据尺寸给出宽度和高度,但保持两者相等

.circle {
  background-color: gray;
  height: 400px;
  width: 400px;
  border-radius: 100%;
}
<div class="circle">
</div>

答案 6 :(得分:3)

.fa-circle{
  color: tomato;
}

div{
  font-size: 100px;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<div><i class="fa fa-circle" aria-hidden="true"></i></div>

只是想提一下另一个解决方案,它解决了“比使用图像更容易创建圆形div的问题?”这是使用FontAwesome。

您导入fontawesome css文件或从CDN here

导入

然后你就是:

<div><i class="fa fa-circle" aria-hidden="true"></i></div>

你可以给它任何你想要任何字体大小的颜色。

答案 7 :(得分:1)

您可以使用半径,但它不适用于IE:border-radius: 5px 5px;

答案 8 :(得分:1)

您可以尝试radial-gradient CSS功能:

.circle {
    width: 500px;
    height: 500px;
    border-radius: 50%;
    background: #ffffff; /* Old browsers */
    background: -moz-radial-gradient(center, ellipse cover, #ffffff 17%, #ff0a0a 19%, #ff2828 40%, #000000 41%); /* FF3.6-15 */
    background: -webkit-radial-gradient(center, ellipse cover, #ffffff 17%,#ff0a0a 19%,#ff2828 40%,#000000 41%); /* Chrome10-25,Safari5.1-6 */
    background: radial-gradient(ellipse at center, #ffffff 17%,#ff0a0a 19%,#ff2828 40%,#000000 41%); /* W3C, IE10+, FF16+, Chrome26+, Opera12+, Safari7+ */
}

将其应用于div图层:

<div class="circle"></div>

答案 9 :(得分:1)

.circle {
	height: 20rem;
	width: 20rem;
	border-radius: 50%;
	background-color: #EF6A6A;
}
<div class="circle"></div>

答案 10 :(得分:0)

基本上这使用div的绝对位置来将字符放在给定的坐标处。所以使用圆的参数方程,你可以绘制一个圆。如果你要将div的位置改为亲戚,那么它会导致正弦波......

本质上我们通过滥用position属性来绘制方程。我不熟悉css,所以有人可以让这更优雅。享受。

这适用于所有浏览器和移动设备(我都知道)。我在自己的网站上使用它来绘制正弦波的文本(www.cpixel.com)。此代码的原始来源可在此处找到:www.mathopenref.com/coordcirclealgorithm.html

    <html>
    <head></head>
    <body>
    <script language="Javascript">

    var x_center = 50; //0 in both x_center and y_center will place the center
    var y_center = 50; // at the top left of the browser
    var resolution_step = 360; //how many times to stop along the circle to plot your character.
    var radius = 50; //how big ya want your circle?
    var plot_character = "·"; //could use any character here, try letters/words for cool effects
    var div_top_offset=10;
    var div_left_offset=10;
    var x,y;

    for ( var angle_theta = 0;  angle_theta < 2 * Math.PI;  angle_theta += 2 * Math.PI/resolution_step ){
        x = x_center + radius * Math.cos(angle_theta);
        y = y_center - radius * Math.sin(angle_theta);
        document.write("<div style='position:absolute;top:" + (y+div_top_offset) + ";left:"+ (x+div_left_offset) + "'>" + plot_character + "</div>");
    }

    </script>
    </body>
    </html>

答案 11 :(得分:0)

添加Error: $ operator is invalid for atomic vectors > is.recursive(c14) [1] TRUE > is.atomic(c14) [1] FALSE 属性:

#### data input #### c14 <- read.table("dates_c14", header=TRUE) c14 <- as.data.frame(t(c14)) c14 # V1 V2 V3 V4 V5 # labID A B C D E # age 1550 2080 2680 3625 3940 # error 30 30 35 35 35 # depth 45.0 62.0 82.5 100.5 104.0 #### code #### library(rbacon) agedepth(set ="c14", BCAD = FALSE, unit = "cm", d.lab = "Depth (cm)", yr.lab ="cal BP", d.min = 1, d.max = 107, d.by = 1, depths = seq(0,1,length=107), depths.file = FALSE, yr.min =2016, yr.max = -3000, bins = c(), dark = set$dark, prob = 0.95, rounded = 1, d.res = 200, yr.res = 200, date.res = 100, grey.res = 100, rotate.axes = FALSE, rev.yr = FALSE, rev.d = FALSE, maxcalc = 500, height = 15, mirror = TRUE, up = TRUE, cutoff = 0.001, plot.range = TRUE, panels = layout(1), range.col = grey(0.5), range.lty = "12", mn.col = "red", mn.lty = "12", med.col = NA, med.lty = "12", C14.col = rgb(0, 0, 1, 0.35), C14.border = rgb(0, 0, 1,0.5), cal.col = NA,cal.border = NA, hiatus.lty = "12", greyscale = function(x) grey(1 - x), normalise.dists = TRUE, cc=1, title ="Gulf of Aqaba Core", title.location = "topleft", after =1, bty = "l", mar = c(3,3, 1, 1), mgp = c(1.5, 0.7, 0), xaxs = "r", yaxs = "r", xaxt = "s", yaxt = "s", plot.pdf = TRUE, dates.only = FALSE, model.only = FALSE, cleanup = TRUE, talk = TRUE)

到任何div使它循环。

答案 12 :(得分:0)

假设您有此图片:

要对此圈出一个圆圈,您只需添加

.circle {
  border-radius: 50%;
  width: 100px;
  height: 100px; 
}

因此,如果您有div,则可以执行相同的操作。

检查以下示例:

.circle {
  border-radius: 50%;
  width: 100px;
  height: 100px; 
  animation: stackoverflow-example infinite 20s linear;
  pointer-events: none;
}

@keyframes stackoverflow-example {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}
<div>
  <img class="circle" src="https://www.sitepoint.com/wp-content/themes/sitepoint/assets/images/icon.javascript.png">
</div>

答案 13 :(得分:0)

我有4种解决方案来完成此任务:

  1. 边界半径
  2. 剪切路径
  3. 伪元素
  4. 径向渐变

#circle1 {
  background-color: #B90136;
  width: 100px;
  height: 100px;
  border-radius: 50px;/* specify the radius */
}

#circle2 {
  background-color: #B90136;
  width: 100px;/* specify the radius */
  height: 100px;/* specify the radius */
  clip-path: circle();
}

#circle3::before {
  content: "";
  display: block;
  width: 100px;
  height: 100px;
  border-radius: 50px;/* specify the radius */
  background-color: #B90136;
}

#circle4 {
  background-image: radial-gradient(#B90136 70%, transparent 30%);
  height: 100px;/* specify the radius */
  width: 100px;/* specify the radius */
}
<h3>1 border-radius</h3>
<div id="circle1"></div>
<hr/>
<h3>2 clip-path</h3>
<div id="circle2"></div>
<hr/>
<h3>3 pseudo element</h3>
<div id="circle3"></div>
<hr/>
<h3>4 radial-gradient</h3>
<div id="circle4"></div>

答案 14 :(得分:-1)

对于圆形,创建一个div元素,然后输入width =边框半径的2倍=填充的2倍。同样行高= 0 例如,使用50px作为圆的半径,下面的代码效果很好:

width: 100px;
padding: 50px 0;
border: solid;
line-height: 0px;
border-radius: 50px;