gpt4 book ai didi

html - 如何使用 CSS 网格布局在行表上制作悬停状态

转载 作者:技术小花猫 更新时间:2023-10-29 12:14:26 25 4
gpt4 key购买 nike

这是一个使用 CSS 网格布局创建的表格,但我有一个问题,我无法在每一行上设置悬停状态。

我只想为此使用 CSS。

谁能给我一个解决方案?

.table {
display: grid;
grid-template-columns: [col-start] auto [col-end];
grid-template-rows: [header-start] 50px [header-end row-start] auto [row-end];
grid-auto-rows: auto;
grid-auto-columns: auto;
grid-gap: 1px;
}

.table>* {
background: gray;
padding: 10px;
}

.heading {
background: navy;
color: #fff;
grid-row: header;
}
<div class="table">
<div class="heading">Title 1</div>
<div class="heading">Title 2</div>
<div class="heading">Title 3</div>
<div class="heading">Title 4</div>
<div class="heading">Title 5</div>

<div class="row">Row 1</div>
<div class="row">Row 1</div>
<div class="row">Row 1</div>
<div class="row">Row 1</div>
<div class="row">Row 1</div>

<div class="row">Row 2</div>
<div class="row">Row 2</div>
<div class="row">Row 2</div>
<div class="row">Row 2</div>
<div class="row">Row 2</div>

<div class="row">Row 3</div>
<div class="row">Row 3</div>
<div class="row">Row 3</div>
<div class="row">Row 3</div>
<div class="row">Row 3</div>
</div>

最佳答案

由于您将其视为一个表格,其中的元素位于同一行中,因此您还可以将每一行包装在一个 DIV 中,并将“显示”设置为“内容”。这形成了一个无害的父元素,您可以将其用于悬停,然后设置子元素的样式。 (不过,Edge 尚不支持显示:内容。)

.table {
display: grid;
grid-template-columns: [col-start] auto [col-end];
grid-template-rows: [header-start] 50px [header-end row-start] auto [row-end];
grid-auto-rows: auto;
grid-auto-columns: auto;
grid-gap: 1px;
overflow: hidden;
background: gray;
}

.table .row, .table .heading{
padding: 10px;
position: relative;
}

.heading {
background: navy;
color: #fff;
grid-row: header;
}

.row span {
position: relative;
z-index: 2;
}

.rowWrapper{
display: contents;
}

.rowWrapper:hover > div{
background-color: orange;
}
<div class="table">

<div class="heading">Title 1</div>
<div class="heading">Title 2</div>
<div class="heading">Title 3</div>
<div class="heading">Title 4</div>
<div class="heading">Title 5</div>

<div class="rowWrapper">
<div class="row"><span>Row 1</span></div>
<div class="row"><span>Row 1</span></div>
<div class="row"><span>Row 1</span></div>
<div class="row"><span>Row 1</span></div>
<div class="row"><span>Row 1</span></div>
</div>

<div class="rowWrapper">
<div class="row"><span>Row 2</span></div>
<div class="row"><span>Row 2</span></div>
<div class="row"><span>Row 2</span></div>
<div class="row"><span>Row 2</span></div>
<div class="row"><span>Row 2</span></div>
</div>

<div class="rowWrapper">
<div class="row"><span>Row 3</span></div>
<div class="row"><span>Row 3</span></div>
<div class="row"><span>Row 3</span></div>
<div class="row"><span>Row 3</span></div>
<div class="row"><span>Row 3</span></div>
</div>

</div>

关于html - 如何使用 CSS 网格布局在行表上制作悬停状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48108879/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com