三列浮动固定布局

时间:2015-03-16 05:03:52

标签: html css html5 css3 css-float

<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>3 column layout</title>
<style>
aside, article, section, header, footer, nav {
    display: block;
}
html, body {
    margin: 0;
    padding: 0;
}
html {
    background: rgb(123, 121, 143);
}
body {
    width: 960px;
    background: #fff;
    margin: 0 auto 2em;
    font: 100% Georgia, "Times New Roman", Times, serif;
}
header {
    background: rgb(76, 67, 65);
    margin-bottom: 20px;
}
header h1 {
    font: normal 2em Arial, Helvetica, sans-serif;
    color: white;
    text-align: center;
    line-height: 4em;
    text-transform: uppercase;
    letter-spacing:.1em;
    margin: 0;
}
.col1 {
    background: rgb(237, 228, 214);
    height: 500px;
    float:left;
    width:300px;

}
.col2 {
    background: rgb(219,126,64);
    height: 500px;
    width:300px;
    margin-left:330px;

}
.col3 {
    background: rgb(173, 169, 130);
    height: 500px;
    width:300px;
    margin-left:660px;
}
footer {
    background: rgb(100, 98, 102);
    line-height: 3em;
    font-size: .6em;
    color: white;
    padding: 0 2em;
    clear: both;
}
</style>
</head>
<body>
<header>
<h1>Cool company header</h1>
</header>
<section class="col1">
This is where the really important stuff goes.
</section>
<section class="col2">
This is where equally important stuff goes.
</section>
<aside class="col3">
This is where the related content goes.
</aside>
<footer>Copyright stuff....</footer>
</body>
</html>

OR

<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>3 column layout</title>
<style>
aside, article, section, header, footer, nav {
    display: block;
    margin:0;
    padding:0;
}
html, body {
    margin: 0;
    padding: 0;
}
html {
    background: rgb(123, 121, 143);
}
body {
    width: 960px;
    background: #fff;
    margin: 0 auto 2em;
    font: 100% Georgia, "Times New Roman", Times, serif;
}
header {
    background: rgb(76, 67, 65);
    margin-bottom: 20px;
}
header h1 {
    font: normal 2em Arial, Helvetica, sans-serif;
    color: white;
    text-align: center;
    line-height: 4em;
    text-transform: uppercase;
    letter-spacing:.1em;
    margin: 0;
}
.col1 {
    background: rgb(237, 228, 214);
    height: 500px;
    float:left;
    width:300px;
    margin-right:30px;

}
.col2 {
    background: rgb(219,126,64);
    height: 500px;
    width:300px;
    margin-right:20px;


}
.col3 {
    background: rgb(173, 169, 130);
    height: 500px;
    width:300px;

}
footer {
    background: rgb(100, 98, 102);
    line-height: 3em;
    font-size: .6em;
    color: white;
    padding: 0 2em;
    clear: both;
}

section {
    display:inline-block;
}
aside {
    display:inline-block;
}

</style>
</head>
<body>
<header>
<h1>Cool company header</h1>
</header>
<section class="col1">
This is where the really important stuff goes.
</section>
<section class="col2">
This is where equally important stuff goes.
</section>
<aside class="col3">
This is where the related content goes.
</aside>
<footer>Copyright stuff....</footer>
</body>
</html>

我的体宽为960px,因此将其分为3列,每列300px X 3,因此总共900px,边距为30px X 2 b / w,两列总共为60px。所有它总计达960px。 / p>

现在我已经给出了第一列宽度为300px并使用了float属性,所以第二个盒子旁边是allign,所以我给了一个330px的边距,即20px,所以我完成了工作。所以我还剩一个空间在右侧330px,我给了第3个盒子660px的边距,即20px,宽度为300px。

我希望第3个盒子坐在第二个旁边没有发生而不是它进入第二行,我知道我可以使用float-left两个盒子或者使用一个浮动到第三个盒子。我想知道为什么这个方法不是很强硬他们的空间。

在第二个1中我使用了旁边和部分作为内联块,即使它起作用,但问题是,我在所有三个盒子上使用300px消耗了900px [300X3 = 900]我的'身体'宽度是960px,当我给出右边30px和30px的边距时,第三个框移动到第二行,但是当我使用30px和20px时它仍然保持原因?

2 个答案:

答案 0 :(得分:0)

很简单,您没有将column2浮动到左侧,因此它包含在普通文档流中。因此它获取整个块空间并将第三列移动到它下面。你需要做的就是

&#13;
&#13;
.col2 {
    background: rgb(219,126,64);
    height: 500px;
    width:300px;
    float: left;
    margin-left:30px;
}
&#13;
&#13;
&#13;

考虑到这一点,我还减少了左边距,因为它会在我对其应用浮动之后堆叠在column1上时从column1计算其边距。

答案 1 :(得分:0)

请检查一下http://jsfiddle.net/966naq5e/17/ 我改变了一点结构并更新了

&#13;
&#13;
aside, article, section, header, footer, nav {
    display: block;
    margin:0;
    padding:0;
}
*{
    box-sizing: border-box
}
html, body {
    margin: 0;
    padding: 0;
}
html {
    background: rgb(123, 121, 143);
}
body {
    width: 960px;
    background: #fff;
    margin: 0 auto 2em;
    font: 100% Georgia, "Times New Roman", Times, serif;
}
header {
    background: rgb(76, 67, 65);
    margin-bottom: 20px;
}
header h1 {
    font: normal 2em Arial, Helvetica, sans-serif;
    color: white;
    text-align: center;
    line-height: 4em;
    text-transform: uppercase;
    letter-spacing:.1em;
    margin: 0;
}
.col1 .content{
    background: rgb(237, 228, 214);
    height: 500px;

}
.col2 .content{
    background: rgb(219,126,64);
    height: 500px;


}
.col3 .content{
    background: rgb(173, 169, 130);
    height: 500px;

}
footer {
    background: rgb(100, 98, 102);
    line-height: 3em;
    font-size: .6em;
    color: white;
    padding: 0 2em;
    clear: both;
}

section {
    display:inline-block;
}
aside {
    display:inline-block;
}
.container{
    width: 960px;
    margin: 0 auto;
}
.holder{
    margin: 0 -15px;
    overflow: hidden;
}
.holder .col{
    width: 330px;
    padding: 0 15px;
    float: left;
}
&#13;
<div class="container">
    <div class="holder">
        <section class="col1 col">
         <div class="content">
             This is where the really important stuff goes.
         </div>
        </section>
        <section class="col2 col">
         <div class="content">This is where equally important stuff goes.</div>
        </section>
        <aside class="col3 col">
       <div class="content"> This is where the related content goes.</div>
        </aside>
    </div>
</div>
<footer>Copyright stuff....</footer>
&#13;
&#13;
&#13;