1

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

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

4

3 回答 3

7

您需要使用媒体查询。

只需为每个“断点”设置媒体查询并更改 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/

于 2012-10-04T14:18:44.133 回答
0

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

演示

HTML

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

我编写它的方式(在列表项之间带有注释)确实有一个目的 -使用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%;
}
于 2012-10-04T17:14:31.233 回答
0

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

于 2012-10-04T14:20:28.643 回答