ChatGPT解决这个技术问题 Extra ChatGPT

Make Font Awesome icons in a circle?

I am using font awesome on some project but I have some things that I want to do with font awesome icons, I can easily call an icon like this:

<i class="fa fa-lock"></i>

Is it possible to all icon always be in round circle with border? Something like this, I have a picture:

https://i.stack.imgur.com/m7xGh.jpg

Using

i {
  background-color: white;
  border-radius: 50%;
  border: 1px solid grey;
  padding: 10px;
}

Will do the effect, but the problem is that icons are always bigger that the text or element beside, any solutions?


T
TombMedia

With Font Awesome you can easily use stacked icons like this:

<span class="fa-stack fa-2x">
  <i class="fas fa-circle-thin fa-stack-2x"></i>
  <i class="fas fa-lock fa-stack-1x fa-inverse"></i>
</span>

refer Font Awesome Stacked Icons

Update:- Fiddle for stacked icons


The stacked icons only work up till 2x. If you want larger than that, it doesn't work
wish this was the accepted answer, its the "correct" way to do this with fontawesome. Accepted answer is a hack that does not adjust well.
Same with numbers <span class="fa-stack fa-lg"> <i class="fa fa-circle-thin fa-stack-2x"></i> <i class="fa fa-stack-1x">1</i> </span>
@emrah - its the only option here that is truely fontawesome, not custom css, so its more precise then anything here.
Very elegant solution. @mmativ if you mean a background color on the circle you simply do
N
Nico O

i.fa { display: inline-block; border-radius: 60px; box-shadow: 0 0 2px #888; padding: 0.5em 0.6em; }


Got sometning, but not round circle?
The border-radius should round the element. What brower do you use? will updated my answer with prefixes
But this is not a solution. This only works if the i has content. Usually with FA you don't have content within the i-tag, but the icon is rendered by CSS later: <i class="fa fa-lock"></i> and this is what @Schneider actually asked for.
@Jan I will investigate in that tomorrow. But i can not see a Problem. You are Right, that the tags don't have content, but the content will be added via css. Even when the Icon would fail to load, you would see an circle. Can you make a jsFiddle of a Problem you see with the answer?
No. This is the answer to the question. See the images of the question. There is a shadow, not a hard circle.
C
Community

You don't need css or html tricks for it. Font Awesome has built in class for it - fa-circle To stack multiple icons together you can use fa-stack class on the parent div

<span class="fa-stack fa-lg">
  <i class="fa fa-circle fa-stack-2x"></i>
  <i class="fa fa-flag fa-stack-1x fa-inverse"></i>
</span> 

//And we have now facebook icon inside circle:)


Sampat Badhe has told in his answer the same more than 2 years ago. Why dont you read other answers before writing yours?
hey, sorry for Sampat, i didn't see him answer, i'll try to modify my answer and add more solutions or helpful info.
It is a good solution, but it gives a blue background to each of the icon. Any work around ?
@CodeIt Just use css to clear it or add new background color. It shouldn't be a problem.
Could you look into this? stackoverflow.com/questions/64270359/…
D
Dave Everitt

Font icon in a circle using em as the base measurement

if you use ems for the measurements, including line-height, font-size and border-radius, with text-align: center it makes things pretty solid:

#info i {
  font-size: 1.6em;
  width: 1.6em;
  text-align: center;
  line-height: 1.6em;
  background: #666;
  color: #fff;
  border-radius: 0.8em; /* or 50% width & line-height */
}

Could you look into this? stackoverflow.com/questions/64270359/…
a
atilkan

Update: Rather use flex.

If you want precision this is the way to go.

Fiddle. Go Play -> http://jsfiddle.net/atilkan/zxjcrhga/

Here is the HTML

<div class="sosial-links">
    <a href="#"><i class="fa fa-facebook fa-lg"></i></a>
    <a href="#"><i class="fa fa-twitter fa-lg"></i></a>
    <a href="#"><i class="fa fa-google-plus fa-lg"></i></a>
    <a href="#"><i class="fa fa-pinterest fa-lg"></i></a>
</div>

Here is the CSS

.sosial-links a{
    display: block;
    float: left;
    width: 36px;
    height: 36px;
    border: 2px solid #909090;
    border-radius: 20px;
    margin-right: 7px; /*space between*/

} 
.sosial-links a i{
    padding: 12px 11px;
    font-size: 20px;
    color: #909090;
}

NOTE: Don't use this anymore. Use flexbox.


adding text-align:center; Makes it work with pretty much everything even fa-info
how about vertical align?
Could you look into this? stackoverflow.com/questions/64270359/…
n
nclsvh

UPDATE:

Upon learning flex recently, there is a cleaner way (no tables and less css). Set the wrapper as display: flex; and to center it's children give it the properties align-items: center; for (vertical) and justify-content: center; (horizontal) centering.

See this updated JS Fiddle

