代码之家  ›  专栏  ›  技术社区  ›  elias

旋转文本覆盖粘性导航栏

  •  1
  • elias  · 技术社区  · 7 年前

    我正在建立一个网站,它有一个粘性导航栏和一个表格。

    以下是my example.html:

    <!DOCTYPE html>
    <html lang="en">
        <head><meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
            <link rel="stylesheet" type="text/css" href="style.css">
            <title>Funny overlay example</title>
        </head>
        <body>
            <nav class="navbar">
                <div>
                    Sticky Navbar
                </div>
            </nav>
            <section>
                <div>
                    <table>
                        <tr><td rowspan="8" class="cell_vert_text"><div class="rotwrap"><div class="textcon">Problematic Text</div></div></td><td class="cell_horiz_text">Ok Text</td></tr>
                        <tr><td class="cell_horiz_text">Ok Text</td></tr>
                        <tr><td class="cell_horiz_text">Ok Text</td></tr>
                        <tr><td class="cell_horiz_text">Ok Text</td></tr>
                        <tr><td class="cell_horiz_text">Ok Text</td></tr>
                        <tr><td class="cell_horiz_text">Ok Text</td></tr>
                        <tr><td class="cell_horiz_text">Ok Text</td></tr>
                        <tr><td class="cell_horiz_text">Ok Text</td></tr>
                    </table>
                </div>
            </section>
        </body>
    </html>
    

    和style.css:

    * {
        margin: 0;
        padding: 0;
        box-sizing: border-box;
    }
    
    .navbar {
        background: #ff0000;
        position: sticky;
        top: 0;
        width: 100%;
    }
    
    table td.cell_vert_text {
        text-align: center;
        white-space: nowrap;
        vertical-align: middle;
        width: 1.5em;
    }
    
    table td.cell_vert_text div.rotwrap div.textcon {
        transform: rotate(270deg);
        margin-left: -10em;
        margin-right: -10em;
    }
    

    我怀疑旋转对DOM结构有一些副作用,但我真的不知道发生了什么。我试图通过引入z索引属性来解决这个问题,但没有任何帮助。 我不仅在寻找一种将旋转文本推到导航栏后面的解决方案,而且要清楚地解释到底发生了什么。非常感谢。

    2 回复  |  直到 7 年前
        1
  •  2
  •   Ori Drori    7 年前

    元素上的变换创建 stacking context ,因为它的父对象都不是堆栈上下文,所以它与另一个堆栈上下文navbar(位置粘性)处于同一级别。

    z-index

    解决方案:在导航栏上设置z索引:

    .navbar {
      z-index: 1;
      background: #ff0000;
      position: sticky;
      top: 0;
      width: 100%;
    }
    

    演示

    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }
    
    .container {
      height: 150px;
      overflow: auto;
    }
    
    .navbar {
      z-index: 1;
      background: #ff0000;
      position: sticky;
      top: 0;
      width: 100%;
    }
    
    table td.cell_vert_text {
      text-align: center;
      white-space: nowrap;
      vertical-align: middle;
      width: 1.5em;
    }
    
    table td.cell_vert_text div.rotwrap div.textcon {
      transform: rotate(270deg);
      margin-left: -10em;
      margin-right: -10em;
    }
    <div class="container">
      <nav class="navbar">
        <div>
          Sticky Navbar
        </div>
      </nav>
      <section>
        <div>
          <table>
            <tr>
              <td rowspan="8" class="cell_vert_text">
                <div class="rotwrap">
                  <div class="textcon">Problematic Text</div>
                </div>
              </td>
              <td class="cell_horiz_text">Ok Text</td>
            </tr>
            <tr>
              <td class="cell_horiz_text">Ok Text</td>
            </tr>
            <tr>
              <td class="cell_horiz_text">Ok Text</td>
            </tr>
            <tr>
              <td class="cell_horiz_text">Ok Text</td>
            </tr>
            <tr>
              <td class="cell_horiz_text">Ok Text</td>
            </tr>
            <tr>
              <td class="cell_horiz_text">Ok Text</td>
            </tr>
            <tr>
              <td class="cell_horiz_text">Ok Text</td>
            </tr>
            <tr>
              <td class="cell_horiz_text">Ok Text</td>
            </tr>
          </table>
        </div>
      </section>
    </div>
        2
  •  2
  •   Joe B.    7 年前

    * {
        margin: 0;
        padding: 0;
        box-sizing: border-box;
    }
    
    body{
      height: 5000px;
    }
    .navbar {
        background: #ff0000;
        position: sticky;
        top: 0;
        width: 100%;
      z-index: 1;
    }
    
    table td.cell_vert_text {
        text-align: center;
        white-space: nowrap;
        vertical-align: middle;
        width: 1.5em;
    }
    
    table td.cell_vert_text div.rotwrap div.textcon {
        transform: rotate(270deg);
        margin-left: -10em;
        margin-right: -10em;
    }
    <body>
      <nav class="navbar">
        <div>
          Sticky Navbar
        </div>
      </nav>
      <section>
        <div>
          <table>
            <tr><td rowspan="8" class="cell_vert_text"><div class="rotwrap"><div class="textcon">Problematic Text</div></div></td><td class="cell_horiz_text">Ok Text</td></tr>
            <tr><td class="cell_horiz_text">Ok Text</td></tr>
            <tr><td class="cell_horiz_text">Ok Text</td></tr>
            <tr><td class="cell_horiz_text">Ok Text</td></tr>
            <tr><td class="cell_horiz_text">Ok Text</td></tr>
            <tr><td class="cell_horiz_text">Ok Text</td></tr>
            <tr><td class="cell_horiz_text">Ok Text</td></tr>
            <tr><td class="cell_horiz_text">Ok Text</td></tr>
          </table>
        </div>
      </section>
    </body>

    将导航栏规则设为正 z-index

    .navbar {
     background: #ff0000;
     position: sticky;
     top: 0;
     width: 100%;
     z-index: 1
    }