溢出:隐藏在这个程序中到底是什么?

时间:2017-10-12 23:51:17

标签: css



* {
    box-sizing: border-box;
}
body {
    margin: 0;
}
.header {
    background-color: #2196F3;
    color: white;
    text-align: center;
    padding: 15px;
}
.footer {
    background-color: #444;
    color: white;
    padding: 15px;
}
.topmenu {
    list-style-type: none;
    margin: 0;
    padding: 0;
    overflow: hidden;
    background-color: #777;
}
.topmenu li {
    float: left;
}
.topmenu li a {
    display: inline-block;
    color: white;
    text-align: center;
    padding: 16px;
    text-decoration: none;
}
.topmenu li a:hover {
    background-color: #222;
}
.topmenu li a.active {
    color: white;
    background-color: #4CAF50;
}
.column {
    float: left;
    padding: 15px;
}
.clearfix::after {
    content: "";
    clear: both;
    display: table;
}
.sidemenu {
    width: 25%;
}
.content {
    width: 75%;
}
.sidemenu ul {
    list-style-type: none;
    margin: 0;
    padding: 0;
}
.sidemenu li a {
    margin-bottom: 4px;
    display: block;
    padding: 8px;
    background-color: #eee;
    text-decoration: none;
    color: #666;
}
.sidemenu li a:hover {
    background-color: #555;
    color: white;
}
.sidemenu li a.active {
    background-color: #008CBA;
    color: white;
}

<!DOCTYPE html>
<html>
<head>
</head>
<body>

<ul class="topmenu">
  <li><a href="#home" class="active">Home</a></li>
  <li><a href="#news">News</a></li>
  <li><a href="#contact">Contact</a></li>
  <li><a href="#about">About</a></li>
</ul>

<div class="clearfix">
  <div class="column sidemenu">
    <ul>
      <li><a href="#flight">The Flight</a></li>
      <li><a href="#city" class="active">The City</a></li>
      <li><a href="#island">The Island</a></li>
      <li><a href="#food">The Food</a></li>
      <li><a href="#people">The People</a></li>
      <li><a href="#history">The History</a></li>
      <li><a href="#oceans">The Oceans</a></li>
    </ul>
  </div>

  <div class="column content">
    <div class="header">
      <h1>The City</h1>
    </div>
    <h1>Chania</h1>
    <p>Chania is the capital of the Chania region on the island of Crete. The city can be divided in two parts, the old town and the modern city.</p>
    <p>You will learn more about responsive web pages in a later chapter.</p>
  </div>
</div>

<div class="footer">
  <p>Footer Text</p>
</div>

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

我编译了这个程序没有溢出:隐藏,它没有提供相同的输出,我无法理解为什么溢出:隐藏在这里是必要的?我甚至使用overflow:auto并且它没有显示滚动因此我知道它不是盒子对于数据来说太小了。它出现在顶部菜单中。 我是CSS的新手,我真的很困惑,为什么他们为某些元素有两个类,为什么他们使用box-sizing, 如果我的方式有任何问题,请编辑 问,因为贬低它禁止我提问和 - 让我无助。

1 个答案:

答案 0 :(得分:0)

我是一位经验丰富的CSS用户,这需要一段时间才能找到。 .topbar元素是ul,没有定义的高度,因此它会查找子元素以确定高度。但是,由于您的元素内部使用float,因此不会调整父级的大小。

正如this previous answer所讨论的那样,overflow: none是解决此问题的黑客行为。但是,我不认为这是您的最佳方法。我要么:

  1. 为顶栏定义恒定高度
  2. 使用flexbox代替ul