为什么浮动元素在使用宽度时不并排对齐?

时间:2021-03-08 06:07:15

I have the following code below however am confused as to why the div element rightnav appears below the div element leftnav if I apply a width property to it. What am I doing wrong or have I misunderstood the use of floats?

我有下面的代码,但是我不明白为什么div元素rightnav会出现在div元素leftnav的下方,如果我对它应用一个width属性的话。我做错了什么,或者我误解了花车的用途?

CODE

代码

<!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="&copy; 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>

Output

输出

为什么浮动元素在使用宽度时不并排对齐?

Now if I amend the code as follows by applying the property width to the declaration rightnav, the element appears below leftnav. I thought that it may have to do with the width of the div element content however there is sufficient width with the combination of both div elements i.e. 200px + 200px < 700px

现在,如果我修改代码如下,将属性宽度应用到声明rightnav,元素就会出现在leftnav下面。我认为它可能与div元素内容的宽度有关,但是两个div元素的组合有足够的宽度,比如200px + 200px < 700px

CODE

代码

<!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="&copy; 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>

OUTPUT

输出

为什么浮动元素在使用宽度时不并排对齐?

2 个解决方案

#1


6  

In your first example, #rightnav is not floating, but it stays at the right because it hasn't a clear:left; rule. See here: http://jsfiddle.net/5sHdg/

在第一个例子中,#rightnav并不是浮动的,但是它在右边,因为它没有一个清晰的:左;规则。在这里看到的:http://jsfiddle.net/5sHdg/

In your second example, when you specify a width for #rightnav, the browser has a explicit rule about the div's size, so it renders it as a block element should. But it doesn't float next to #leftnav because it lacks a float:left; rule. See here: http://jsfiddle.net/Br4Lm/

在第二个示例中,当您为#rightnav指定宽度时,浏览器对div的大小有明确的规则,因此它将其呈现为块元素。但是它不会在#leftnav旁边浮动,因为它没有float:left;规则。在这里看到的:http://jsfiddle.net/Br4Lm/

So rememeber:

所以记得:

  1. Use float in every div that needs to be positioned besides another one, thus overriding it's block element appearance.
  2. 在每个需要定位的div中使用float,除了另一个div之外,这样就覆盖了它的块元素外观。
  3. If you expect to have a div element below divs that re floating, be sure to include clear:both; (left, right, or both);
  4. 如果您希望在div元素下面有一个浮动的div元素,请确保包含clear:both;(左,右,或者两者兼而有之);

#2


3  

Adding overflow: hidden to #rightnav will solve your problem. http://jsfiddle.net/Wexcode/gCVaz/

增加溢出:隐藏到#右导航将解决您的问题。http://jsfiddle.net/Wexcode/gCVaz/

An explanation of why this works can be found here: http://colinaarts.com/articles/the-magic-of-overflow-hidden

可以在这里找到这幅作品的解释:http://colinaarts.com/articles/the magic of overflow-hidden

#1


6  

In your first example, #rightnav is not floating, but it stays at the right because it hasn't a clear:left; rule. See here: http://jsfiddle.net/5sHdg/

在第一个例子中,#rightnav并不是浮动的,但是它在右边,因为它没有一个清晰的:左;规则。在这里看到的:http://jsfiddle.net/5sHdg/

In your second example, when you specify a width for #rightnav, the browser has a explicit rule about the div's size, so it renders it as a block element should. But it doesn't float next to #leftnav because it lacks a float:left; rule. See here: http://jsfiddle.net/Br4Lm/

在第二个示例中,当您为#rightnav指定宽度时,浏览器对div的大小有明确的规则,因此它将其呈现为块元素。但是它不会在#leftnav旁边浮动,因为它没有float:left;规则。在这里看到的:http://jsfiddle.net/Br4Lm/

So rememeber:

所以记得:

  1. Use float in every div that needs to be positioned besides another one, thus overriding it's block element appearance.
  2. 在每个需要定位的div中使用float,除了另一个div之外,这样就覆盖了它的块元素外观。
  3. If you expect to have a div element below divs that re floating, be sure to include clear:both; (left, right, or both);
  4. 如果您希望在div元素下面有一个浮动的div元素,请确保包含clear:both;(左,右,或者两者兼而有之);

#2


3  

Adding overflow: hidden to #rightnav will solve your problem. http://jsfiddle.net/Wexcode/gCVaz/

增加溢出:隐藏到#右导航将解决您的问题。http://jsfiddle.net/Wexcode/gCVaz/

An explanation of why this works can be found here: http://colinaarts.com/articles/the-magic-of-overflow-hidden

可以在这里找到这幅作品的解释:http://colinaarts.com/articles/the magic of overflow-hidden