[英]Creating a circle using `border-radius`
I am reading HTML and CSS by Jon Duckett, and have been introduced to the border-radius
property. 我正在阅读Jon Duckett撰写的HTML和CSS ,并已介绍到
border-radius
属性。
I am trying to create a circle using the code below, but the circle is not perfect. 我正在尝试使用下面的代码创建一个圆圈,但圆圈并不完美。 I am using the radius of
50px
but it isn't perfect. 我使用的半径为
50px
但并不完美。
p { border: 5px solid #ee3e80; padding: 10px; width: 100px; height: 100px; display: inline-block; margin: 20px; } p.three { padding: 0px; border-radius: 50px; -moz-border-radius: 50px; -webkit-border-radius: 50px; }
<p class="three"></p>
What am I doing wrong? 我究竟做错了什么?
padding
and the width of the border
is calculated additionally to the width
and height
of your element. padding
和border
的宽度是根据元素的width
和height
计算的。
You have different options to solve this: 你有不同的选择来解决这个问题:
box-sizing: border-box
to your element which defines what should include in the size calculation box-sizing: border-box
到元素中,该元素定义了大小计算中应包含的内容 border-radius: 50%
border-radius: 50%
border-radius
. border-radius
。 Here the solution just with box-sizing
这里的解决方案只是使用
box-sizing
p { display: inline-block; margin: 20px; width: 100px; height: 100px; /* these values get added to your 100px by default */ border: 5px solid #ee3e80; padding: 10px; } p.three { padding: 0px; border-radius: 50px; /* now width and height determine how big your element is as a whole */ box-sizing: border-box; }
<p class="three"></p>
For a more detailed explanation about the CSS box model look at this article from MDN . 有关CSS框模型的更详细说明,请参阅MDN上的这篇文章 。
It should be 50%
, not 50px
. 它应该是
50%
,而不是50px
。 50%
will always draw a circle regardless of the size of the element. 无论元素的大小如何,
50%
将始终绘制圆圈。 Setting a pixel value will only draw a circle if the element is sufficiently small. 如果元素足够小,则设置像素值将仅绘制圆形。
See below. 见下文。
p { border: 5px solid #ee3e80; padding: 10px; width: 100px; height: 100px; display: inline-block; margin: 20px; } p.three { padding: 0px; border-radius: 50%; -moz-border-radius: 50%; -webkit-border-radius: 50%; }
<p class="three"></p>
It's because you didn't take into account the width coming from the border width, which is 5px
on each end. 这是因为你没有考虑边界宽度的宽度,每边的宽度为
5px
。 So the total width is 110px
, so your border radius will need to be 55px
. 因此总宽度为
110px
,因此您的边界半径需要为55px
。 An easier way for a perfect circle is to just set border-radius
to 50%
. 完美圆的更简单方法是将
border-radius
设置为50%
。
p { border: 5px solid #ee3e80; padding: 10px; width: 100px; height: 100px; display: inline-block; margin: 20px; } p.three { padding: 0px; border-radius: 50%; -moz-border-radius: 50%; -webkit-border-radius: 50%; }
<p class="three"></p>
You just need to add 50%
to the border-radius
property. 您只需要向
border-radius
属性添加50%
。 Below is a snippet and you will find it is a perfect circle. 下面是一个片段,你会发现它是一个完美的圆圈。
p { border: 5px solid #ee3e80; padding: 10px; width: 100px; height: 100px; display: inline-block; margin: 20px; } p.three { padding: 0px; border-radius: 50%; -moz-border-radius: 50%; -webkit-border-radius: 50%; }
<p class="three"></p>
Yet another option is to set your element's box-sizing
property to border-box
(as I do for nearly all elements). 另一种选择是将元素的
box-sizing
属性设置为border-box
(就像我几乎所有元素一样)。
p { border: 5px solid #ee3e80; padding: 10px; width: 100px; height: 100px; display: inline-block; margin: 20px; box-sizing: border-box; /* < -------------------- here */ } p.three { padding: 0px; border-radius: 50px; -moz-border-radius: 50px; -webkit-border-radius: 50px; }
<p class="three"></p>
Border-box
takes into consideration border when doing math, and generally simplifies layout and styling when applied across the board. Border-box
在进行数学运算时会考虑边框,并且在全面应用时通常会简化布局和样式。 Libraries like Bootstrap do this for you . 像Bootstrap 这样的库为你做这件事 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.