【发布时间】:2013-08-30 18:19:23
【问题描述】:
我有一个<li>,它的宽度受到限制,高度设置为 1:1 的纵横比。
然后我在里面有一个元素,它绝对定位为 100% 的宽度和高度。然后我将图标字体添加到 :before 伪元素。如何垂直居中该伪元素?
我的代码是:
li
+span-columns(1, 6)
a
+border-radius(50%)
display: block
position: relative
width: 100%
border: 5px solid $bright-blue
border: remCalc(5px) solid $bright-blue
&:before
padding-top: 100%
content: ''
display: block
span
display: block
position: absolute
top: 0
left: 0
width: 100%
height: 100%
&:before
// Content is added via the style for data-icon
display: inline-block
min-height: 100%
vertical-align: middle
width: 100%
color: $white
font-size: 32px
text-align: center
outline: 1px solid red
问题的图片。红色轮廓在 span:before 上
编译输出:
li {
width: 150px;
}
li a {
border-radius: 50%;
display: block;
position: relative;
width: 100%;
border: 5px solid blue;
}
li a:before {
padding-top: 100%;
content: '';
display: block;
}
li a span {
display: block;
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
li a span:before {
content: attr(data-icon);
font-family: 'IconFont';
display: inline-block;
min-height: 100%;
vertical-align: middle;
width: 100%;
color: white;
font-size: 32px;
text-align: center;
outline: 1px solid red;
}
【问题讨论】:
-
由于您的问题是关于 CSS(而不是关于让 Sass 生成您需要的 CSS),因此提供编译后的 CSS(和演示!)会更有用。
-
我目前无法直接获得编译后的输出,但转换非常容易。现在将为您更新 OP。很遗憾,暂时无法提供演示。
-
为您添加了编译输出。问题基本上是如何垂直对齐容器内的文本(图标字体文本字符串)。
-
您尝试过 any vertical alignment 技术吗?
-
是的,但没有一个行得通。
标签: css sass vertical-alignment pseudo-element