不同大小的div对齐左上角

时间:2013-08-10 06:30:26

标签: javascript css

CSS

  #box1, #box2, #box3, #box4, #box5, #box6, #box7, #box8, #box9, #box10, #box11, #box12, #box13, #box14, #box15, #box16, #box17, #box18, #box19, #box20 {
    position: relative;
    list-style: none;
    float: left;
clear:left:
}
#box1 {
    width: 60px;
    height: 60px;
    background: yellow;
}
#box2 {
    width: 80px;
    height: 50px;
    background: blue;
}
#box3 {
    width: 40px;
    height: 60px;
    background: red;
}
#box4 {
    width: 200px;
    height: 150px;
    background: green;
}
#box5 {
    width: 60px;
    height: 100px;
    background: red;
}
#box6 {
    width: 70px;
    height: 30px;
    background: blue;
}
#box7 {
    width: 40px;
    height: 80px;
    background: yellow;
}
#box8 {
    width: 90px;
    height: 60px;
    background: red;
}
#box9 {
    width: 50px;
    height: 80px;
    background: blue;
}
#box10 {
    width: 40px;
    height: 60px;
    background: yellow;
}
#box11 {
    width: 60px;
    height: 60px;
    background: yellow;
}
#box12 {
    width: 80px;
    height: 50px;
    background: blue;
}
#box13 {
    width: 40px;
    height: 60px;
    background: red;
}
#box14 {
    width: 100px;
    height: 50px;
    background: yellow;
}
#box15 {
    width: 60px;
    height: 100px;
    background: red;
}
#box16 {
    width: 70px;
    height: 30px;
    background: blue;
}
#box17 {
    width: 40px;
    height: 80px;
    background: yellow;
}
#box18 {
    width: 90px;
    height: 60px;
    background: red;
}
#box19 {
    width: 50px;
    height: 80px;
    background: blue;
}
#box20 {
    width: 40px;
    height: 60px;
    background: yellow;
}

我需要在左上角对齐多个div,div之间没有任何空格。但我需要,一个div来保持我需要的确切位置(如窗口的“绿色”div中心)。在小提琴中,你可以看到p17和p18与其他div完全不同。这些div有空间。我需要将这两个div放入该空间。我可以在不给出边距的情况下解决这个问题。

如果我添加新的div,我需要自动附加这些div与其他div,在演示中以相同的方式,没有空格。有没有办法做到这一点。

check the [Demo][1]


|__||  ||     ||   |
|  ||__||_____||   |
|  ||         ||___|
|__||_________||___|
|  ||      ||      |
|__||______||______|

![drawing][2]

i need align all divs in the above demo, like this drawing.

http://jsfiddle.net/ramswarnak/G8dKr/8/

http://i.stack.imgur.com/MbLLf.png

1 个答案:

答案 0 :(得分:2)

请添加此css:

#box1, #box2, #box3, #box4, #box5, #box6, #box7, #box8, #box9, #box10, #box11, #box12, #box13, #box14, #box15, #box16, #box17, #box18, #box19, #box20 {   margin:0px; padding:0px; float: left; }

ul, li {  margin:0px; padding:0px; list-style:none;}