本文介绍了CSS - 没有固定宽度和高度的圆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在圆圈内显示通知计数,但我不希望它有固定宽度,因此当圈子中有更大的数字/文字时,圈子可以展开。
.circle {height:20px; width:20px; line-height:20px; border-radius:50%; background-color:red;颜色:白色; text-align:center; }
< div class =circle> 5< / div>< br>< div class =circle> 102< / div>
解决方案
查看此CSS仅解决方案。为1位数字设置相同的值 min-width
和 min-height
。使用伪元素进行垂直对齐并保持方形。 border-radius
适用于圈子的容器。
.circle {display:inline-block; border-radius:50%; min-width:20px; min-height:20px; padding:5px;背景:红色;颜色:白色; text-align:center; line-height:1; box-sizing:content-box; white-space:nowrap;}。circle:before {content:; display:inline-block; vertical-align:middle; padding-top:100%; height:0;}。circle span {display:inline-block; vertical-align:middle;}
< div class = circle>< span> 8< / span>< / div>< div class =circle>< span> 64< / span>< / div>< div class = >< span>< / span>< / span>< / span> 512< / span>< / div>< div class =circle>< pre>
I want to display the notification count inside a circle but I don't want it to have a fixed width so the circle can expand when there is a bigger number/text inside the circle.
.circle {
height: 20px;
width: 20px;
line-height: 20px;
border-radius: 50%;
background-color: red;
color: white;
text-align: center;
}
<div class="circle"> 5 </div>
<br>
<div class="circle"> 102 </div>
解决方案
See this CSS only solution. Set the same value of min-width
and min-height
for 1 digit number. Use a pseudo element for vertical alignment and to maintain the square shape. With border-radius
applies to the container for the circle.
.circle {
display: inline-block;
border-radius: 50%;
min-width: 20px;
min-height: 20px;
padding: 5px;
background: red;
color: white;
text-align: center;
line-height: 1;
box-sizing: content-box;
white-space: nowrap;
}
.circle:before {
content: "";
display: inline-block;
vertical-align: middle;
padding-top: 100%;
height: 0;
}
.circle span {
display: inline-block;
vertical-align: middle;
}
<div class="circle"><span>8</span></div>
<div class="circle"><span>64</span></div>
<div class="circle"><span>512</span></div>
<div class="circle"><span>4096</span></div>
这篇关于CSS - 没有固定宽度和高度的圆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!