首页 > input[type=checkbox]:checked + table {display:block;} ?

input[type=checkbox]:checked + table {display:block;} ?

<!DOCTYPE HTML>
<html lang="en-US">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Demo</title>
    <style type="text/css">
    *{margin:0;padding:0;box-sizing:border-box;}
    .dn{
        display:none;
    }
    input[type=checkbox]{
        visibility:hidden;
        position:absolute;
    }
    input[type=checkbox]:checked+table{
        display:block;
    }
    table{
        border:1px solid red;
    }
    </style>
</head>
<body>
    <label for="swtich">swtich</label>
    <table>
        <tr>
            <td>1111111111</td>
            <td>222222222</td>
            <td>333333333</td>
            <td>444444444</td>
            <td>555555555</td>
        </tr>
    </table>
    <input type="checkbox" id='swtich'/>
    <table class='dn'>
        <tr>
            <td>1111111111</td>
            <td>222222222</td>
            <td>333333333</td>
            <td>444444444</td>
            <td>555555555</td>
        </tr>
    </table>
</body>
</html>

下面那个table的宽是什么回事啊?


display: block; 默认就是占满父元素的宽度。

我理解这代码是像通过 display: block 让原本 display: none<table> 显示。可惜 <table> 的默认 display 不是 block 而是 table

所以你可以改成

input[type=checkbox]:checked+table{
    display: table;
}

事实上像 <div> 默认 displayblock<span> 默认是 inline 外,不同元素的默认 display 都会不同。所以还有个简单的办法是 display: initial 直接使用默认的 display。不过浏览器兼容性不好。

【热门文章】
【热门文章】