CSS - 在里面创建一个带有垂直对齐文本的圆形DIV

问题描述 投票:0回答:2

我想用bootstrap 3创建一个圆形DIV,并尝试在垂直居中设置一些文本,但我不能......

这是HTML

<div class="circle">
<div>
<span>Info</span><br/>
<span class="big">1000</span>
</div>
</div>

用这个css

.circle{
    border-radius:50%;
    border: 3px solid #fff;
    background-color:red;
    display:table;
    width:100%;
    height:auto;    
    padding-top:100%;

}

.circle > div{
    display: table-cell;
    vertical-align: middle;
}

.big {
  font-size:30px;
}

如果我没有放任何文字,圆圈就是完美的,文字就变成了椭圆形......在这里你可以找到它的小提琴

Fiddle

css
2个回答
1
投票

你可能会使用flexbox:

.circle {
  border-radius: 50%;
  border: 3px solid #fff;
  background-color: red;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  text-align: center;
}

.circle:after {
  content: '';
  padding-bottom: 100%;
}

.big {
  font-size: 30px;
}
<div class="circle">
  <div class="circle-inner">
    <span>Info</span><br/>
    <span class="big">1000</span>
  </div>
</div>

0
投票

试试这个:

小提琴:https://jsfiddle.net/intervalia/jhz1xvcL/1/

.circle{
	border-radius:50%;
	border: 3px solid #fff;
	background-color:red;
	display:table;
	width:100%;
	height:auto; 	
	padding-top:100%;
	position: relative;
}

.circle > div{
	display: flex;
	align-items: center;
  align-content: center;
  justify-content: center;
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
}

.big {
  font-size:30px;
}
<div class="circle">
<div>
<span>Info</span><br/>
<span class="big">1000</span>
</div>
</div>
© www.soinside.com 2019 - 2024. All rights reserved.