问题
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