gpt4 book ai didi

html - CSS- float 元素折叠父元素

转载 作者:行者123 更新时间:2023-11-28 17:28:13 25 4
gpt4 key购买 nike

我是堆栈overflow 和网络开发的新手,非常感谢您的耐心等待。 :)
所有 HTML 和 CSS 减去某些不相关的部分都可以在最底部找到。


当使用 CSS 设置 HTML 元素的样式时,我遇到了 float 元素及其父元素的问题。 ID 为navma​​in 的div 包含在id 为content 的div 中。 nav 包含一个无序列表,它将用作各种基本导航栏,而 main 包含页面的段落和“内容”。我希望段落位于导航栏的右侧,所以我决定将它们都 float 。

下面的 CSS 应用于这些元素。


#nav {
width: 100px;
float: left;
}

#nav ul {
list-style-type: none;
padding: 0px;
}

#main {
width: 600px;
float: right;
}

当我在浏览器中运行我的代码时,一切看起来都很好。元素被定位在它们适当的位置。但是,在使用检查器工具进行仔细检查时,我的 id 内容的父元素已折叠。

不幸的是,由于缺乏声誉,我无法发布图片。希望这是足够的信息。我已经在网上搜索过这个问题,这在像我这样的新手中似乎很常见。我读了一篇关于 css-tricks.com 的文章,我尝试了他们解决这个问题的方法。不幸的是,这无济于事。经过进一步搜索,我感到困惑。我想为这个问题的广泛性道歉。感谢大家的宝贵时间和知识。


这是 HTML..


<!DOCTYPE html>
<html>
<head>
<title>My Website</title>
<link rel="stylesheet" type="text/css" href="../css/refreshstyle.css">
</head>
<body>
<div id="container">
<div id="header">
<h1>My Website</h1>
</div>
<div id="content">
<div id="nav">
<h3>Navigation</h3>
<ul>
<li><a class="home" href="">Home</a></li>
<li><a class="about" href="">About</a></li>
<li><a class="contacts" href="">Contact</a></li>
</ul>
</div>
<div id="main">
<h2>Home Page</h2>
<p></p>
<p></p>
<p></p>
<p></p>

</div>
</div>
<div id="footer">
Copyright &copy;2014
</div>
</div>
</body>


下面是 CSS...


body {
background-image: url('congruent_pentagon.png');
}
#container {
background-color: white;
width: 800px;
margin-left: auto;
margin-right: auto;
border: 2px solid #a1a1a1;
border-radius: 25px;
}
#header {
background-color: #66CCFF;
color: white;
text-align: center;
padding: 10px;
border-bottom: 2px solid #a1a1a1;
border-top-right-radius:23px;
border-top-left-radius: 23px;
}
h1, h2, h3 {
margin:0;
}
#nav {
width: 100px;
float: left;
}
#nav ul {
list-style-type: none;
padding: 0px;
}
a {
text-decoration: none;
color: #66CCFF;
}
#nav .home {
font-weight: bold;
}
#main {
width: 600px;
float: right;
}
#content {
padding: 10px;
}
#footer {
clear: both;
background-color: #B1F6CB;
padding: 10px;
margin: 0px;
text-align: right;
border-top: 2px solid #a1a1a1;
border-bottom-right-radius: 23px;
border-bottom-left-radius: 23px;

}

最佳答案

在这种情况下,您可以使用 css clearfix 类。在您的 CSS 中定义 clearfix 类:

.clearfix:before,
.clearfix:after {
content: "";
display: table;
}

.clearfix:after {
clear: both;
}

.clearfix {
zoom: 1; /* ie 6/7 */
}

在父元素上使用 clearfix 类,在您的例子中是在#content 元素上:

<div id="content" class="clearfix">
[...]
</div>

在此 blog post 中查找有关清除 float 的更多信息.

顺便说一句:欢迎使用 stackoverflow :)

关于html - CSS- float 元素折叠父元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26705696/

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