我在下面有以下代码,但是如果我对其应用宽度属性,为什么 div 元素会rightnav
出现在 div 元素下方,我对此感到困惑。leftnav
我做错了什么或者我误解了浮动的使用?
代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTMl 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<meta http-equiv="content-language" content="en-us" />
<meta name="keywords" content="" />
<meta name="description" content="" />
<meta name="author" content="" />
<meta name="copyright" content="© 2012" />
<title>DIV example</title>
<base href="" />
<link rel="stylesheet" href="" />
<style type="text/css">
* {
margin: 0px;
padding: 0px;
font-family: Arial, Verdana, sans-serif;
font-size: 100%;
}
#content {
width: 700px;
margin-top: 20px;
margin-right: auto;
margin-bottom: 0px;
margin-left: auto;
}
#leftnav {
float: left;
width: 200px;
border-width: 1px;
border-color: #000000;
border-style: solid;
}
#rightnav {
border-width: 1px;
border-color: #000000;
border-style: solid;
}
</style>
</head>
<body>
<div id="container">
<div id="content">
<div id="leftnav">left nav</div>
<div id="rightnav">right nav</div>
</div>
</div>
</body>
</html>
输出
width
现在,如果我通过将属性应用于声明来修改代码如下rightnav
,则该元素将出现在下面leftnav
。我认为这可能与 div 元素的宽度有关,content
但是两个 div 元素的组合有足够的宽度,即 200px + 200px < 700px
代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTMl 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<meta http-equiv="content-language" content="en-us" />
<meta name="keywords" content="" />
<meta name="description" content="" />
<meta name="author" content="" />
<meta name="copyright" content="© 2012" />
<title>DIV example</title>
<base href="" />
<link rel="stylesheet" href="" />
<style type="text/css">
* {
margin: 0px;
padding: 0px;
font-family: Arial, Verdana, sans-serif;
font-size: 100%;
}
#content {
width: 700px;
margin-top: 20px;
margin-right: auto;
margin-bottom: 0px;
margin-left: auto;
}
#leftnav {
float: left;
width: 200px;
border-width: 1px;
border-color: #000000;
border-style: solid;
}
#rightnav {
width: 200px;
border-width: 1px;
border-color: #000000;
border-style: solid;
}
</style>
</head>
<body>
<div id="container">
<div id="content">
<div id="leftnav">left nav</div>
<div id="rightnav">right nav</div>
</div>
</div>
</body>
</html>
输出