我尝试制作类似地铁的菜单,当用户将鼠标悬停在每个磁贴上时,我希望它显示一个盒子阴影。现在我开始工作了,但我读到最好在::after
伪元素上使用它更加平滑,但它似乎不起作用。这是jsFiddle。
p.tile {
text-align: center;
padding: 5rem;
background: #58588E;
color: white;
margin: 2rem;
box-sizing: border-box;
}
p.tile::after {
content: "";
box-shadow: 3px 5px 20px -1px rgba(0, 0, 0, 0.8);
opacity: 0;
transition: opacity 0.3s ease-in-out;
}
p.tile:hover::after {
opacity: 1;
}

<a href="#" class="tile-link">
<p class="tile">My Progress</p>
</a>
&#13;
答案 0 :(得分:5)
问题是::after
伪元素实际上没有任何宽度或高度。您可以在容器上设置position:relative
,并设置position:absolute
+ left:0
,right:0
,top:0
,bottom:0
技巧,使其大小相同作为容器。
p.tile {
text-align: center;
padding: 5rem;
background: #58588E;
color: white;
margin: 2rem;
box-sizing: border-box;
/* new */
position: relative;
}
p.tile::after {
content: "";
box-shadow: 3px 5px 20px -1px rgba(0, 0, 0, 0.8);
opacity: 0;
transition: opacity 0.3s ease-in-out;
/* new */
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
}
p.tile:hover::after {
opacity: 1;
}
<a href="#" class="tile-link">
<p class="tile">My Progress</p>
</a>
答案 1 :(得分:0)
您已经创建了伪元素但是没有给它任何高度或宽度。首先,你必须给它显示。
p.tile {
text-align: center;
padding: 5rem;
background: #58588E;
color: white;
margin: 2rem;
box-sizing: border-box;
position: relative;
}
p.tile::after {
content: "";
display: block;
box-shadow: 3px 5px 20px -1px rgba(0,0,0,0.8);
opacity: 0;
transition: opacity 0.3s ease-in-out;
}
p.tile:hover::after {
opacity: 1;
}