如何将图标对齐div元素中的中心?

时间:2017-01-14 15:26:07

标签: html css center

我想将图标居中于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>

4 个答案:

答案 0 :(得分:4)

position: relative;添加到.rectangle以及以下CSS中的图标:

.fa-quora {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  }

&#13;
&#13;
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;
&#13;
&#13;

答案 1 :(得分:2)

将此添加到您的.rectangle课程

display: flex;
justify-content: center;
align-items: center;

示例代码段

&#13;
&#13;
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;
&#13;
&#13;

如果您想支持旧浏览器,请将其添加到.rectangle

display: table-cell;
text-align: center;
vertical-align: middle;

示例代码段

&#13;
&#13;
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;
&#13;
&#13;

答案 2 :(得分:0)

&#13;
&#13;
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;
&#13;
&#13;

答案 3 :(得分:0)

我不确定你的要求......

您可以在padding-left课程的padding-toprectangle中添加一些像素。之后,使用相同的像素数减小宽度和高度。但是除了Q之外的其他字符可以具有不同的渲染。

&#13;
&#13;
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;
&#13;
&#13;

这是另一个没有固定高度的例子。它使用flex。

&#13;
&#13;
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;
&#13;
&#13;