代码之家  ›  专栏  ›  技术社区  ›  Ramazan Chasygov

为什么宽度:0不起作用

  •  1
  • Ramazan Chasygov  · 技术社区  · 9 年前

    在里面 @media screen and (max-width: 700px) ,我设置 aside {width: 0} width: 52px ,为什么会发生这种情况,我该如何解决?

    * {
      margin:0;
      padding:0;
    }
    
    html, body {
      height: 100%;
    }
    
    body {
      background-color: #eee;
    }
    
    .wrapper {
      height: 100%;
      display: table;
      width: 100%;
      text-align: center;
      border-collapse: collapse;
    }
    
    header {
      box-sizing: border-box;
      display: table-row;
      height: 50px;
      background-color: #eee;
      border-bottom: 1px solid black;
    }
    
    main {
      height: 100%;
      display: table;
      width: 800px;
      margin: 0 auto;
      background-color: #fff;
    }
    
    section {
      display: table-cell;
    }
    
    aside {
      display: table-cell;
      box-sizing: border-box;
      border-left: 1px solid black;
      width: 300px;
    
      /*__transition*/
      opacity: 1;
      transition: all .25s;
    }
    
    footer {
      box-sizing: border-box;
      border-top: 1px solid black;
      display: table-row;
      height: 50px;
      background-color: #eee;
    }
    
    @media screen and (max-width: 800px) {
    
      main {
        width: 100%;
      }
    }
    
    @media screen and (max-width: 700px) {
    
      section {
        width: 100%;
      }
    
      aside {
        opacity: 0;
        width: 0;
      }
    }
    <html>
      <body>
        <div class="wrapper">
          <header>HEADER</header>
          <main>
            <section>SECTION</section>
            <aside>ASIDE</aside>
          </main>
          <footer>FOOTER</footer>
        </div>
      </body>
    </html>
    3 回复  |  直到 9 年前
        1
  •  2
  •   max    9 年前

    display: none

    @media screen and (max-width: 700px) {
      aside {
        opacity: 0;
        width: 0;
        border: none;
        font-size: 0;
      }
    }
    
        2
  •  2
  •   yqlim    9 年前

    因为它具有 display: table-cell

    你只需添加 display: block 在当前代码中。

    或者,将代码替换为单个 display: none 会成功的。

    或者,用一种相当奇怪的风格,见下文。

    * {
      margin:0;
      padding:0;
    }
    
    html, body {
      height: 100%;
    }
    
    body {
      background-color: #eee;
    }
    
    .wrapper {
      height: 100%;
      display: table;
      width: 100%;
      text-align: center;
      border-collapse: collapse;
    }
    
    header {
      box-sizing: border-box;
      display: table-row;
      height: 50px;
      background-color: #eee;
      border-bottom: 1px solid black;
    }
    
    main {
      height: 100%;
      display: table;
      width: 800px;
      margin: 0 auto;
      background-color: #fff;
    }
    
    section {
      display: table-cell;
    }
    
    aside {
      display: table-cell;
      box-sizing: border-box;
      border-left: 1px solid black;
      width: 300px;
    
      /*__transition*/
      opacity: 1;
      transition: all .25s;
    }
    
    footer {
      box-sizing: border-box;
      border-top: 1px solid black;
      display: table-row;
      height: 50px;
      background-color: #eee;
    }
    
    @media screen and (max-width: 800px) {
    
      main {
        width: 100%;
      }
    }
    
    @media screen and (max-width: 700px) {
    
      section {
        width: 100%;
      }
    
      aside {    
        display: none;
        
        /* OR */
        
        display: table-cell;
        border: none;
        opacity: 0;
        width: 0;
        font-size: 0;
        
        /* OR */
        
        display: block;
        opacity: 0;
        width: 0;
      }
    }
    <html>
      <body>
        <div class="wrapper">
          <header>HEADER</header>
          <main>
            <section>SECTION</section>
            <aside>ASIDE</aside>
          </main>
          <footer>FOOTER</footer>
        </div>
      </body>
    </html>
        3
  •  1
  •   João Mosmann    9 年前

    因为你在使用 display: table 制作布局。

    表格有自己的宽度计算方法,所以设置宽度并不意味着它就是这样。

    display: none 相反