gpt4 book ai didi

html - 如何解决CSS中的嵌套选择器?

转载 作者:行者123 更新时间:2023-11-28 13:59:09 24 4
gpt4 key购买 nike

表格的padding应该是8px,但是当有modal的时候,padding应该是0,这样才能填满整个单元格,见图enter image description here

如何解决嵌套在表中以及不同类别和 ID 下的特定选择器?

我尝试过组合:#tabletu .modal #myBtn2 td 但都没有使填充消失。例如:

// Get the modal
var modal = document.getElementById("myModal");

// Get the button that opens the modal
var btn = document.getElementById("myBtn2");

// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];

// When the user clicks the button, open the modal
btn.onclick = function() {
modal.style.display = "block";
}

// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal.style.display = "none";
}

// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
#tabletu .modal {
padding: 0px;
}

#tabletu {
background-color: transparent;
border-collapse: collapse;
width: 100%;
}

#tabletu td,
th {
border: 1px solid #000000;
padding: 8px;
}

#tabletu th {
padding: 8px;
text-align: left;
border: 1px solid #000000;
background-color: rgba(0, 110, 167, 1);
color: white;
}


/* The Modal (background) */

.modal {
display: none;
/* Hidden by default */
position: fixed;
/* Stay in place */
z-index: 1;
/* Sit on top */
padding-top: 100px;
/* Location of the box */
left: 0;
top: 0;
width: 100%;
/* Full width */
height: 100%;
/* Full height */
overflow: auto;
/* Enable scroll if needed */
background-color: rgb(0, 0, 0);
/* Fallback color */
background-color: rgba(0, 0, 0, 0.4);
/* Black w/ opacity */
}


/* Modal Content */

.modal-content {
background-color: #fefefe;
margin: auto;
padding: 20px;
border: 1px solid #888;
width: 80%;
}

.modal td {
padding: 0px;
}


/* The Close Button */

.close {
color: #aaaaaa;
float: right;
font-size: 28px;
font-weight: bold;
}

.close:hover,
.close:focus {
color: #000;
text-decoration: none;
cursor: pointer;
}

.notebook {
border-collapse: collapse;
width: 70%;
}

.notebook td:hover {
background-color: #ddd;
}

.notebook td,
.notebook th {
border: 1px solid #ddd;
padding: 0px;
}

.block {
display: block;
width: 100%;
border: none;
background-color: rgba(0, 166, 214, 1);
padding: 14px 28px;
font-size: 16px;
cursor: pointer;
text-align: center;
box-sizing: border-box;
}

.block:hover {
background-color: rgba(0, 166, 214, 0.78);
color: black;
}

.month {
text-align: center;
}

#category {
padding: 8px;
}
<h2>Experiment Notebook</h2>
<center>
<table id="tabletu" class="notebook">
<tr id="category">
<th></th>
<th id="category">Test1</th>
<th>Test2</th>
<th>Other</th>
</tr>
<tr>
<td rowspan=2 class="month">April</td>
<td>
<!-- Trigger/Open The Modal -->
<button id="myBtn2" class="block">Open Modal</button>

<!-- The Modal -->
<div id="myModal" class="modal">

<!-- Modal content -->
<div class="modal-content">
<span class="close">&times;</span>
<p>Some text in the Modal..</p>
</div>

</div>
</td>
<td><button id="myBtn2" class="block">Open Modal</button></td>
<td>Germany</td>
</tr>
<tr>

<td>Berglunds snabbköp</td>
<td>Christina Berglund</td>
<td>Sweden</td>
</tr>
<tr>
<td></td>
<td>Centro comercial Moctezuma</td>
<td>Francisco Chang</td>
<td>Mexico</td>
</tr>
<tr>
<td></td>
<td>Ernst Handel</td>
<td>Roland Mendel</td>
<td>Austria</td>
</tr>
<tr>
<td></td>
<td>Island Trading</td>
<td>Helen Bennett</td>
<td>UK</td>
</tr>
<tr>
<td></td>
<td>Königlich Essen</td>
<td>Philip Cramer</td>
<td>Germany</td>
</tr>
<tr>
<td></td>
<td>Laughing Bacchus Winecellars</td>
<td>Yoshi Tannamuri</td>
<td>Canada</td>
</tr>
<tr>
<td></td>
<td>Magazzini Alimentari Riuniti</td>
<td>Giovanni Rovelli</td>
<td>Italy</td>
</tr>
<tr>
<td></td>
<td>North/South</td>
<td>Simon Crowther</td>
<td>UK</td>
</tr>
<tr>
<td></td>
<td>Paris spécialités</td>
<td>Marie Bertrand</td>
<td>France</td>
</tr>
</table>

</center>

在 A. Meshu 的帮助下,我到目前为止已经完成了,但模态框下方仍然有一条细小的白线。 enter image description here

最佳答案

你可以通过给所有的 <td> 添加一个类来解决它拥有 button然后用 !important 设置样式.在这个例子中我使用了 modalTD类:

.modalTD {padding: 0!important;} // remove horizontal padding
.modalTD button {height: 65px!important;} // make the button height wrap the entire cell

html 看起来像这样:

.
.
.
<td class="modalTD"><!-- Trigger/Open The Modal -->
<button id="myBtn2" class="block">Open Modal</button>
.
.
.

旁注:有一天 :has pseudo-class将使我们的生活更轻松,但目前它不支持任何......

编辑

另一种解决方案会更有效率,但它会影响整行(我必须说视觉上不是那么糟糕),您可以尝试的是:

#tabletu tr:nth-of-type(2) td {padding: 0;}

关于html - 如何解决CSS中的嵌套选择器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58018310/

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