代码之家  ›  专栏  ›  技术社区  ›  Ken Ray

正确使用浮动(对于那些喜欢CSS标准的人)?

  •  0
  • Ken Ray  · 技术社区  · 7 年前

    我想知道我在下面的代码中是否以正确的方式(有效地)使用了浮动。

    这是我的 :

    <div id="content">
        <div id="left-column">
            <h2>left-column</h2>
            <p>
            erat, nec semper dui diam ut libero. Donec adipiscing placerat metus. 
            Integer eu eros vel risus ornare consequat. Curabitur sem erat, tempor 
            </p>
        </div>
        <div id="main-column">
            <h2>main-column</h2>
            <p>
            erat, nec semper dui diam ut libero. Donec adipiscing placerat metus. 
            Integer eu eros vel risus ornare consequat. Curabitur sem erat, tempor 
            </p>
        </div>
        <div id="right-column">
            <h2>right-column</h2>
            <p>
            erat, nec semper dui diam ut libero. Donec adipiscing placerat metus. 
            Integer eu eros vel risus ornare consequat. Curabitur sem erat, tempor 
            </p>
        </div>
    </div>
    

    这是我的 style.css:

    #navigation {
    font-family: Trebuchet MS;
    font-weight: bold;
    color: #FFF;
    width: 900px;
    background-color: #000;
    overflow: hidden;
    }
    
    #left-column {
    margin: 10px 10px 0 0;
    width: 150px;
    padding: 10px;
    float: left; 
    color: #FFF;
    background-color: #A53030;
    }
    
    #main-column {
    width:410px;
    padding: 10px;
    float: left; 
    }
    
    #right-column {
    width: 260px;
    padding: 10px;
    float: right; 
    }
    
    #footer {
    margin-top: 10px;
    padding: 5px;
    width: 900px;
    border-color: #262626;
    border-top-style: solid; 
    border-width: medium;
    clear: both;
    }
    

    我在页脚中添加了clear:两者都是因为我在很多网页中都看到了这一点,但我不确定为什么会出现,因为如果我删除它,什么都不会发生。

    3 回复  |  直到 14 年前
        1
  •  2
  •   Dan Herbert    16 年前

    你做得对。

    你不需要 clear: both 在您的页脚中,因为您 clearing your floats the "Correct Way"™ . 基本上,通过将overflow设置为hidden,可以避免清除页脚中的浮动。把它放在那里应该不会有什么伤害,但我会把它拿走 清楚:两者都有

        2
  •  0
  •   jaywon    16 年前

    clear 定义是说任何其他浮动内容都将放置在“cleared”元素下面。正如Dan所指出的,这里没有必要这样做,但是如果你要做浮动CSS布局,了解它的功能是很重要的。

        3
  •  0
  •   jhuffaker    16 年前

    您可能应该在所有这些选项上都使用float:left。然后,如果希望内容显示在3列下面,可以使用clear:left。