浮动的div被推倒了

时间:2014-03-07 14:11:27

标签: html css

我正在尝试在2列中实现墙拆分:

enter image description here

正如您所看到的,div数字1高于其他div,并且它向下推了div 3和4。 我希望2到4之间的边距与1和3相同(20px)。

这是代码:

    <div class="wall">

            <div class="postDiv">

                      /* this is DIV NUMBER 1 */
                      <div data-post-id="91" class="post" id=""><img src="/images/male_profile.png" class="postImg">    <div class="formatted-text"><h4>roberto mancino</h4>                     <h5>tryewklekwleklweklwkelkweklweklweklwkelwkelkwelkwlekwlekwlekwelkwelkwelwkelkwelkwelkwlekwleklweklwkelweklwkelwkelwkelwkelweklwkewleklwkelwkelwekwe</h5><h6>today - <span>about 10 minutes ago</span></h6><a style="font-size:11px;" class="cancelPost">cancel</a></div></div>

                      /* this is DIV NUMBER 2 */
                      <div data-post-id="90" class="post" id=""><img src="/images/male_profile.png" class="posting">    <div class="formatted-text"><h4>roberto mancino </h4><h5>try</h5><h6>today - <span>about 10 minutes ago</span></h6><a style="font-size:11px;" class="cancelPost">cancel</a></div></div>

                      /* this is DIV NUMBER 3 */
                      <div data-post-id="89" class="post" id=""><img src="/images/male_profile.png" class="posting">     <div class="formatted-text"><h4>roberto mancino </h4><h5>try</h5><h6>today - <span>about 10 minutes ago</span></h6><a style="font-size:11px;" class="cancelPost">cancel</a></div></div>

                      /* this is DIV NUMBER 4 */
                      <div data-post-id="88" class="post" id=""><img src="/images/male_profile.png" class="posting">    <div class="formatted-text"><h4>roberto mancino </h4><h5>try</h5><h6>today - <span>about 10 minutes ago</span></h6><a style="font-size:11px;" class="cancelPost">cancel</a></div></div>



              </div>
       </div>

CSS

.wall{
margin-top: 20px;
width: 100%;
height: 100%;
}

.post{
clear: left;
background-color: lime;
display: block;
z-index: 2;
position: relative;
padding:20px 20px 20px 20px;
width: 42%;
margin: 20px 10px 0px 10px;
background-color: #edeff4;
float: left;
border: 1px solid rgb(216,216,216);
box-shadow: 0px 0px 15px rgba(0,0,0,0.1);
-moz-box-shadow: 0px 0px 15px rgba(0,0,0,0.1);
-webkit-box-shadow: 0px 0px 15px rgba(0,0,0,0.1);
}


.post:nth-child(2n) {
float: right;
clear: right;
}


.formatted-text{

margin-left:80px;
word-wrap: break-word; /* or "normal" */
overflow-wrap: break-word;
}

2 个答案:

答案 0 :(得分:1)

如果您可以更改HTML,则可以使用column-count来获取您要查找的显示:

ul {
    /*styles*/
    -moz-column-count: 2;
    -webkit-column-count: 2;
    column-count: 2;
}

Demo Fiddle1


我做了一个小提琴复制下面评论中的风格。检查它:

Demo Fiddle2 /观看Fullscreen

答案 1 :(得分:0)

我在底部发布的jsfiddle中做的是添加两个div容器,一个rightFloat和一个leftFloat,每个包含两个roberto mansion div(它们可以包含任意数字),然后是第三个包含这两个的整个容器里面有自动边距,以便居中。 CSS在下面(注意我删除了第n个孩子的CSS样式):

.wall {
    margin-top: 20px;
    width: 100%;
    height: 100%;
}
.post {
    background-color: lime;
    z-index: 2;
    padding:20px 20px 20px 20px;
    margin: 20px 10px 0px 10px;
    background-color: #edeff4;
    border: 1px solid rgb(216, 216, 216);
    box-shadow: 0px 0px 15px rgba(0, 0, 0, 0.1);
    -moz-box-shadow: 0px 0px 15px rgba(0, 0, 0, 0.1);
    -webkit-box-shadow: 0px 0px 15px rgba(0, 0, 0, 0.1);
}
.leftFloat {
    float: left;
    clear: left;
    width: 50%;
    display: block;
    position: relative;
}
.rightFloat {
    float: right;
    clear: right;
    width: 50%;
    display: block;
    position: relative;
}
.container {
    margin-left:auto;
    margin-right:auto;
    width: 100%;
}
.formatted-text {
    margin-left:80px;
    word-wrap: break-word;
    /* or "normal" */
    overflow-wrap: break-word;
}

HTML:

<div class="wall">
    <div class="postDiv">
        <div class="container">
            <div class="leftFloat">
                <div data-post-id="91" class="post" id="">
                    <img src="/images/male_profile.png" class="postImg">
                    <div class="formatted-text">
                         <h4>roberto mansion</h4> 
                         <h5>tryewklekwleklweklwkelkweklweklweklwkelwkelkwelkwlekwlekwlekwelkwelkwelwkelkwelkwelkwlekwleklweklwkelweklwkelwkelwkelwkelweklwkewleklwkelwkelwekwe</h5>

                         <h6>today - <span>about 10 minutes ago</span></h6><a style="font-size:11px;" class="cancelPost">cancel</a>

                    </div>
                </div>
                <div data-post-id="90" class="post" id="">
                    <img src="/images/male_profile.png" class="posting">
                    <div class="formatted-text">
                         <h4>roberto mansion</h4>

                         <h5>try</h5>

                         <h6>today - <span>about 10 minutes ago</span></h6><a style="font-size:11px;" class="cancelPost">cancel</a>

                    </div>
                </div>
            </div>
            <div class="rightFloat">
                <div data-post-id="89" class="post" id="">
                    <img src="/images/male_profile.png" class="posting">
                    <div class="formatted-text">
                         <h4>roberto mansion</h4>

                         <h5>try</h5>

                         <h6>today - <span>about 10 minutes ago</span></h6><a style="font-size:11px;" class="cancelPost">cancel</a>

                    </div>
                </div>
                <div data-post-id="88" class="post" id="">
                    <img src="/images/male_profile.png" class="posting">
                    <div class="formatted-text">
                         <h4>roberto mansion</h4>

                         <h5>try</h5>

                         <h6>today - <span>about 10 minutes ago</span></h6><a style="font-size:11px;" class="cancelPost">cancel</a>

                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

jsfiddle:http://jsfiddle.net/TSLz6/5/