我想将图标居中于div元素“rectangle”。任何其他替代解决方案也受到欢迎。
body {
background-color: black;
}
.rectangle {
width: 125px;
height: 125px;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
background-color: #fff;
}
h3 {
color: #fff;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<div class="rectangle">
<span><i class="fa fa-quora" aria-hidden="true" style="font-size: 75px;" align="center"></i></span>
</div>
<h3>Quora</h3>
答案 0 :(得分:4)
将position: relative;
添加到.rectangle
以及以下CSS中的图标:
.fa-quora {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
body {
background-color: black;
}
.rectangle {
width: 125px;
height: 125px;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
background-color: #fff;
position: relative;
}
.fa-quora {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
h3 {
color: #fff;
}
&#13;
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<div class="rectangle">
<span><i class="fa fa-quora" aria-hidden="true" style="font-size: 75px;"></i></span>
</div>
<h3>Quora</h3>
&#13;
答案 1 :(得分:2)
将此添加到您的.rectangle
课程
display: flex;
justify-content: center;
align-items: center;
示例代码段
body {
background-color: black;
}
.rectangle {
width: 125px;
height: 125px;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
background-color: #fff;
display: flex;
justify-content: center;
align-items: center;
}
h3 {
color: #fff;
}
&#13;
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<div class="rectangle">
<span><i class="fa fa-quora" aria-hidden="true" style="font-size: 75px;" align="center"></i></span>
</div>
<h3>Quora</h3>
&#13;
如果您想支持旧浏览器,请将其添加到.rectangle
类
display: table-cell;
text-align: center;
vertical-align: middle;
示例代码段
body {
background-color: black;
}
.rectangle {
width: 125px;
height: 125px;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
background-color: #fff;
display: table-cell;
text-align: center;
vertical-align: middle;
}
h3 {
color: #fff;
}
&#13;
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<div class="rectangle">
<span><i class="fa fa-quora" aria-hidden="true" style="font-size: 75px;" align="center"></i></span>
</div>
<h3>Quora</h3>
&#13;
答案 2 :(得分:0)
body {
background-color: black;
}
.rectangle {
width: 125px;
height: 125px;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
background-color: #fff;
position: absolute;
left: 50%;
}
h3 {
color: #fff;
}
&#13;
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<div class="rectangle">
<span><i class="fa fa-quora" aria-hidden="true" style="font-size: 75px; position: absolute; left: 50%;"></i></span>
</div>
<h3>Quora</h3>
&#13;
答案 3 :(得分:0)
我不确定你的要求......
您可以在padding-left
课程的padding-top
和rectangle
中添加一些像素。之后,使用相同的像素数减小宽度和高度。但是除了Q之外的其他字符可以具有不同的渲染。
body {
background-color: black;
}
.rectangle {
width: 98px;
height: 98px;
padding-top: 22px;
padding-left: 22px;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
background-color: #fff;
}
h3 {
color: #fff;
}
&#13;
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<div class="rectangle">
<span><i class="fa fa-quora" aria-hidden="true" style="font-size: 75px;" align="center"></i></span>
</div>
<h3>Quora</h3>
&#13;
这是另一个没有固定高度的例子。它使用flex。
body {
background-color: black;
}
.rectangle {
width:125px;
display: flex;
justify-content: center;
align-items: center;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
background-color: #fff;
}
h3 {
color: #fff;
}
&#13;
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<span class="rectangle">
<span><i class="fa fa-quora" aria-hidden="true" style="font-size: 75px;" align="center"></i></span>
</span>
<h3>Quora</h3>
&#13;