我正在尝试清除浮动中的两个元素,但是它不起作用,我不知道为什么。答案可能(字面上)就在我眼前。
清除之前,可能与我的媒体查询或其他样式有关。清除本身甚至可以正常工作,但float属性无效。
这是样式表中的两个元素:
/* main navigation on small screens like smartphones */
#main_nav {
position: sticky;
top: 0;
text-align: center;
box-shadow: 0 0 1.563rem 0;
}
#main_nav ul {
margin: 0;
padding: 0;
background-color: #1f1f14;
border-top: 0.0625rem solid #faf352;
}
#main_nav ul li {
list-style-type: none;
display: inline-block;
margin: 0.625rem 0;
width: 22%;
padding: 0;
text-align: center;
}
#main_nav ul li a {
padding: 0.2rem;
color: #D7D7CF;
font-family: 'Finger Paint';
font-size: 0.7rem;
text-decoration: none;
}
/* main container on small screens */
main {
padding: 0.2rem 0.5rem 0 0.5rem;
}
/* large desktop screens - more than 1000px width */
@media screen and (min-width: 62.500em) {
#main_nav {
position: static;
float: left;
width: 20%;
padding: 1rem;
box-shadow: none;
}
#main_nav ul li {
width: 100%;
padding: 0.5rem;
display: block;
}
#main_nav ul li a {
display: inline-block;
width: 90%;
font-size: 0.9rem;
margin: 0.2rem;
padding: 0.3rem;
}
main {
width: 95%;
float: left;
padding: 0 1rem;
}
/* clear float */
.cf:before,
.cf:after {
content: "";
display: table;
display: block;
}
.cf:after {
clear: both;
}
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>page title</title>
<link href="mystyles.css" rel="stylesheet" type="text/css">
</head>
<body>
<div id="holder">
<header id="page_header">
<h1><span class="yellow">This is</span> a heading</h1>
</header>
<nav id="main_nav" class="cf">
<ul>
<li><a href="#">link one</a></li>
<li><a href="#">link two</a></li>
<li><a href="#">link three</a></li>
<li><a href="#">link four</a></li>
</ul>
</nav>
<main class="cf">
<section>
<article>
<h3>This is an awesome heading</h3>
<p>text</p>
<img src="images/st-color-300px.jpg" alt="picture">
<p>text</p>
</article>
</section>
</main>
</div>
</body>
</html>
答案 0 :(得分:0)
在容器上而不是在浮动元素上应用clearfix。在这里,您应该在#holder
上应用clearfix(尽管我建议添加一个类并以它而不是id为目标,但我在这里使用了container
)。
/* main navigation on small screens like smartphones */
#main_nav {
position: sticky;
top: 0;
text-align: center;
box-shadow: 0 0 1.563rem 0;
}
#main_nav ul {
margin: 0;
padding: 0;
background-color: #1f1f14;
border-top: 0.0625rem solid #faf352;
}
#main_nav ul li {
list-style-type: none;
display: inline-block;
margin: 0.625rem 0;
width: 22%;
padding: 0;
text-align: center;
}
#main_nav ul li a {
padding: 0.2rem;
color: #D7D7CF;
font-family: 'Finger Paint';
font-size: 0.7rem;
text-decoration: none;
}
/* main container on small screens */
main {
padding: 0.2rem 0.5rem 0 0.5rem;
}
/* large desktop screens - more than 1000px width */
@media screen and (min-width: 62.500em) {
#main_nav {
position: static;
float: left;
width: 20%;
padding: 1rem;
box-shadow: none;
}
#main_nav ul li {
width: 100%;
padding: 0.5rem;
display: block;
}
#main_nav ul li a {
display: inline-block;
width: 90%;
font-size: 0.9rem;
margin: 0.2rem;
padding: 0.3rem;
}
main {
width: 95%;
float: left;
padding: 0 1rem;
}
/* clear float */
.container:after {
content: "";
display: table;
clear: both;
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>page title</title>
<link href="mystyles.css" rel="stylesheet" type="text/css">
</head>
<body>
<div id="holder" class="container">
<header id="page_header">
<h1><span class="yellow">This is</span> a heading</h1>
</header>
<nav id="main_nav" class="cf">
<ul>
<li><a href="#">link one</a></li>
<li><a href="#">link two</a></li>
<li><a href="#">link three</a></li>
<li><a href="#">link four</a></li>
</ul>
</nav>
<main class="cf">
<section>
<article>
<h3>This is an awesome heading</h3>
<p>text</p>
<img src="images/st-color-300px.jpg" alt="picture">
<p>text</p>
</article>
</section>
</main>
</div>
</body>
</html>