Strange that nobody suggested this before.. I always use tables to do this.
Simply make a wrapper have display: table and center stuff inside it with text-align: center for horizontal and vertical-align: middle for vertical alignment.

<div class='wrapper'>
  <i class='icon fa fa-bars'></i>
</div>

and some sass like this

.wrapper{
  display: table; 

  i{
    display: table-cell;
    vertical-align: middle;
    text-align: center;
  }

}

or see this JS Fiddle


This ist a very nice and clean and small one! Heres a fiddle with CSS instead of SCSS jsfiddle.net/bqtkmy0s
@summsel isn't this the exact same thing as my original answer? You just wrote the code in scss and not css.
yes, of course it is. I tried your solution in css and there are plenty of people out there prefering css. So why not post the jsfiddle of this css-version.
Could you look into this? stackoverflow.com/questions/64270359/…
This is easiest solution and should have been accepted answer, as this doesn't need to add new elements as shown in accepted or highest voted answer. This answer follow KISS technique very well.
I
Ian Blair

You can also do this. I wanted to add a circle around my icomoon icons. Here is the code.

span {
font-size: 54px;
border-radius: 50%;
border: 10px solid rgb(205, 209, 215);
padding: 30px;
}

m
mmativ

This is the approach you don't need to use padding, just set the height and width for the a and let the flex handle with margin: 0 auto.

.social-links a{ text-align:center; float: left; width: 36px; height: 36px; border: 2px solid #909090; border-radius: 100%; margin-right: 7px; /*space between*/ display: flex; align-items: flex-start; transition: all 0.4s; -webkit-transition: all 0.4s; } .social-links a i{ font-size: 20px; align-self:center; color: #909090; transition: all 0.4s; -webkit-transition: all 0.4s; margin: 0 auto; } .social-links a i::before{ display:inline-block; text-decoration:none; } .social-links a:hover{ background: rgba(0,0,0,0.2); } .social-links a:hover i{ color:#fff; }


Could you look into this? stackoverflow.com/questions/64270359/…
P
Petros Kyriakou

The below example didnt quite work for me,this is the version that i made work!

HTML:

<div class="social-links">
    <a href="#"><i class="fa fa-facebook fa-lg"></i></a>
    <a href="#"><i class="fa fa-twitter fa-lg"></i></a>
    <a href="#"><i class="fa fa-google-plus fa-lg"></i></a>
    <a href="#"><i class="fa fa-pinterest fa-lg"></i></a>
</div>

CSS:

.social-links {
    text-align:center;
}

.social-links a{
    display: inline-block;
    width:50px;
    height: 50px;
    border: 2px solid #909090;
    border-radius: 50px;
    margin-right: 15px;

}
.social-links a i{
    padding: 18px 11px;
    font-size: 20px;
    color: #909090;
}

M
MaxineHu

try this

HTML:

<div class="icon-2x-circle"><i class="fa fa-check fa-2x"></i></div>

CSS:

i {
    width: 30px;
    height: 30px;
}

.icon-2x-circle {
    text-align: center;
    padding: 3px;
    display: inline-block;
    -moz-border-radius: 100px;
    -webkit-border-radius: 100px;
    border-radius: 100px;
    -moz-box-shadow: 0px 0px 2px #888;
    -webkit-box-shadow: 0px 0px 2px #888;
    box-shadow: 0px 0px 2px #888;
}

T
TSlegaitis

Font Awesome icons are inserted as a :before. Therefore you can style either your i or a like so:

.i {
   background: #fff;
   border-radius: 50%;
   display: inline-block;
   height: 20px;   
   width: 20px;
}

<a href="#"><i class="fa fa-facebook fa-lg"></i></a>

Could you look into this? stackoverflow.com/questions/64270359/…
J
Jude Obiejesi

You can simply get round icon using this code:

<a class="facebook-share-button social-icons" href="#" target="_blank">
    <i class="fab fa-facebook socialicons"></i>
</a>

Now your CSS will be:

.social-icons {
display: inline-block;border-radius: 25px;box-shadow: 0px 0px 2px #888;
padding: 0.5em;
background: #0D47A1;
font-size: 20px;
}
.socialicons{color: white;}

Could you look into this? stackoverflow.com/questions/64270359/…
O
Oznog

I like Dave Everitt's answer with the « line-height » but it only works by specifying the « height » and then we have to add « !important » to line-height ...

.cercle {
    font-size: 2em;
    width: 2em;
    height: 2em;
    text-align: center;
    line-height: 2em!important;
    background: #666;
    color: #fff;
    border-radius: 2em;
}

The line-height was solving it for me
Adding !important is a band-aid, not a solution. It means you have another CSS property somewhere else overriding your property.
A
Armando Guarino

This is the best and most precise solution I've found so far.

CSS:

.social .fa {
      margin-right: 1rem;
      border: 2px #fff solid;
      border-radius: 50%;
      height: 20px;
      width: 20px;
      line-height: 20px;
      text-align: center;
      padding: 0.5rem;
    }