2

我有这个 175x175 的背景图像,但我正试图用它制作一个“CD”封面。在下面的代码中(jsFiddle 可用),您会看到它没有调整大小,而只是“裁剪”。我该如何解决?

HTML:

<div class="cd"><div class="hole"></div></div>
<p>I want the image above to be resized to 75x75... but it's not</p>

<img src="http://userserve-ak.last.fm/serve/126/23679395.jpg" alt="Test" />
<p>Actual image size above.</p>

CSS:

.cd {
    -moz-border-radius: 63px;
    -webkit-border-radius: 63px;
    border-radius: 63px;
    background-image: url('http://userserve-ak.last.fm/serve/126/23679395.jpg');
    width: 75px;
    height: 75px;
    position: relative;
    border:1px solid #A1A1A1;
}
.cd .hole {
    width: 20px;
    height: 20px;
    position: absolute;
    background-color: #ddd;
    border:1px solid #A1A1A1;
    left: 28px;
    top: 28px;
    -moz-border-radius: 15px;
    -webkit-border-radius: 15px;
    border-radius: 15px;
}
4

3 回答 3

6

使用background-size CSS 属性:

background-size: 75px 75px;

或者:

background-size: 100% 100%;

还有速记背景属性:

background: url('http://userserve-ak.last.fm/serve/126/23679395.jpg') 100% 100%;
于 2012-06-12T05:58:25.053 回答
2

像这里一样使用背景大小:

.cd {
    -moz-border-radius: 63px;
    -webkit-border-radius: 63px;
    border-radius: 63px;
    background-image: url('http://userserve-ak.last.fm/serve/126/23679395.jpg');
    background-size: 75px;
    width: 75px;
    height: 75px;
    position: relative;
    border:1px solid #A1A1A1;
}

示例:http: //jsfiddle.net/5CDnw/6/

于 2012-06-12T05:59:29.117 回答
1

使用 background-size:cover 它将填充容器,无论容器的大小是多少。

.cd {
    -moz-border-radius: 63px;
    -webkit-border-radius: 63px;
    border-radius: 63px;
    background-image: url('http://userserve-ak.last.fm/serve/126/23679395.jpg');
    background-size:cover;/*Add This*/
    -webkit-background-size: cover;/*Add This for webkit*/
    -moz-background-size: cover;/*Add This for mozilla*/
    -o-background-size: cover;/*Add This for opera*/
    width: 75px;
    height: 75px;
    position: relative;
    border:1px solid #A1A1A1;
}
于 2012-06-12T06:05:58.160 回答