如何自动调整移动网站的图片大小?

Ric*_*ard 23 html css image

我尝试了谷歌搜索,仍然无法弄清楚如何根据各种移动设备的宽度调整图像大小.这是我的尝试:

CSS:

img.test {
    width: 100%;
    height: auto;
}
Run Code Online (Sandbox Code Playgroud)

HTML:

<html>
    <head>
        <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
        <meta content="width=device-width; initial-scale=1.0; maximum-scale=1.0;   user-scalable=0;" name="viewport">
        <link rel="stylesheet" href="../includes/style.css">
    </head>
    <img class="test"  src="../includes/foo.jpg">
Run Code Online (Sandbox Code Playgroud)

但是图像仍在加载其原始缩放.我是一个CSS和HTML新手,所以任何帮助都会很棒!

编辑:

谢谢你的回复.这是一个现在有效的修订版本.

HTML:

   <html>
    <head>
        <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
        <meta content="width=device-width; initial-scale=1.0; maximum-scale=1.0;  user-scalable=0;" name="viewport">
        <link rel="stylesheet" href="../includes/style.css">
    </head>
    <body width = "device-width">
        <img class="test"  src="../includes/buoy_map.jpg">
    </body>
Run Code Online (Sandbox Code Playgroud)

Doo*_*ake 44

img {
  max-width: 100%;
}
Run Code Online (Sandbox Code Playgroud)

应该将图像设置为占据其包含元素的100%.

  • 在这种情况下,它应该包括高度:auto; (4认同)

小智 18

img
{
    max-width: 100%;
    min-width: 300px;
    height: auto;
}
Run Code Online (Sandbox Code Playgroud)


Cha*_*a D 9

您可以使用以下 css 调整移动视图的图像大小

object-fit: scale-down; max-width: 100%
Run Code Online (Sandbox Code Playgroud)


Lin*_*nce 7

如果你使用bootstrap 3,只需在你的img标签中添加img-responsive

<img class="img-responsive"  src="...">
Run Code Online (Sandbox Code Playgroud)

如果你使用bootstrap 4,在你的img标签中添加img-fluid

<img class="img-fluid"  src="...">
Run Code Online (Sandbox Code Playgroud)

工作人员:max-width:100%,height:auto,display:block to the image


kon*_*ddy 1

您的 css 没有任何效果,因为外部元素没有定义宽度(并且主体也丢失了)。

另一种方法是提供已经缩放的图像。例如, http://www.sencha.com/products/io/提供的图像已根据查看设备按比例缩小。