1

I would like to center icon to the div element "rectangle". Any other alternative solution is also welcomed.

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>
Johannes
  • 64,305
  • 18
  • 73
  • 130
akietta
  • 371
  • 1
  • 4
  • 11

4 Answers4

8

Add position: relative; to .rectangle and the following CSS for the icon:

.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;
}
<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>
Johannes
  • 64,305
  • 18
  • 73
  • 130
7

By adding this to your .rectangle class

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

Sample snippet

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;
}
<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>

If you want to support older browsers, add this to your .rectangle class

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

Sample snippet

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;
}
<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>
Asons
  • 84,923
  • 12
  • 110
  • 165
1

I'm not sure what your asking...

You can add some pixels in padding-left and padding-top on rectangle class. After that your reduce the width and the height with the same number of pixels. But other characters than Q can have a different rendering.

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;
}
<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>

Here is another example without fixing height. It's using 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;
}
<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>
Alexandre Tranchant
  • 4,426
  • 4
  • 43
  • 70
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;
}
<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>
Zer0
  • 1,580
  • 10
  • 28