不使用固定宽度和高度的CSS圈

不使用固定宽度和高度的CSS圈

本文介绍了不使用固定宽度和高度的CSS圈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在圆圈内显示通知数量,但我不希望它具有固定的宽度,因此圆圈可以在圆圈内有更大的数字/文本时展开。



   
=jsdata-hide =falsedata-console =truedata-babel =false>

  .circle {display:inline-block;边界半径:50%; min-width:20px; min-height:20px;填充:5px;背景:红色;白颜色; text-align:center; line-height:1;方块大小:内容框; white-space:nowrap;}。circle:before {content:;显示:inline-block; vertical-align:middle;填充顶部:100%; height:0;}。circle span {display:inline-block; vertical-align:middle;}  
< div class = circle>< span> 8< / span>< / div>< div class =circle>< span> 64< / span>< < span> 512< / span>< / div>< div class =circle>< span> 4096< / span>< / div> 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圈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 03:41