html - 如何让图标和文字在悬停时同时改变颜色?
问题描述
我有divs
每个包含一个<i>
和一个<p>
。悬停时,我希望两者同时改变颜色。检查论坛上的一些答案,我设法,但是在悬停时,另一个divs
全部变成白色。(我正在使用 Bootstrap 4)。
.category-box {
background-color: white;
border-radius: 10px;
box-shadow: 0px 3px 11px rgba(170, 192, 205, 0.49);
padding: 20px 0;
margin-bottom: 20px;
}
.category-box p {
color: #1F2833;
font-size: 1.1em;
}
.category-box:hover {
background-color: #0074e1;
transition: color .15s ease-in-out, background-color .15s ease-in-out, border-color .15s ease-in-out, box-shadow .15s ease-in-out;
color: white;
}
.category-box-container i {
color: #0074e1;
}
.category-box-container:hover p,
.category-box-container:hover i {
color: white;
text-decoration: none;
}
.category-box i {
font-size: 1.7em;
margin-bottom: 10px;
}
<!-- Bootstrap CDN -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
<!-- Fontawsome CDN -->
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.3.1/css/all.css">
<div class="col-lg-9 col-md-12 col-sm-12 col-12 mx-auto category-box-container">
<div class="row">
<!-- Sobre Paygol -->
<a href="#" class="col-lg-4 col-md-6 col-sm-12 col-12 text-center">
<div class="col category-box">
<i class="fas fa-user-tie"></i>
<p>Sobre Paygol</p>
</div>
</a>
<!-- Vendedores -->
<a href="#" class="col-lg-4 col-md-6 col-sm-12 col-12 text-center">
<div class="col category-box">
<i class="fas fa-hand-holding-usd"></i>
<p>Vendedores</p>
</div>
</a>
<!-- Compradores -->
<a href="#" class="col-lg-4 col-md-6 col-sm-12 col-12 text-center">
<div class="col category-box">
<i class="fas fa-shopping-cart"></i>
<p>Compradores</p>
</div>
</a>
<!-- Países -->
<a href="#" class="col-lg-4 col-md-6 col-sm-12 col-12 text-center">
<div class="col category-box">
<i class="fas fa-globe-americas"></i>
<p>Países</p>
</div>
</a>
<!-- Desarrolladores -->
<a href="#" class="col-lg-4 col-md-6 col-sm-12 col-12 text-center">
<div class="col category-box">
<i class="fas fa-laptop-code"></i>
<p>Desarrolladores</p>
</div>
</a>
<!-- Términos -->
<a href="#" class="col-lg-4 col-md-6 col-sm-12 col-12 text-center">
<div class="col category-box">
<i class="fas fa-file-alt"></i>
<p>Términos</p>
</div>
</a>
<!-- /Términos -->
</div>
</div>
解决方案
您需要删除:hover
影响整个容器/父级的。注释中的代码更改。
.category-box {
background-color: white;
border-radius: 10px;
box-shadow: 0px 3px 11px rgba(170, 192, 205, 0.49);
padding: 20px 0;
margin-bottom: 20px;
}
.category-box p {
color: #1F2833;
font-size: 1.1em;
}
.category-box:hover {
background-color: #0074e1;
transition: color .15s ease-in-out, background-color .15s ease-in-out, border-color .15s ease-in-out, box-shadow .15s ease-in-out;
color: white;
}
.category-box-container i {
color: #0074e1;
}
.category-box:hover p,
.category-box:hover i { /* Renamed from `.category-box-container` */
color: white;
text-decoration: none;
}
.category-box i {
font-size: 1.7em;
margin-bottom: 10px;
}
<!-- Bootstrap CDN -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
<!-- Fontawsome CDN -->
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.3.1/css/all.css">
<div class="col-lg-9 col-md-12 col-sm-12 col-12 mx-auto category-box-container">
<div class="row">
<!-- Sobre Paygol -->
<a href="#" class="col-lg-4 col-md-6 col-sm-12 col-12 text-center">
<div class="col category-box">
<i class="fas fa-user-tie"></i>
<p>Sobre Paygol</p>
</div>
</a>
<!-- Vendedores -->
<a href="#" class="col-lg-4 col-md-6 col-sm-12 col-12 text-center">
<div class="col category-box">
<i class="fas fa-hand-holding-usd"></i>
<p>Vendedores</p>
</div>
</a>
<!-- Compradores -->
<a href="#" class="col-lg-4 col-md-6 col-sm-12 col-12 text-center">
<div class="col category-box">
<i class="fas fa-shopping-cart"></i>
<p>Compradores</p>
</div>
</a>
<!-- Países -->
<a href="#" class="col-lg-4 col-md-6 col-sm-12 col-12 text-center">
<div class="col category-box">
<i class="fas fa-globe-americas"></i>
<p>Países</p>
</div>
</a>
<!-- Desarrolladores -->
<a href="#" class="col-lg-4 col-md-6 col-sm-12 col-12 text-center">
<div class="col category-box">
<i class="fas fa-laptop-code"></i>
<p>Desarrolladores</p>
</div>
</a>
<!-- Términos -->
<a href="#" class="col-lg-4 col-md-6 col-sm-12 col-12 text-center">
<div class="col category-box">
<i class="fas fa-file-alt"></i>
<p>Términos</p>
</div>
</a>
<!-- /Términos -->
</div>
</div>
推荐阅读
- css - CSS 属性“外观”/“-moz-appearance”/“-webkit-appearance”不起作用
- tableau-api - 用于显示特定行的 Tableau 计算字段
- c# - 在控制台应用程序中将 Serilog 与 ApplicationInsights 一起使用时,从不调用 ITelemetryInitializer.Initialize
- javascript - javascript 的效果不支持单独的行
- c++ - 如何修复我的 topological.cpp 输出错误?
- javascript - 如何正确检查指针向下后的事件更改?
- python - 将插件添加到 Sublime Text 3
- python - 如何在 Django 模型中保存()参数
- c++ - 如何在 sublime text 3 和 Visual Studio 2019 社区中使用 c++ 绑定设置 raylib
- php - PHP - 发送电子邮件后重定向到页面