【发布时间】:2020-12-31 01:12:29
【问题描述】:
我正在尝试创建一个 CSS 网格(或 flex),其中每个项目在悬停时展开,而其他项目被隐藏。我可以使用 :nthchild 让第一个项目以这种方式工作,但显然这个 nthchild 不适用于网格中的先前项目。
<div class="container">
<div id="a" class="box">Div A Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</div>
<div id="b" class="box">Div B</div>
<div id="c" class="box">Div C</div>
</div>
CSS
.container {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-gap: 20px;
margin: 20px;
}
.box {
background-color: grey;
padding: 20px;
}
#a:hover {
grid-column: 1 / 4;
}
#a:hover ~ :nth-child(-n+8) {
display: none;
}
#b:hover {
grid-column: 1 / 4;
}
#b:hover ~ :nth-child(-n+8) {
display: none;
}
这是 jsfiddle 上的代码: https://jsfiddle.net/ktz8qdcp/20/
感谢您的帮助!
【问题讨论】:
标签: javascript html css responsive-design css-grid