在具有右 1:1 比例的圆形边框中制作 Font Awesome 图标

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

在某些情况下,如果图标的比例不是1:1,边框就不再是圆了。

这是一个例子:

我目前正在使用:

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

有什么方法可以使用 CSS 来解决问题吗?

border-radius中的百分比计算为宽度和高度的百分比不同。尝试使用 50px 进行考试。

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>

设置一个固定的widthheight,并使用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>

要在保持图标纵横比的同时实现它,您需要为图标分配一个容器,比例为 1:1(宽度与高度相同)。

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>

您需要设置宽度、高度、行高和文本对齐以使图标居中。 图标还需要垂直对齐重置为中间。

避免以像素为单位进行填充,但以 em 或 rem 为单位使用 width/height/line-height。然后您可以更改字体大小并保持比例而不更新其他值。

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>