为什么此侧面导航菜单溢出了容器?

时间:2018-08-09 00:44:47

标签: html css navigation

我只是在为初学者准备的在线HTML / CSS课程中建立一个迷你测试项目,而且我不明白为什么我的导航栏内容不响应屏幕宽度。如果您继续减小屏幕宽度,则标题边框会一直贯穿我的内容(我将边框保留为打开状态,以便可以看到这种情况)。

我已在下面复制了我的代码和课程讲师的代码,以便您可以告诉我为什么他的作品但我的却没有。我看到的唯一主要区别是我使用了锚标签,而他使用了按钮标签进行导航,但我仍然不明白为什么这是个问题(我将锚标签设置为display:block;以防内联显示成为问题)。我以为大多数人在其导航中使用了定位标记?? :/请帮忙!!!

我的代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="mycss.css">
</head>
<body>

  <header>
    <div id="top-nav">
      <ul>
        <li><a href="#" class="bg-gray">Inbox</a></li>
        <li><a href="#">Contact</a></li>
        <li><a href="#">Accounts</a></li>
      </ul>
    </div>

    <div>
      <ul>
        <li><a href="#">Legal</a></li>
      </ul>
    </div>
  </header>

</body>
</html>

* {
  margin: 0;
  border: 0;
  padding: 0;
  box-sizing: border-box;
}

header{
  border: 1px solid hotpink;
  height: 100vh;
  width: 10vw;
  display: flex;
  flex-direction: column;
  justify-content: space-around;
  border-right: 1px solid #D7DBDD;
}

div#top-nav {
  border: 5px solid green;
  height: 25%;
  width: 100%;
  display: flex;
  flex-direction: column;
}

ul {
  border: 1px solid gold;
  list-style-type: none;
  height: 100%;
  width: 100%;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  align-items: center;
}

li {
  border: 1px solid red;
  width: 100%;
  /* text-align: center; */
}

li a {
  border: 1px solid blue;
  display: block;
  padding: 20px 0 20px 20px;
  text-decoration: none;
  color: #16A2D7;
  font-size: font-size: 2vw;
}

li a:hover {
  background-color: #EEF3F5;
}

讲师代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Sidebar Menu</title>
    <link rel="stylesheet" href="main.css">
</head>
<body>

<div id="sidebar">
    <nav class="sidebar-child sbc-top">
        <button>Inbox</button>
        <button>Contact</button>
        <button>Accounts</button>
    </nav>
    <div class="sidebar-child sbc-btm">
        <button>Legal</button>
    </div>
</div>

</body>
</html>

html, body, div, nav, button {
    padding: 0;
    border: 0;
    margin: 0;
    box-sizing: border-box;
}

html {
    color: #16A2D7;
    font-size: 2vw;
}

#sidebar {
    height: 100vh;
    /* arrange child elements */
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    /* format */
    border-right: 1px solid #D7DBDD;
    width: fit-content; /*the width*/
}

.sidebar-child {
    /* arrange child elements */
    display: flex;
    flex-direction: column;
}

.sbc-top {
    /* format */
    padding-top: 2rem;
}

.sbc-btm {
    padding-bottom: 2rem;
}

button {
    /* format */
    color: inherit;
    background-color: white;
    font-size: 1rem;
    height: 3rem;
    width: 10rem; /*the width*/
    text-align: left;
    padding-left: 2rem;
    cursor: pointer;
}

button:hover {
    background-color: #EEF3F5;
}

0 个答案:

没有答案