0

I want to create a rounded div that has a font awesome i arrow tag centered in it like this:

I tried to wrap the i tag inside a div and center it with margin 0 auto but it does not center it perfectly, how can I do it?

here is my html:

<div class="parent">
    <div class="child">
      <i class="fas fa-arrow-up"></i>
    </div>
</div>

and my css:

.parent {
    width: 300px;
    background: #f00;
    height: 300px;
    margin: 0 auto;
    border-radius: 50%;
}

.child {
    width: 150px;
    margin: auto;
    height: 100px;
   }

and a link to my fiddle: http://jsfiddle.net/philipkovachev9/8kgxL59f/4/

Rupert
  • 3
  • 2
  • Besides the given answers, you can also try this: https://stackoverflow.com/a/21906433/2928853 – jrook Oct 09 '18 at 17:32

4 Answers4

3

You can stack fontawesome icons, so just use the circle and arrow:

<link href="https://use.fontawesome.com/releases/v5.3.1/css/all.css" rel="stylesheet"/>
<span class="fa-stack fa-2x">
  <i class="far fa-circle fa-stack-2x"></i>
  <i class="fas fa-arrow-up fa-stack-1x"></i>
</span>
j08691
  • 204,283
  • 31
  • 260
  • 272
  • Nice, but that way I can't style the border of the circle in a different color – Rupert Oct 09 '18 at 17:22
  • @Rupert of course you can. http://jsfiddle.net/j08691/n592t8rw/. And if that's a requirement of yours, you should mention it in your question. – j08691 Oct 09 '18 at 17:24
2

You can center it like this:

.parent {
  width: 300px;
  background: #f00;
  height: 300px;
  margin: 0 auto;
  border-radius: 50%;
}

.child {
  width: 150px;
  margin: auto;
  height: 100px;
  position: relative;
}

.child i {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
0
.parent {
   width: 300px;
   background: #f00;
   height: 300px;
   margin: 0 auto;
   border-radius: 50%;
 }

.child {
  font-size: 100px;
  display: flex;
  justify-content: center;
  align-items: center;
  position: absolute;
  top: 25%;
  left: 50%;
  transform: translate(-50%, -50%);
 }
Asiya Fatima
  • 1,388
  • 1
  • 10
  • 20
0

I would rather just give the <i> a circle without the need to add any divs. It shortens your code and you will have a perfect circle as an outcome.

HTML:

<div class="social>
  <i class="fas fa-arrow-up"></i>
</div>

CSS:

.social .fas {
  margin-right: 1rem;
  border: 2px #fff solid;
  border-radius: 50%;
  height: 20px;
  width: 20px;
  line-height: 20px;
  text-align: center;
  padding: 0.5rem;
}
Alessio
  • 3,404
  • 19
  • 35
  • 48
Armando Guarino
  • 1,120
  • 1
  • 5
  • 16