CSS ::before pseudo 属性 在使用字体时可以约束宽度分配吗?
Can CSS ::before pseudo property constrain to a width assignment when using a font?
我正在使用 FontAwesome 字体在我当前的项目中推送图标,并且想在某些标签上使用 ::before 伪 属性。
正如您在下图中看到的,标签的对齐方式已关闭。我希望使用 'width' 属性 来解决这个问题,以确保图标在水平方向上占据相同数量的 space。
我目前使用的CSS是
.title::before {
font-family: FontAwesome;
font-size: 1.2em;
margin-right: 5px;
width:35px; // this can be 34523px and still does nothing for the view
}
&.error .title::before {
content: $fa-var-exclamation-circle;
color: $color_error;
}
虽然我知道该对象是一个字体字形并且有它自己的字体大小。我不想操纵较宽图标的字体大小来适应差异。当我按原样应用宽度时,没有变化,如上所述。
这似乎是风格的理性元素。我错过了什么吗?
让我们让它回答:添加
display: inline-block;
到元素 --> http://jsfiddle.net/d72bo9q1/1/
.xxx:before {
content: 'x';
background-color: green;
width: 100px;
display:inline-block;
}
.xxx {
background-color:orange;
}
<div class="xxx">bla bla bla</div>
尝试将 display: inline-block;
和 text-align: center;
添加到 .title::before
.title::before {
display: inline-block;
text-align: center;
width:35px;
font-family: FontAwesome;
font-size: 1.2em;
margin-right: 5px;
}
display: inline-block;
允许您为元素设置宽度。 text-align: center;
确保图标彼此居中。
希望对您有所帮助。
我正在使用 FontAwesome 字体在我当前的项目中推送图标,并且想在某些标签上使用 ::before 伪 属性。
正如您在下图中看到的,标签的对齐方式已关闭。我希望使用 'width' 属性 来解决这个问题,以确保图标在水平方向上占据相同数量的 space。
我目前使用的CSS是
.title::before {
font-family: FontAwesome;
font-size: 1.2em;
margin-right: 5px;
width:35px; // this can be 34523px and still does nothing for the view
}
&.error .title::before {
content: $fa-var-exclamation-circle;
color: $color_error;
}
虽然我知道该对象是一个字体字形并且有它自己的字体大小。我不想操纵较宽图标的字体大小来适应差异。当我按原样应用宽度时,没有变化,如上所述。
这似乎是风格的理性元素。我错过了什么吗?
让我们让它回答:添加
display: inline-block;
到元素 --> http://jsfiddle.net/d72bo9q1/1/
.xxx:before {
content: 'x';
background-color: green;
width: 100px;
display:inline-block;
}
.xxx {
background-color:orange;
}
<div class="xxx">bla bla bla</div>
尝试将 display: inline-block;
和 text-align: center;
添加到 .title::before
.title::before {
display: inline-block;
text-align: center;
width:35px;
font-family: FontAwesome;
font-size: 1.2em;
margin-right: 5px;
}
display: inline-block;
允许您为元素设置宽度。 text-align: center;
确保图标彼此居中。
希望对您有所帮助。