如何解决 css 中的嵌套选择器?
How to address nested selector in css?
table的padding应该是8px,但是当有modal的时候,padding应该是0,这样才能填满整个cell,见图
如何解决嵌套在 table 和不同种类的 class 和 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">×</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 的帮助下,我到目前为止已经完成了,但模态框下方仍然有一条细小的白线。
当我们点击模式打开时,让我们在单元格中添加一个modal-open
class。 modal-open
class 有 css 应用 0px 填充。当我们关闭模式时,删除 class 并因此删除填充。
#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 */
}
.btn-cell {
padding: 8px;
}
.btn-cell.modal-open {
padding: 0px!important;
}
/* 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;
}
</style>
</head>
<body>
<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 class='btn-cell'><!-- 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">×</span>
<p>Some text in the Modal..</p>
</div>
</div>
</td>
<td class='btn-cell'><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>
<script>
// 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";
togglePadding(true)
}
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal.style.display = "none";
togglePadding()
}
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
togglePadding()
}
}
var togglePadding = function(add) {
var buttonCells = document.getElementsByClassName('btn-cell')
for (var i = 0; i < buttonCells.length; i++) {
if (add) {
buttonCells[i].classList.add('modal-open')
}
else {
buttonCells[i].classList.remove('modal-open')
}
}
}
</script>
因为你在父 #tabletu td
上有 id 选择器,所以它克服了 class 选择器 .modal td
。您只需要使用 !important
来克服 id 选择器:
.modal td {padding:0} !important
您可以通过向包含 button
的所有 <td>
添加一个 class 来解决它,然后用 !important
设置样式。在这个例子中我使用了 modalTD
class:
.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;}
table的padding应该是8px,但是当有modal的时候,padding应该是0,这样才能填满整个cell,见图
如何解决嵌套在 table 和不同种类的 class 和 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">×</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 的帮助下,我到目前为止已经完成了,但模态框下方仍然有一条细小的白线。
当我们点击模式打开时,让我们在单元格中添加一个modal-open
class。 modal-open
class 有 css 应用 0px 填充。当我们关闭模式时,删除 class 并因此删除填充。
#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 */
}
.btn-cell {
padding: 8px;
}
.btn-cell.modal-open {
padding: 0px!important;
}
/* 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;
}
</style>
</head>
<body>
<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 class='btn-cell'><!-- 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">×</span>
<p>Some text in the Modal..</p>
</div>
</div>
</td>
<td class='btn-cell'><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>
<script>
// 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";
togglePadding(true)
}
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal.style.display = "none";
togglePadding()
}
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
togglePadding()
}
}
var togglePadding = function(add) {
var buttonCells = document.getElementsByClassName('btn-cell')
for (var i = 0; i < buttonCells.length; i++) {
if (add) {
buttonCells[i].classList.add('modal-open')
}
else {
buttonCells[i].classList.remove('modal-open')
}
}
}
</script>
因为你在父 #tabletu td
上有 id 选择器,所以它克服了 class 选择器 .modal td
。您只需要使用 !important
来克服 id 选择器:
.modal td {padding:0} !important
您可以通过向包含 button
的所有 <td>
添加一个 class 来解决它,然后用 !important
设置样式。在这个例子中我使用了 modalTD
class:
.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;}