gpt4 book ai didi

html - 防止标题下的 float UL 和 LI 元素

转载 作者:太空狗 更新时间:2023-10-29 15:25:11 25 4
gpt4 key购买 nike

下面是标题内的菜单。 ul 和 li 元素是 float 的,现在 float 在标题下面,我试图用 clear:both 来防止这种情况。但是,这似乎不起作用,所以我想知道……哪里出了问题?

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>

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;
}

最佳答案

您可以使用 clearfix:

.clearfix:after {
clear:both;
content:".";
display:block;
height:0;
line-height:0;
visibility:hidden;
}

并将其应用于 ul 和 header 。

关于html - 防止标题下的 float UL 和 LI 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12989259/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com