单击响应式导航按钮时文本未对齐

时间:2016-10-13 09:41:27

标签: javascript html css

我只是测试W3Schools提供的响应式顶部导航示例,我对内部样式表进行了一些小修改,因此文本Home将移动到菜单的中心,当我点击时它未对齐在导航按钮上。

为什么会发生这种情况以及如何解决这个问题?

<!DOCTYPE html>
<html>

<head>
  <style>
    body {
      margin: 0;
    }
    ul.topnav {
      text-align: center;
      list-style-type: none;
      margin: 0;
      padding: 0;
      overflow: hidden;
      background-color: #333;
    }
    ul.topnav li {
      display: inline-block;
      text-align: center;
    }
    ul.topnav li a {
      display: inline-block;
      color: #f2f2f2;
      text-align: center;
      text-decoration: none;
      transition: 0.3s;
      padding: 14px 16px;
      font-size: 17px;
    }
    ul.topnav li a:hover {
      background-color: #555;
    }
    ul.topnav li.icon {
      display: none;
    }
    @media screen and (max-width: 680px) {
      ul.topnav li:not(:first-child) {
        display: none;
      }
      ul.topnav li.icon {
        float: right;
        display: inline-block;
      }
    }
    @media screen and (max-width: 680px) {
      ul.topnav.responsive {
        position: relative;
      }
      ul.topnav.responsive li.icon {
        position: absolute;
        right: 0;
        top: 0;
      }
      ul.topnav.responsive li {
        float: none;
        display: inline;
      }
      ul.topnav.responsive li a {
        display: block;
        text-align: center;
      }
    }
  </style>
</head>

<body>

  <ul class="topnav" id="myTopnav">
    <li><a class="active" href="#home">Home</a>
    </li>
    <li><a href="#news">News</a>
    </li>
    <li><a href="#contact">Contact</a>
    </li>
    <li><a href="#about">About</a>
    </li>
    <li class="icon">
      <a href="javascript:void(0);" style="font-size:15px;" onclick="myFunction()">☰</a>
    </li>
  </ul>

  <div style="padding-left:16px">
    <h2>Responsive Topnav Example</h2>
    <p>Resize the browser window to see how it works.</p>
  </div>

  <script>
    function myFunction() {
      var x = document.getElementById("myTopnav");
      if (x.className === "topnav") {
        x.className += " responsive";
      } else {
        x.className = "topnav";
      }
    }
  </script>

</body>

</html>

2 个答案:

答案 0 :(得分:1)

就像我在评论中所说,问题是因为li.iconposition: relativeposition: absolute之间切换

您必须将其修改为position:absolute

&#13;
&#13;
<!DOCTYPE html>
<html>

<head>
  <style>
    body {
      margin: 0;
    }
    ul.topnav {
      text-align: center;
      list-style-type: none;
      margin: 0;
      padding: 0;
      overflow: hidden;
      background-color: #333;
    }
    ul.topnav li {
      display: inline-block;
      text-align: center;
    }
    ul.topnav li a {
      display: inline-block;
      color: #f2f2f2;
      text-align: center;
      text-decoration: none;
      transition: 0.3s;
      padding: 14px 16px;
      font-size: 17px;
    }
    ul.topnav li a:hover {
      background-color: #555;
    }
    ul.topnav li.icon {
      display: none;
    }
    @media screen and (max-width: 680px) {
      ul.topnav li:not(:first-child) {
        display: none;
      }
      ul.topnav li.icon {
        display: inline-block;
        position: absolute;
        right: 0;
      }
    }
    @media screen and (max-width: 680px) {
      ul.topnav.responsive {
        position: relative;
      }
      ul.topnav.responsive li.icon {
        position: absolute;
        right: 0;
        top: 0;
      }
      ul.topnav.responsive li {
        float: none;
        display: inline;
      }
      ul.topnav.responsive li a {
        display: block;
        text-align: center;
      }
    }
  </style>
</head>

<body>

  <ul class="topnav" id="myTopnav">
    <li><a class="active" href="#home">Home</a>
    </li>
    <li><a href="#news">News</a>
    </li>
    <li><a href="#contact">Contact</a>
    </li>
    <li><a href="#about">About</a>
    </li>
    <li class="icon">
      <a href="javascript:void(0);" style="font-size:15px;" onclick="myFunction()">☰</a>
    </li>
  </ul>

  <div style="padding-left:16px">
    <h2>Responsive Topnav Example</h2>
    <p>Resize the browser window to see how it works.</p>
  </div>

  <script>
    function myFunction() {
      var x = document.getElementById("myTopnav");
      if (x.className === "topnav") {
        x.className += " responsive";
      } else {
        x.className = "topnav";
      }
    }
  </script>

</body>

</html>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

你的ul.topnav li.icon在活动时浮动。

<!DOCTYPE html>
<html>

<head>
  <style>
    body {
      margin: 0;
    }
    ul.topnav {
      text-align: center;
      list-style-type: none;
      margin: 0;
      padding: 0;
      overflow: hidden;
      background-color: #333;
    }
    ul.topnav li {
      display: inline-block;
      text-align: center;
    }
    ul.topnav li a {
      display: inline-block;
      color: #f2f2f2;
      text-align: center;
      text-decoration: none;
      transition: 0.3s;
      padding: 14px 16px;
      font-size: 17px;
    }
    ul.topnav li a:hover {
      background-color: #555;
    }
    ul.topnav li.icon {
      display: none;
    }
    @media screen and (max-width: 680px) {
      ul.topnav li:not(:first-child) {
        display: none;
      }
      ul.topnav li.icon {
        position: absolute;
        top: 0;
        right: 0;
        display: inline-block;
      }
    }
    @media screen and (max-width: 680px) {
      ul.topnav.responsive {
        position: relative;
      }

      ul.topnav.responsive li {
        float: none;
        display: inline;
      }
      ul.topnav.responsive li a {
        display: block;
        text-align: center;
      }
    }
  </style>
</head>

<body>

  <ul class="topnav" id="myTopnav">
    <li><a class="active" href="#home">Home</a>
    </li>
    <li><a href="#news">News</a>
    </li>
    <li><a href="#contact">Contact</a>
    </li>
    <li><a href="#about">About</a>
    </li>
    <li class="icon">
      <a href="javascript:void(0);" style="font-size:15px;" onclick="myFunction()">☰</a>
    </li>
  </ul>

  <div style="padding-left:16px">
    <h2>Responsive Topnav Example</h2>
    <p>Resize the browser window to see how it works.</p>
  </div>

<script>
  function myFunction() {
      var x = document.getElementById("myTopnav");
      if (x.className === "topnav") {
        x.className += " responsive";
      } else {
        x.className = "topnav";
      }
    }
</script>

</body>


</html>

http://codepen.io/twotoan/pen/zKaBzK

这是你想要实现的目标吗?