不影响居中 HTML 元素的伪元素
Pseudo elements that don't affect centered HTML elements
我正在尝试在元素中心完全对齐的圆圈内显示百分比。太棒了,这有效。
但是,我希望数值居中对齐,而“%”浮动在旁边并且不影响位置。
参见代码笔:https://codepen.io/brycesnyder/pen/MEwddv
div {
font-family: sans-serif;
height: 200px;
width: 200px;
background: #ae63e4;
border-radius: 50%;
display: inline-flex;
align-items: center;
justify-content: center;
font-size: 60px;
line-height: 1;
color: white;
text-align: center;
}
div.percent:after {
content: '%';
font-size: 30px;
margin-bottom: 15px;
}
div.a-percent {
position: relative;
background: #0ebeff;
}
div.a-percent:after {
content: '%';
position: absolute;
font-size: 30px;
margin-top: 5px;
}
<div class="percent">0</div>
<div class="percent">10</div>
<div class="percent">100</div>
<br>
<br>
<div class="a-percent">0</div>
<div class="a-percent">10</div>
<div class="a-percent">100</div>
看来我已经想出了如何通过简单地向伪元素添加绝对位置来保持节奏。以前,我曾认为这会导致元素的位置为 0,0。
div {
height: 200px; width: 200px;
background: maroon;
border-radius: 50%;
display: inline-flex;
align-items: center;
justify-content: center;
font-size: 24px;
color: white;
&.percent {
&:after {
content: '%';
position: absolute;
}
}
试试这个:
div {
font-family: sans-serif;
height: 200px; width: 200px;
background: #ae63e4;
border-radius: 50%;
display: inline-flex;
align-items: center;
justify-content: center;
font-size: 60px;
line-height: 1;
color: white;
text-align: center;
&.percent {
&:after {
content: '%';
font-size: 30px;
}
}
}
为什么不在数字的左侧添加相同的百分比符号?
然后用 visibility: hidden
隐藏这个副本。
这在容器中创造了平衡,数字可以完美居中。
div {
font-family: sans-serif;
height: 200px;
width: 200px;
background: #ae63e4;
border-radius: 50%;
display: inline-flex;
align-items: center;
justify-content: center;
font-size: 60px;
line-height: 1;
color: white;
text-align: center;
}
div::after {
content: '%';
font-size: 30px;
margin-bottom: 15px;
}
div::before {
content: '%';
font-size: 30px;
margin-bottom: 15px;
visibility: hidden;
}
div.a-percent {
background: #0ebeff;
}
<div class="percent">0</div>
<div class="percent">10</div>
<div class="percent">100</div>
<br>
<br>
<div class="a-percent">0</div>
<div class="a-percent">10</div>
<div class="a-percent">100</div>
此处有更多详细信息和其他选项:
我正在尝试在元素中心完全对齐的圆圈内显示百分比。太棒了,这有效。
但是,我希望数值居中对齐,而“%”浮动在旁边并且不影响位置。
参见代码笔:https://codepen.io/brycesnyder/pen/MEwddv
div {
font-family: sans-serif;
height: 200px;
width: 200px;
background: #ae63e4;
border-radius: 50%;
display: inline-flex;
align-items: center;
justify-content: center;
font-size: 60px;
line-height: 1;
color: white;
text-align: center;
}
div.percent:after {
content: '%';
font-size: 30px;
margin-bottom: 15px;
}
div.a-percent {
position: relative;
background: #0ebeff;
}
div.a-percent:after {
content: '%';
position: absolute;
font-size: 30px;
margin-top: 5px;
}
<div class="percent">0</div>
<div class="percent">10</div>
<div class="percent">100</div>
<br>
<br>
<div class="a-percent">0</div>
<div class="a-percent">10</div>
<div class="a-percent">100</div>
看来我已经想出了如何通过简单地向伪元素添加绝对位置来保持节奏。以前,我曾认为这会导致元素的位置为 0,0。
div {
height: 200px; width: 200px;
background: maroon;
border-radius: 50%;
display: inline-flex;
align-items: center;
justify-content: center;
font-size: 24px;
color: white;
&.percent {
&:after {
content: '%';
position: absolute;
}
}
试试这个:
div {
font-family: sans-serif;
height: 200px; width: 200px;
background: #ae63e4;
border-radius: 50%;
display: inline-flex;
align-items: center;
justify-content: center;
font-size: 60px;
line-height: 1;
color: white;
text-align: center;
&.percent {
&:after {
content: '%';
font-size: 30px;
}
}
}
为什么不在数字的左侧添加相同的百分比符号?
然后用 visibility: hidden
隐藏这个副本。
这在容器中创造了平衡,数字可以完美居中。
div {
font-family: sans-serif;
height: 200px;
width: 200px;
background: #ae63e4;
border-radius: 50%;
display: inline-flex;
align-items: center;
justify-content: center;
font-size: 60px;
line-height: 1;
color: white;
text-align: center;
}
div::after {
content: '%';
font-size: 30px;
margin-bottom: 15px;
}
div::before {
content: '%';
font-size: 30px;
margin-bottom: 15px;
visibility: hidden;
}
div.a-percent {
background: #0ebeff;
}
<div class="percent">0</div>
<div class="percent">10</div>
<div class="percent">100</div>
<br>
<br>
<div class="a-percent">0</div>
<div class="a-percent">10</div>
<div class="a-percent">100</div>
此处有更多详细信息和其他选项: