将宽度和高度指定为百分比,而不扭曲 HTML 中的照片比例
我想知道在宽度和高度 属性中,我是否可以将宽度和高度指定为百分比?
嗯,我想这是显而易见的,因为当我尝试这样做时,它会调整大小,但它似乎会扭曲我的图像质量。
这是我的具有固定属性的标记的示例:
<img src="#" width="346" height="413">
现在,在尝试通过百分比缩小比例(例如一半)时:
<img src="#" width="50%" height="50%">
我得到的结果与以下内容完全不同:
<img src="#" width="173" height="206.5">
我认为我只是从根本上误解了我的百分比标记或其他东西,因为有我的第二个和第三个例子在视觉上有明显的区别。
I was wondering if in the width and height <img>
attributes, I could specify width and height as percentages?
Well, I guess that is obvious, because when I try so, it resizes, but it appears to skew the quality of my image.
Here is an example of my markup with fixed attributes:
<img src="#" width="346" height="413">
Now, while trying to scale this down, say by half, via percentages:
<img src="#" width="50%" height="50%">
I get something completely different than:
<img src="#" width="173" height="206.5">
I think I'm just fundamentally mistaking my percentage markup or something because there is a noticeable difference between my second and third example visually.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
注意:直接提供百分比作为
width
或height
属性是无效的,除非您使用 HTML 4.01(请参阅当前规范,过时规范和此答案了解更多信息细节)。话虽这么说,浏览器通常会容忍这种行为以支持向后兼容性。第二个示例中的这些百分比宽度实际上适用于您的
所在的容器,而不是图像的实际大小。假设您有以下标记:
您生成的图像将是 500 像素宽和 300 像素高。
jQuery 调整大小
如果您尝试将图像缩小到其宽度的 50%,您可以使用 jQuery 片段来实现:
只需确保首先去掉所有高度/宽度 = 50% 的属性。
Note: it is invalid to provide percentages directly as
<img>
width
orheight
attribute unless you're using HTML 4.01 (see current spec, obsolete spec and this answer for more details). That being said, browsers will often tolerate such behaviour to support backwards-compatibility.Those percentage widths in your 2nd example are actually applying to the container your
<img>
is in, and not the image's actual size. Say you have the following markup:Your resulting image will be 500px wide and 300px tall.
jQuery Resize
If you're trying to reduce an image to 50% of its width, you can do it with a snippet of jQuery:
Just make sure you take off any height/width = 50% attributes first.
您可以设置其中之一(但不能同时设置两者),这应该会得到您想要的结果。
You can set one or the other (just not both) and that should get the result you want.
区别如下:
这会将图像设置为其原始大小的一半。
这会将图像设置为其可用演示区域的一半。
例如,如果您将其作为页面上的唯一元素,它将尝试占据页面宽度的 50%,从而使其可能大于其原始大小 - 而不是您期望的原始大小的一半。
如果以大于原始尺寸的方式呈现,图像将出现严重像素化。
Here is the difference:
This sets the image to half of its original size.
This sets the image to half of its available presentation area.
For example, if you put this as the only element on the page, it would attempt to take up 50% of the width of the page, thus making it potentially larger than its original size - not half of its original size as you are expecting.
If it is being presented at larger than original size, the image will appear greatly pixelated.
尝试在 css3 中使用缩放属性:
原始值的 75%:
原始值的 50%:
Try use scale property in css3:
75% of original:
50% of original:
不确定这是否正是您想要的,但为什么不直接使用内联 CSS 呢?
例子:
Not sure if this is quite what you're looking for but why not just use inline CSS?
Example:
如果我没有记错的话, width="50%" 和 height="50%" 将宽度和高度属性设置为父元素宽度和高度的一半。如果您使用百分比,则仅设置宽度或高度也应将宽度或高度设置为父元素的百分比。
width="50%" and height="50%" sets the width and height attributes to half of the parent element's width and height if I'm not mistaken. Also setting just width or height should set the width or height to the percentage of the parent element, if you're using percents.
由于缺乏有关原始图像大小的信息,指定宽度和高度的百分比将导致结果非常不稳定。如果您试图确保图像适合页面上的特定位置,那么您将需要使用一些服务器端代码来管理重新缩放。
Given the lack of information regarding the original image size, specifying percentages for the width and height would result in highly erratic results. If you are trying to ensure that an image will fit within a specific location on your page then you'll need to use some server side code to manage that rescaling.
来自 W3Schools
包含元素的高度百分比(例如“20% ").
所以我认为它们是指 div 所在的元素?
From W3Schools
The height in percent of the containing element (like "20%").
So I think they mean the element where the div is in?
它对我来说就像这样,vh就像这里的父百分比一样。
PS。这些类是引导程序。
It worked like that with me, The vh works like percentage of parent here.
PS. Those classes are bootstrap.
实际上有一种方法可以只用 html 来做到这一点。只需设置:
There is actually a way to do this with html only. Just sets:
<img src="#" width height="50%">