2012-05-13 163 views
3

如何更改此网格布局,使第二行块位于其上方相应块的正下方,而不是形成全新的行?HTML/CSS网格布局

http://jsfiddle.net/AndyMP/wSvrN/

body { 
    margin-left: 0px; 
    margin-top: 0px; 
    margin-right: 0px; 
    margin-bottom: 0px; 
} 
#container { 
    position: absolute; 
    width: 60%; 
    left: 50%; 
    margin-left: -30%; 
    border: 1px dotted #333; 
    padding: 20px; 
} 
.box1 { 
    position: relative; 
    float: left; 
    width: 100px; 
    height: 150px; 
    background: #666; 
    margin: 0 15px 15px 0; 
} 
.box2 { 
    position: relative; 
    float: left; 
    width: 100px; 
    height: 200px; 
    background: #666; 
    margin: 0 15px 15px 0; 
} 
.box3 { 
    position: relative; 
    float: left; 
    width: 100px; 
    height: 100px; 
    background: #666; 
    margin: 0 15px 15px 0; 
} 

HTML

<div id="container"> 

    <div class="box1"></div> 
    <div class="box3"></div> 
    <div class="box1"></div> 
    <div class="box2"></div> 
    <div class="box3"></div> 
    <div class="box2"></div> 
    <div class="box3"></div> 
    <div class="box2"></div> 
    <div class="box1"></div> 
    <div class="box1"></div> 
    <div class="box3"></div> 
    <div class="box2"></div> 

</div> 
+0

我猜css的“浮动”部分只是水平排列而不是垂直排列,但也许有人可以证实这一点。 – Andy

+0

我猜我需要使用像Masonry(http://masonry.desandro.com/)这样的插件,并且只有普通的CSS才有可能。 – Andy

回答

1

我不完全得到你想要的。当我将该代码放入测试文件时,我在浏览器中看到以下内容:enter image description here

对我来说,看起来最后两个块看起来好像在同一列中的其他块之下。

一个问题可能是您将容器的宽度指定为屏幕的百分比,并将块的宽度指定为绝对值。

请评论,所以我可以让我的答案更清晰,这是我可以从你的问题中得到的所有信息。

+1

我的意思是说,一旦最后一个箱子右边没有空间,就会创建一个新的行,第一个箱子坐在第一个箱子的下面,紧接着第二个箱子的下面,等等。可能是一个容器宽度的东西,但即使有一个固定的宽度,问题仍然存在http://jsfiddle.net/AndyMP/wSvrN/1/ – Andy

+0

我明白你的问题了。我编辑小提琴没有位置和浮动,但它并没有真正的帮助。我不知道这是可能的只是CSS。如果我发现任何事情,我会让你知道的。我的小提琴:http://jsfiddle.net/wSvrN/9/ ​​ – Hidde

2

你的意思是这样吗? http://jsfiddle.net/LtLrx/

CSS:

body { 
    margin-left: 0px; 
    margin-top: 0px; 
    margin-right: 0px; 
    margin-bottom: 0px; 
} 
#container { 
    position: absolute; 
    width: 60%; 
    left: 50%; 
    margin-left: -30%; 
    border: 1px dotted #333; 
    padding: 20px; 
} 
.contleft 
{ 
    float: left; 
    margin-right: 15px; 
    width: 30%; 
    position: relative; 
    height: 100%; 
} 
.contmiddle 
{ 
    float: left; 
    margin-right: 15px; 
    width: 30%; 
    position: relative; 
    height: 100%; 
} 
.contright 
{ 
    float: left; 
    width: 30%; 
    position: relative; 
    height: 100%; 
} 
.box1 { 
    position: relative; 
    float: left; 
    width: 100px; 
    height: 150px; 
    background: #666; 
    margin: 0 15px 15px 0; 
} 
.box2 { 
    position: relative; 
    float: left; 
    width: 100px; 
    height: 200px; 
    background: #666; 
    margin: 0 15px 15px 0; 
} 
.box3 { 
    position: relative; 
    float: left; 
    width: 100px; 
    height: 100px; 
    background: #666; 
    margin: 0 15px 15px 0; 
} 

HTML:

<div id="container"> 

    <div class="contleft"> 
     <div class="box1"></div> 
     <div class="box2"></div> 
     <div class="box3"></div> 
     <div class="box1"></div> 
    </div> 
    <div class="contmiddle"> 
     <div class="box2"></div> 
     <div class="box1"></div> 
     <div class="box1"></div> 
     <div class="box2"></div> 
    </div> 
    <div class="contright"> 
     <div class="box3"></div> 
     <div class="box1"></div> 
     <div class="box2"></div> 
     <div class="box1"></div> 
    </div> 
</div> 
+1

它可以完成这项工作,但我只是避免使用列,因为我必须手动将任何新块分配给相应的列。当添加新的DIV时,它不会自动对齐它们。 – Andy