Make Font Awesome icons in a circle border with right 1:1 ratio

自作多情 提交于 2021-02-05 07:36:38

问题


In some case, if the ratio of the icon is not 1:1, the border is not a circle anymore.

Here's an example:

I'm currently using:

HTML:

.socials
      a(href='#') <i class="fa fa-facebook"></i>
      a(href='#') <i class="fa fa-twitter"></i>
      a(href='#') <i class="fa fa-google"></i>

SASS:

      border-radius: 50%
      border: solid white
      padding: 10px

Is there anyway that I can use CSS to fix the problem?


回答1:


You need to set width, height, line height, & text-align to center the icon. icon will need also vertical-align reset to middle.

Avoid padding in pixels, but use width/height/line-height in em or rem. You can then change font-size and keep the ratio without updating other values.

a /* or selector a .fa */
  {  
  font-size:3em;
  border-radius: 50%;
  border: solid white;
  color: white;
  line-height: 2em;
  width: 2em;
  height: 2em;
  text-align: center;
  display: inline-block;
    transition:0.5s;
}
/* demo purpose */
a:hover {font-size:2em}
.fa {
/* optionnal vertical-align: middle;*/
}
body {
  background: #333
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
<a href="#"><i class="fa fa-facebook"></i></a>
<a href="#"><i class="fa fa-twitter"></i></a>
<a href="#"> <i class="fa fa-google"></i>
</a>



回答2:


set a fixed width and height, and use text-align:center

i {
  border-radius: 50%;
  border: solid black;
  padding: 10px;
  width:16px;
  height: 16px;
  text-align:center
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<i class="fa fa-facebook"></i>
<i class="fa fa-twitter"></i>
<i class="fa fa-google"></i>



回答3:


border-radius in the percent calculated as a percent of width and height different. try 50px for exam.

div {
  margin: .3em;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 2em;
  color: white;
}
.pc {
  width: 200px;
  height: 80px;
  background-color: blue;
  border-radius: 50%;
}
.px {
  width: 200px;
  height: 80px;
  background-color: blue;
  border-radius: 50px;
}
<div class="pc">%</div>
<div class="px">px</div>



回答4:


To achieve it while keeping the icon with its aspect ratio you need to assign a container to the icon with a 1:1 ratio (same width as height).

i.fa-facebook {
  padding: 10px;
  color: white;
}

.border {
  border-radius: 50%;
  border: solid white;
}

a.icon_container {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: solid white;
  text-align: center;
  float: left;
}

body {
  background: black;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<i class="fa fa-facebook border"></i>
<a href="#" class="icon_container border"><i class="fa fa-facebook"></i></a>


来源:https://stackoverflow.com/questions/41532908/make-font-awesome-icons-in-a-circle-border-with-right-11-ratio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!