我正在学习CSS中的浮动元素,并遇到以下特殊行为。CSS - 意外浮动问题
下面是代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
#one { background-color: red; width: 200px; height: 200px; margin: 10px; }
#two { background-color: yellow; width: 200px; height: 205px; margin: 10px; }
#three { background-color: lightpink; width: 200px; height: 200px; margin: 10px; }
#four { background-color: green; width: 200px; height: 200px; margin: 10px; }
#five { background-color: coral; width: 200px; height: 200px; margin: 10px; }
#six { background-color: #b1ffea; width: 200px; height: 200px; margin: 10px; }
div{
/*display: inline-block;*/
float: left;
vertical-align: central;
}
</style>
</head>
<body>
<div id="one">
First div
</div>
<div id="two">
Second div
</div>
<div id="three">
three div
</div>
<div id="four">
four div
</div>
<div id="five">
five div
</div>
<div id="six">
six div
</div>
</body>
</html>
我的问题是,为什么 “四个格” 被放置在 “第三次DIV” 不是第一和第二下面?另一方面,如果我调整浏览器的大小(见下图),为什么在这种情况下它完美的工作?
它是如何滑动的?请详细说明或给你指的资源的网址 – Cody
我添加了另一个例子,每行使用4个div元素。我会尽力找到一个资源指向你,但这就是浮动定位在元素高度不同时的工作原理。 –
如果div元素具有预定的宽度,那么您可以使用CSS Media Queries将'clear:left;'添加到适当的':nth-of-type'中以使行均匀。 –