css - 使 ::after 元素的大小和位置与 flex 子元素相同
问题描述
我正在尝试按照这篇文章为我的 flex 元素添加一个有效的 box-shadow 。
问题是它不起作用,这是缩小的例子。
我试过设置position: relative
on nav
,但它只是使::after
元素成为整个导航栏的大小。
<nav>
<div class="nav-left">
<a href="#">
hello
</a>
</div>
<div class="nav-center"></div>
<div class="nav-right"></div>
</nav>
nav {
display: flex;
min-height: 5rem;
background-color: #402424;
align-items: stretch;
}
nav a,
nav .brand {
text-decoration: none;
align-items: center;
display: flex;
color: #AFAFAF;
}
nav a::after {
content: '';
position: absolute;
z-index: -1;
width: 100%;
height: 100%;
opacity: 0;
border-radius: 5px;
box-shadow: 0 5px 15px rgba(0,0,0,0.3);
transition: opacity 0.3s ease-in-out;
}
nav a:hover::after {
opacity: 1;
}
解决方案
试试这个,
添加到导航
nav a{position:relative;}
还添加到导航 a::after
nav a::after{
top:0;
right:0;
left:0;
bottom:0;
}
body {
background: #20262E;
padding: 0;
margin: 0;
font-family: Helvetica;
}
nav {
display: flex;
min-height: 5rem;
background-color: #402424;
align-items: stretch;
}
nav a,
nav .brand {
text-decoration: none;
align-items: center;
display: flex;
color: #AFAFAF;
position:relative;
}
nav a::after {
content: '';
position: absolute;
z-index: 1;
opacity: 0;
top:0;
right:0;
left:0;
bottom:0;
border-radius: 5px;
box-shadow: 0 5px 15px rgba(0,0,0,0.3);
transition: opacity 0.3s ease-in-out;
}
nav a:hover::after {
opacity: 1;
}
.nav-left,
.nav-center,
.nav-right {
display: flex;
flex: 1;
}
.nav-left {
justify-content: flex-start;
}
.nav-center {
justify-content: center;
}
.nav-right {
justify-content: flex-end;
}
<nav>
<div class="nav-left">
<a href="#">
hello
</a>
</div>
<div class="nav-center">
</div>
<div class="nav-right">
World
</div>
</nav>
推荐阅读
- mongodb - nestjs mongoose 类型“字符串”不可分配给类型“条件”
> - html - 测试时页脚中显示的正文链接,但代码中没有
- pointers - 为什么用 RBP 而不是另一个寄存器作为帧指针?
- arrays - 如何在 mongodb 中对深度嵌套的 JSON 执行更新查询?
- loops - 我不知道如何处理循环和 if 语句
- java - 如何将使用 testng 的放心测试更改为黄瓜?
- python - 如何更改 seaborn violinplot 图例标签?
- java - 如何使用 MouseEvent 定义按下哪个按钮
- ios - 将网格动画从搅拌机导出到 XCode
- android - 有没有办法使用导航图从一个片段滑动到另一个片段?