简体   繁体   English

使用像素和百分比约束的 HTML 自动图像大小调整

[英]HTML automatic image resize using pixel and percent constraints

How can I add an image to an HTML page using the following constraints:如何使用以下约束将图像添加到 HTML 页面:

  • No upscaling allowed ( if the picture width is 400 pixels I don't want to resize it to 600).不允许放大(如果图片宽度为 400 像素,我不想将其调整为 600)。
  • The image should be downscaled if the containing element is smaller than the image.如果包含元素小于图像,则应缩小图像。
  • Keep aspect ratio.保持长宽比例。

For instance I have an image (400x300).例如我有一个图像(400x300)。 If the containing element is 600 pixels wide I'd like to show the image at 400x300.如果包含元素的宽度为 600 像素,我想以 400x300 显示图像。 If the containing element is 200 pixels wide ( for instance it's a mobile browser ) I'd like to show the image at 200x150.如果包含元素的宽度为 200 像素(例如它是移动浏览器),我想以 200x150 显示图像。

I'm looking for a pure CSS solution (if it's possible) without hard wiring the image size.我正在寻找一个纯 CSS 解决方案(如果可能的话),而无需硬连接图像大小。

Using the max-width property twice works correctly:使用 max-width 属性两次可以正常工作:

<div style="max-width: 400px" >
<img src="image.jpg" style="max-width:100%;" />
</div>

You should use the max-width property:您应该使用 max-width 属性:

<div style="width:600px">
<img src="images/image.jpg" alt="image" style="max-width:100%;"/>    
</div>

This will constrain your image width to the parent div element, down scaling the image if necessary.这会将您的图像宽度限制为父 div 元素,必要时缩小图像。 It will not lose its aspect ratio or up scale an image.它不会丢失其纵横比或放大图像。 max-width is supported in all up to date browsers but not in IE6所有最新浏览器都支持 max-width,但 IE6 不支持

It does not downscale the image.它不会缩小图像。 – asalamon74 – asalamon74

I plugged AdamY's code into my HTML and it does downscale the image.我将 AdamY 的代码插入到我的 HTML 中,它确实缩小了图像的尺寸。 Here's some samples of the image dimensions I've got: 800w x 626h, 962x640, 895x640.这是我得到的一些图像尺寸示例:800w x 626h、962x640、895x640。 For these and others, it properly scaled them all - meaning after adding 'style="max-width:100%;"' to the img tag, the images fit within the width of my container (#photo-container) and are to scale.对于这些和其他人,它正确地缩放了它们 - 这意味着在将“style="max-width:100%;"”添加到 img 标签后,图像适合我的容器 (#photo-container) 的宽度,并且规模。 Not saying it will work in your code, but it worked for me.并不是说它会在你的代码中工作,但它对我有用。 Here's my relevant CSS/HTML code:这是我的相关 CSS/HTML 代码:

CSS: CSS:

#photo-container {display:block; float:left; width: 800px; 
                    margin-left:5px; margin-top:0px;
                    padding: 10px 10px 10px 10px;
                    background-color:black; color:white;
                    border-bottom-left-radius: 0.5em;
                    border-bottom-right-radius: 0.5em;
                }  

HTML: HTML:

    <div id="photo-container">
        <img id="photo-display" src="PlaceHolder" alt="PlaceHolder"  
         style="max-width:100%;">
    </div>

The images are actually placed using javascript.这些图像实际上是使用 javascript 放置的。 The selection process in my javascript code is a little verbose for this topic, but essentially it equates to:对于这个主题,我的 javascript 代码中的选择过程有点冗长,但本质上它等同于:

photo-display.src=myimage;

I have not tried it with a photo with a width that is less than 800.宽度小于800的照片我没试过。

需要注意的是,您的 IMG 标签需要移除 Width & Height 属性,否则将无法正确缩放。

By tag < img > without using div boxes etc.通过标签 <img> 不使用 div 框等。

img {
width: auto;
height: auto;
max-width: 100%;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM