很棒的字体:使用hover :: before时,图标消失了

时间:2019-03-13 23:29:40

标签: css font-awesome pseudo-element

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

.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>

1 个答案:

答案 0 :(得分:1)

将鼠标悬停时将添加黑色背景。您无法休息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>