调整移动设备的背景图像大小

时间:2013-04-05 10:01:26

标签: html css image responsive-design

就在那时,我一直在玩我的网站,并在一些图片上添加一个“新”横幅,我这样做是我能想到的最简单的方法,如下所示..

<img style="background:url(images/pic3.jpg)" src="images/new.png" alt="" /></a>

现在这在桌面上查看时工作正常,因为图像已经是网站的正确尺寸,但是当我在移动设备上查看时,.png图像会缩小,但不会缩小背景图像..

我知道我遗失了一些来自CSS的东西,但是当我还在学习的时候,我不确切地知道它遗失了什么,这就是你们进来的地方..

这里有一点我的HTML ..

<div class="thumbnail">
    <a href="#">
        <img style="background:url(images/pic3.jpg)" src="images/new.png" alt="" />
             </a>
                 </div>

这是有问题的移动css ..

.thumbnails
{
}

    .thumbnails .thumbnail
    {
        border-top: solid 1px #e5e5e5;
        padding-top: 2em;
        margin-top: 2em;
    }

    .thumbnails .first
    {
        border-top: 0;
        padding-top: 0;
        margin-top: 0;
    }

    .thumbnails .thumbnail img
        {
            margin-top: 5px;
            height: 180px;
            margin-right: 8px;
        }

    .thumbnails .thumbnail blockquote
        {

            margin-left: 143px;
        }

所以基本上,我只需要知道,如何使背景图像像前景图像一样缩小(.png图像)

我相信有人可以提供帮助,所以非常感谢。

编辑:这是一个快速的小提琴,所以你可以看到发生了什么...... http://jsfiddle.net/bBMdp/

由于

3 个答案:

答案 0 :(得分:5)

试试这个:

.thumbnails .thumbnail img{
    margin-top: 5px;
    height: 180px;
    margin-right: 8px;

    background-size: 100% 100%;

}

或者:

.thumbnails .thumbnail img{
    margin-top: 5px;
    height: 180px;
    margin-right: 8px;

    background-size: cover;

}

<强>来源(S)

MDN - background-size - CSS

答案 1 :(得分:1)

使用background-size:XXpx XXpx;第一个值是取宽度,第二个值是取高度。

答案 2 :(得分:0)

-webkit-background-style: cover;
-moz-background-style: cover;
-o-background-size: cover;
background-style: cover;

这将缩小背景,因此它只能覆盖整个盒子。