明确:两者都行不通

hol*_*ard 5 css html5 clear css-float

下面是标题内的菜单.ul和li元素是浮动的,现在浮动在标题下面,我试图用它清除:两者.但是,这似乎没有用,所以我想知道......什么可能是错的?

HTML:

<header>
    <ul>
        <li><a href='#'>Item 1</a></li>
        <li><a href='#'>Item 2</a></li>
        <li><a href='#'>Item 3</a></li>
        <li><a href='#'>Item 4</a></li>
    </ul>
    <div class='clear'/>
</header>
Run Code Online (Sandbox Code Playgroud)

CSS:

header {
    background: #888;
    height: 20px;
    padding: 10px;
}

ul{
  margin: 18px 0;
  padding: 0;
  list-style: none;
}

ul li{
  margin: 0 10px 0 0;
  padding: 0;
  display: block;
  float: left;
  width: 80px;
  height: 20px;
  border: 1px solid #000;
  background: red;
}

ul li a{
  display:block;
  width: 100%;
  height: 100%;
  text-decoration: none;
  float: left;
  text-align: center;
  padding-top: 2px;
}

ul li a:hover{
  color: #fff;
  background-color: #000;
}?

.clear {
    clear:both;
}
Run Code Online (Sandbox Code Playgroud)

Bri*_*ian 11

您可以使用clearfix:

.clearfix:after {
  clear:both;
  content:".";
  display:block;
  height:0;
  line-height:0;
  visibility:hidden;
}
Run Code Online (Sandbox Code Playgroud)

并将其应用于ul和标题.