【发布时间】:2014-09-25 12:06:17
【问题描述】:
我正在使用flaticon创建的字体
我的问题是我无法将图标垂直放置在 div 中
我什么都试过了,vertical-align: middle,改变位置:absolute/relative,margin,padding,line-height = div 的高度,什么都有!
flaticon css
@font-face {
font-family: "Flaticon";
src: url("font/flaticon.eot");
src: url("font/flaticon.eot#iefix") format("embedded-opentype"),
url("font/flaticon.woff") format("woff"),
url("font/flaticon.ttf") format("truetype"),
url("font/flaticon.svg") format("svg");
font-weight: normal;
font-style: normal;
}
[class^="flaticon-"]:before, [class*=" flaticon-"]:before,
[class^="flaticon-"]:after, [class*=" flaticon-"]:after {
font-family: Flaticon;
font-style: normal;
}
.flaticon-synchronization1:before {
content: "\e01e";
}
我的 CSS
body{
background-color: gray;
}
#btnTest{
width: 100px;
height: 100px;
border: 1px solid blue;
text-align: center;
}
#btnTest > span{
border: 1px solid red;
}
#btnTest > span::before{
font-size: 30px;
color: white;
border: 1px solid yellow;
}
html
<div id="btnTest"><span class="flaticon-synchronization1"></span></div>
更新,解决方案
#btnTest{
width: 100px;
height: 100px;
display: table;
}
#btnTest > span{
display: table-cell;
vertical-align: middle;
}
#btnTest > span::before{
font-size: 30px;
color: white;
border: 1px solid yellow;
}
【问题讨论】:
-
将
line-height: 100px;添加到#btnTest > span::before -
我在 Chrome 控制台中打开了您的示例,只是为了尝试一下,并将“span::before”元素的位置设置为相对,我可以通过调整“top”值来更改位置。这就是你要找的吗?
-
有趣的是,在我的浏览器中它看起来不像你的图像。事实上,它是完全居中的。
-
试试这个 - jsfiddle.net/rv264cww
-
@Mary Melody,您的解决方案有效!