如何使用引导程序使我的DIV显示在彼此之下

时间:2015-10-06 12:31:19

标签: css twitter-bootstrap-3

我有以下内容: Pic 1 我想要这些结果: Pic 2

我正在使用bootsrap 3

这是我的HTML和CSS代码:

.product-item {
    /*height: 425px;*/
    padding-left: 5px;
    padding-right: 5px;
    text-align: center;
    /*margin: 0 -10px;*/
    display: inline-block;
}
.product-item_inner {
    background-color: yellow;
    /*display: inline-block;*/
    margin-bottom: 20px;
    /*min-height: 522px;*/
    padding: 10px;

    width:auto; height:auto;
}
a.s_thumb img {
    margin-left: auto;
    margin-right: auto;
    /*max-width: 270px;*/
}
div.product-item h3 {
    font-size: 15px;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <link href="css/bootstrap.min.css" rel="stylesheet">
    <link href="css/style.css" rel="stylesheet">

    <!-- Google Fonts !! -->
    <link href='https://fonts.googleapis.com/css?family=Roboto:300' rel='stylesheet' type='text/css'>

    <!-- Already inserted !! -->
    <link href="css/font-awesome.css" rel="stylesheet">


</head>
<body>


<div class="container">
    <div class="row">
        <div class="col-lg-4 col-md-6 col-sm-6 col-xs-12 product-item">
            <div class="product-item_inner text-center">
                <a class="s_thumb" href="#">
                    <img width="280" height="140" src="http://lorempixel.com/280/140/" class="img-responsive"
                         alt="title example">
                </a>
                <h3><a href="#">Lorem ipsum 1</a>
                </h3>

                <p class="s_price">250,00 € </p>


            </div>
        </div>


        <div class="col-lg-4 col-md-6 col-sm-6 col-xs-12 product-item">
            <div class="product-item_inner text-center">
                <a class="s_thumb" href="#">
                    <img width="280" height="240" src="http://lorempixel.com/280/240/" class="img-responsive"
                         alt="title example">
                </a>
                <h3><a href="#">Lorem ipsum 2</a>
                </h3>

                <p class="s_price">250,00 € </p>

            </div>
        </div>

        <div class="col-lg-4 col-md-6 col-sm-6 col-xs-12 product-item">
            <div class="product-item_inner text-center">
                <a class="s_thumb" href="#">
                    <img width="280" height="70" src="http://lorempixel.com/280/70/" class="img-responsive"
                         alt="title example">
                </a>
                <h3><a href="#">Lorem ipsum 3</a>
                </h3>

                <p class="s_price">250,00 € </p>

            </div>
        </div>

        <div class="col-lg-4 col-md-6 col-sm-6 col-xs-12 product-item">
            <div class="product-item_inner text-center">
                <a class="s_thumb" href="#">
                    <img width="280" height="140" src="http://lorempixel.com/280/140/" class="img-responsive"
                         alt="title example">
                </a>
                <h3><a href="#">Lorem ipsum 4</a>
                </h3>

                <p class="s_price">250,00 € </p>

            </div>
        </div>

        <div class="col-lg-4 col-md-6 col-sm-6 col-xs-12 product-item">
            <div class="product-item_inner text-center">
                <a class="s_thumb" href="#">
                    <img width="280" height="150" src="http://lorempixel.com/280/150/" class="img-responsive"
                         alt="title example">
                </a>
                <h3><a href="#">Lorem ipsum 5</a>
                </h3>

                <p class="s_price">250,00 € </p>

            </div>
        </div>

        <div class="col-lg-4 col-md-6 col-sm-6 col-xs-12 product-item">
            <div class="product-item_inner text-center">
                <a class="s_thumb" href="#">
                    <img width="280" height="205" src="http://lorempixel.com/280/205/" class="img-responsive"
                         alt="title example">
                </a>
                <h3><a href="#">Lorem ipsum 6</a>
                </h3>

                <p class="s_price">250,00 € </p>

            </div>
        </div>


    </div>
</div>


</div>


<script src="js/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/scripts.js"></script>
</body>
</html>

4 个答案:

答案 0 :(得分:0)

您可以使用CSS3列。

要做到这一点,请删除所有col-xx div个,并将product-item课程分配给row div

然后剩下的就是将这种风格添加到product-item

.product-item { -webkit-column-count: 3; column-count: 3; }

演示小提琴http://jsfiddle.net/abhitalks/o8mksrc2/1/

演示代码段

.product-item { -webkit-column-count: 3; column-count: 3; }
.product-item_inner {
    background-color: yellow;
    margin-bottom: 20px; padding: 10px;
    width:auto; height:auto;
    -webkit-column-break-inside: avoid;
}
a.s_thumb img { margin-left: auto; margin-right: auto; }
div.product-item h3 { font-size: 15px; }
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
    <div class="row product-item">
        <div class="product-item_inner text-center">
            <a class="s_thumb" href="#">
                <img width="280" height="140" src="http://lorempixel.com/280/140/" 
                     class="img-responsive" alt="title example" />
            </a>
            <h3><a href="#">Lorem ipsum 1</a></h3>
            <p class="s_price">250,00 € </p>
        </div>        
        <div class="product-item_inner text-center">
            <a class="s_thumb" href="#">
                <img width="280" height="240" src="http://lorempixel.com/280/240/" 
                     class="img-responsive" alt="title example" />
            </a>
            <h3><a href="#">Lorem ipsum 2</a> </h3>
            <p class="s_price">250,00 € </p>
        </div>
        <div class="product-item_inner text-center">
            <a class="s_thumb" href="#">
                <img width="280" height="70" src="http://lorempixel.com/280/70/" 
                     class="img-responsive" alt="title example" />
            </a>
            <h3><a href="#">Lorem ipsum 3</a></h3>
            <p class="s_price">250,00 € </p>
        </div>        
        <div class="product-item_inner text-center">
            <a class="s_thumb" href="#">
                <img width="280" height="140" src="http://lorempixel.com/280/140/" 
                     class="img-responsive" alt="title example" />
            </a>
            <h3><a href="#">Lorem ipsum 4</a></h3>
            <p class="s_price">250,00 € </p>
        </div>        
        <div class="product-item_inner text-center">
            <a class="s_thumb" href="#">
                <img width="280" height="150" src="http://lorempixel.com/280/150/" 
                     class="img-responsive" alt="title example" />
            </a>
            <h3><a href="#">Lorem ipsum 5</a></h3>
            <p class="s_price">250,00 € </p>
        </div>        
        <div class="product-item_inner text-center">
            <a class="s_thumb" href="#">
                <img width="280" height="205" src="http://lorempixel.com/280/205/" 
                     class="img-responsive" alt="title example" />
            </a>
            <h3><a href="#">Lorem ipsum 6</a></h3>
            <p class="s_price">250,00 € </p>
        </div>        
    </div>
</div>
    
    

答案 1 :(得分:0)

Abhitalks响应速度更快。要获得响应,只需在您的css中添加Media Queries引导程序:

@media (max-width:480px) {
    .product-item { -webkit-column-count: 1; column-count: 1; }
}

请参阅此处的值:http://getbootstrap.com/css/#grid-media-queries

答案 2 :(得分:0)

another question已经问过(由我)。那时用户建议 Masonry 这是一个jQuery插件,可以返回您想要的行为,完全响应

使用此插件,元素在容器中绝对定位,基于它们的大小,如果调整浏览器窗口大小,则会重新计算此位置,以便不仅在页面加载时保持完全响应而且还在窗口调整大小。

基于“ columns ”的CSS解决方案可能是一个完美而轻松的解决方案,但您必须记住它们是compatible only since IE10及以上。

此外,使用Masonry,各种元素按行从左到右排序,与一个简单的BootStrap网格非常相似,而使用CSS列,所有项目首先填充第一列,然后第二列......依此类推...... 所以他们在页面上的排序标准会发生变化。

答案 3 :(得分:0)

尝试;

Qlikview