首页 > 解决方案 > 字体真棒:使用 hover::before 时,图标消失了

问题描述

我想悬停图标,以便它显示before元素,但是每次我悬停它时,图标都会消失,当我悬停图标的右半部分时,什么也没发生。如果我更改beforeafter,则效果很好。

.container {
    background-color: lightgray;
    margin: auto;
    width: 500px;
    height: 200px;
    padding: 100px;
}

.icon {
    font-size: 50px;
    cursor: pointer;
    color: red;
}

.icon:hover::before {
    content: '';
    background-color: black;
    padding: 10px;
}
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous">


<div class="container">
		<i class="fas fa-exclamation-circle icon"></i>
</div>

标签: cssfont-awesomepseudo-element

解决方案


这将在悬停时添加黑色背景。你不能休息,content因为它用于显示 FontAwesome 的图标。

.container {
    background-color: lightgray;
    margin: auto;
    width: 500px;
    height: 200px;
    padding: 100px;
}

.icon {
    font-size: 50px;
    cursor: pointer;
    color: red;
}

.icon:hover::before {
    background-color: black;
    padding: 10px;
}
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous">


<div class="container">
		<i class="fas fa-exclamation-circle icon"></i>
</div>


推荐阅读