【问题标题】:Items not below and center underneath the nav bar - Responsive Top Navigation不在导航栏下方和居中的项目 - 响应式顶部导航
【发布时间】:2019-06-21 01:14:48
【问题描述】:

因此,从我上次的论坛跟进,我能够让我的响应式菜单导航栏正常工作。以及我想如何使用很多人推荐的 flex 将我的两个项目居中。

我更新了我的导航栏,因为它在菜单栏中没有标题,所以没问题。现在发生的事情是,当我打开它时,它会弹出一个下拉菜单,但我的项目不在导航下方的中心。为什么它会像“浮动”到页面右侧无法看到和切断(尤其是在 iPhone 模式下查看时)。看看我下面的截图和我的代码,看看有什么困扰着我。

当汉堡图标未被点击时

点击汉堡图标时

这是我的代码。运行代码 sn-p 或更好,将其复制并粘贴到您的文本编辑器中,然后从浏览器运行它,这样您就可以看到我在说什么。 注意:当您运行代码 sn-p 时,它会执行我想要执行的操作,但来自我的文本编辑器和浏览器,它并没有执行我想要执行的操作。

function myFunction() {
  var x = document.getElementById("myTopnav");
  if (x.className === "topnav") {
    x.className += " responsive";
  } else {
    x.className = "topnav";
  }
}
.topnav {
  overflow: hidden;
  background-color: #333;
}

.topnav a {
  float: left;
  display: block;
  color: #f2f2f2;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
  font-size: 17px;
}

.topnav a:hover {
  background-color: #ddd;
  color: black;
}

.active {
  background-color: #4CAF50;
  color: white;
}

.topnav .icon {
  display: none;
}

@media screen and (max-width: 600px) {
  .topnav a:not(:first-child) {
    display: none;
  }
  .topnav a.icon {
    float: right;
    display: block;
  }
}

@media screen and (max-width: 600px) {
  .topnav.responsive {
    position: relative;
  }
  .topnav.responsive a.icon {
    position: absolute;
    right: 0;
    top: 0;
  }
  .topnav.responsive a {
    float: none;
    display: block;
    text-align: center;
  }
}

.summary {
  min-height: 75vh;
  max-width: 2000px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.profilePicture {
  padding: 2rem;
}

.profileSummary {
  max-width: 400px;
}

@media screen and (max-width: 800px) {
  .summary {
    flex-direction: column;
  }
}
<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Homepage</title>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
  <link rel="stylesheet" type="text/css" href="main.css">
  <script src="script.js"></script>
</head>

<body>

  <!-- The navigation menu -->

  <div class="topnav" id="myTopnav">
    <a href="home.html" class="active">Home</a>
    <a href="about.html">About Me</a>
    <a href="portfolio.html">Portfolio</a>
    <a href="contact.html">Contact</a>
    <a href="javascript:void(0);" class="icon" onclick="myFunction()">
      <i class="fa fa-bars"></i>
    </a>
  </div>



  <div class="summary">
    <div class="profilePicture">
      <img src="https://ih1.redbubble.net/image.464384650.8618/flat,550x550,075,f.jpg" style="width: 170px; height: 170px; border-radius:50%;">
    </div>
    <div class="profileSummary">
      Attentive alas because yikes due shameful ouch much kookaburra cantankerously up unbridled far vulnerably climbed aristocratically hired brusque fox said the therefore terrier scallop innocent on goodness mongoose woolly showed insistently and.
    </div>
  </div>

</body>

</html>

【问题讨论】:

  • 很难理解你的意思,或者我的英语很差:(
  • (1) 打开导航 (2) 菜单下拉 (3) 项目不在菜单中心和下方
  • @Cuong Hoang 这就是我想要我的物品的地方 ---> i.imgur.com/eySnLM8.jpg
  • 我似乎无法重现这一点。我在 JSFiddle 中尝试过,但它似乎工作正常:jsfiddle.net/duvLtsrf
  • 问题是您在main.css 的第 242 行有第二个 .responsive 课程。在其中,您将宽度指定为 24.99%。这将覆盖您之前声明的同名类。删除它,它应该可以工作。

标签: html css drop-down-menu responsive-design flexbox


【解决方案1】:

该用户最初发布的代码和标记 sn-p 实际上按预期工作。经过进一步调查,问题在于用户有第二个(重复的)CSS 类覆盖了他们早期的 CSS。

第一个类是在媒体查询中,它本身就起作用了:

@media screen and (max-width: 600px) {
  .topnav.responsive {
    position: relative;
  }
  .topnav.responsive a.icon {
    position: absolute;
    right: 0;
    top: 0;
  }
  .topnav.responsive a {
    float: none;
    display: block;
    text-align: center;
  }
}

这个类在它下面:

.responsive {
  padding: 0 6px;
  float: left;
  width: 24.99999%;
}

当屏幕宽度 responsive 类应用于导航栏和汉堡菜单图标出现。但是第二个 responsive 类将展开菜单的宽度限制为容器的 25%,这导致了不希望的定位。

一般而言,对于此类问题,最好使用浏览器中的开发人员工具查看哪些 CSS 规则应用于相关元素。您可以了解更多关于developer tools here的信息。

【讨论】:

  • 嘿伙计,你认为可以再帮我一次。感谢您之前的帮助,我在这里取得了一些进展。不幸的是,有些东西 w3schools 没有告诉你,那就是切换页面和设计页面。有机会来我的下一个论坛看看stackoverflow.com/q/54396946/10332967
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-04-26
  • 1970-01-01
  • 2019-07-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-17
相关资源
最近更新 更多