html - 将 CSS 应用于 anchor 的类与通过父元素应用于 anchor

标签 html css

<分区>

我正在尝试应用样式,但是当我尝试将样式直接应用到类“.fa {}”时,它看起来与我将它应用到“#contact a {}”时不同

/*
.fa {
  padding: 40px;
  font-size: 30px;
  width: 30px;
  text-align: center;
  text-decoration: none;
  margin: 15px;
  border-radius: 50%;
}
*/

#contact>a {
  padding: 40px;
  font-size: 30px;
  width: 30px;
  text-align: center;
  text-decoration: none;
  margin: 15px;
  border-radius: 50%;
}

.fa:hover {
  opacity: 0.7;
}

.fa-facebook {
  background: #3B5998;
  color: white;
}

.fa-twitter {
  background: #55ACEE;
  color: white;
}

.fa-google {
  background: #dd4b39;
  color: white;
}

.fa-linkedin {
  background: #007bb5;
  color: white;
}

.fa-youtube {
  background: #bb0000;
  color: white;
}

.fa-instagram {
  background: #125688;
  color: white;
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">

<section id="contact">
  <a href="#" class="fa fa-facebook"></a>
  <a href="#" class="fa fa-twitter"></a>
  <a href="#" class="fa fa-google"></a>
  <a href="#" class="fa fa-linkedin"></a>
  <a href="#" class="fa fa-youtube"></a>
  <a href="#" class="fa fa-instagram"></a>
</section>

最佳答案

因为.fa默认css在你使用.fa class时先应用,影响了font-size:30px,所以使用a.fa 或使用 !important 覆盖默认 .fa 的字体大小

.fa {
    display: inline-block;
    font: normal normal normal 14px/1 FontAwesome;
    font-size: inherit;
    text-rendering: auto;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

/*
.fa {
  padding: 40px;
  font-size: 30px;
  width: 30px;
  text-align: center;
  text-decoration: none;
  margin: 15px;
  border-radius: 50%;
}
*/

a.fa {
  padding: 40px;
  font-size: 30px;
  width: 30px;
  text-align: center;
  text-decoration: none;
  margin: 15px;
  border-radius: 50%;
}

.fa:hover {
  opacity: 0.7;
}

.fa-facebook {
  background: #3B5998;
  color: white;
}

.fa-twitter {
  background: #55ACEE;
  color: white;
}

.fa-google {
  background: #dd4b39;
  color: white;
}

.fa-linkedin {
  background: #007bb5;
  color: white;
}

.fa-youtube {
  background: #bb0000;
  color: white;
}

.fa-instagram {
  background: #125688;
  color: white;
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">

<section id="contact">
  <a href="#" class="fa fa-facebook"></a>
  <a href="#" class="fa fa-twitter"></a>
  <a href="#" class="fa fa-google"></a>
  <a href="#" class="fa fa-linkedin"></a>
  <a href="#" class="fa fa-youtube"></a>
  <a href="#" class="fa fa-instagram"></a>
</section>

关于html - 将 CSS 应用于 anchor 的类与通过父元素应用于 anchor ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58406993/

上一篇:javascript - 如何在 VS Code 中使用 Libman json 文件?

下一篇:html - 如何更正药丸中的重叠文本

相关文章:

html - 选择的类不工作 - CSS

html - 如何修复移动的导航栏底部移动

css - 图像和 div 之间的奇数空间 (CSS)

javascript - 如何让一个盒子粘在上面?

javascript - 我可以突出显示选定的列表元素吗?

html - Bootstrap 背景图像网格

javascript - 无法在 Leaflet 1.0.2 和 1.1-dev 上设置分数缩放

html - 在 div 中垂直居中文本

css - 在 Bootstrap 网格中创建单边边框而不更改 <div> 的大小

html - 使一个 div 环绕另一个 div