响应式图像和列表

时间:2012-10-04 14:13:16

标签: css css3 responsive-design

尝试获取它,以便我将图像的最大宽度设置为308px,当浏览器缩放得更大时,列表每行会有更多项目。刚才它似乎停留在每行3个列表项目,但如果我希望它完全是reponsive,如果浏览器缩放到最小值,那么它可以是每行一个图像。

HTML

<ul>
    <li><img src="http://distilleryimage8.s3.amazonaws.com/23ddf3860e2911e29c2d22000a1e9e7e_6.jpg"></li>
    <li><img src="http://distilleryimage8.s3.amazonaws.com/23ddf3860e2911e29c2d22000a1e9e7e_6.jpg"></li>
    <li><img src="http://distilleryimage8.s3.amazonaws.com/23ddf3860e2911e29c2d22000a1e9e7e_6.jpg"></li>
    <li><img src="http://distilleryimage8.s3.amazonaws.com/23ddf3860e2911e29c2d22000a1e9e7e_6.jpg"></li>
    <li><img src="http://distilleryimage8.s3.amazonaws.com/23ddf3860e2911e29c2d22000a1e9e7e_6.jpg"></li>
    <li><img src="http://distilleryimage8.s3.amazonaws.com/23ddf3860e2911e29c2d22000a1e9e7e_6.jpg"></li>
    <li><img src="http://distilleryimage8.s3.amazonaws.com/23ddf3860e2911e29c2d22000a1e9e7e_6.jpg"></li>
    <li><img src="http://distilleryimage8.s3.amazonaws.com/23ddf3860e2911e29c2d22000a1e9e7e_6.jpg"></li>
    <li><img src="http://distilleryimage8.s3.amazonaws.com/23ddf3860e2911e29c2d22000a1e9e7e_6.jpg"></li>
    <li><img src="http://distilleryimage8.s3.amazonaws.com/23ddf3860e2911e29c2d22000a1e9e7e_6.jpg"></li>
    <li><img src="http://distilleryimage8.s3.amazonaws.com/23ddf3860e2911e29c2d22000a1e9e7e_6.jpg"></li>
    <li><img src="http://distilleryimage8.s3.amazonaws.com/23ddf3860e2911e29c2d22000a1e9e7e_6.jpg"></li>
    <li><img src="http://distilleryimage8.s3.amazonaws.com/23ddf3860e2911e29c2d22000a1e9e7e_6.jpg"></li>
    <li><img src="http://distilleryimage8.s3.amazonaws.com/23ddf3860e2911e29c2d22000a1e9e7e_6.jpg"></li>
    <li><img src="http://distilleryimage8.s3.amazonaws.com/23ddf3860e2911e29c2d22000a1e9e7e_6.jpg"></li>
</ul>

CSS

ul{
    position:relative;
    float:left;
    width:100%;
    height:100%;
    margin:45px 0 0;
}
ul li{
    width: 33.3333%;
    float:left;
}
ul li img{
    display: block;
    height: auto;
    width: 100%;
}

JSFIDDLE

3 个答案:

答案 0 :(得分:6)

您需要使用媒体查询。

只需为每个“断点”设置媒体查询并更改li宽度。

例如:

    /* Show one image on devices under 540px */
    @media screen and (max-width: 540px) { 
       ul li{
        width: 100%;
        float:left;
       }
    }

    /* Show three images on devices between 550 and 800px wide */  
    @media screen and (min-width: 540px) and (max-width: 800px) {
       ul li{
        width: 33.333%;
        float:left;
       }
    }

    /* Show five images on devices between 800px and 1180pxwide */  
    @media screen and (min-width: 800px) and (max-width: 1180px) {
       ul li{
        width: 20%;
        float:left;
       }
    }

http://jsfiddle.net/spacebeers/gfjDk/3/

答案 1 :(得分:0)

使用媒体查询(根据SpaceBeers的建议)加上列表项的min-width属性... JsFiddle

答案 2 :(得分:0)

另一个想法是使用display: inline-block;代替float: left,并为列表项设置max-width: 308px;min-width: 100px;

demo

<强> HTML

<ul>
    <li><img src="image.jpg"></li><!--
    --><li><img src="image.jpg"></li><!-- as many more list items as you need
    -->
</ul>

我写它的方式(列表项之间有注释)确实有用 - any kind of whitespace matters when using inline-block

<强> CSS

ul {
    padding: 0;
    margin: 45px 0 0;
}
ul li{
    min-width: 100px;
    width: 33.3333%;
    max-width: 308px; 
    display: inline-block;
}
ul li img{
    display: block;
    height: auto;
    width: 100%;
}