我想拥有一个网站,可以在其中上传不同大小的图像以显示在jquery滑块中。
我似乎无法将图像缩小(缩小)到包含div中。这就是我打算做的

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org    /TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link rel="stylesheet" type="text/css" href="Imtest.css"/>
</head>

<body>

<div id="wrapper">

<div id="im"><img src="Images/Scarpa2_1.jpg" /></div>

</div>
</body>
</html>

和CSS
#wrapper {
    width: 400px;
    height: 400px;
    border: 2px black solid;
    margin: auto;
}

#im {
    max-width: 100%;
}

我试图在CSS中将图片的最大宽度设置为100%。但这是行不通的。

最佳答案

您可以使用background image完成此操作;

从MDN-Background Size: Contain:



Demo

CSS:

#im {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-image: url("path/to/img");
    background-repeat: no-repeat;
    background-size: contain;
}

HTML:
<div id="wrapper">
    <div id="im">
    </div>
</div>

关于html - CSS缩小图像以适合包含div的大小,而不指定原始大小,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18606473/

10-12 12:36
查看更多