CSS:100% 宽度或高度,同时保持纵横比?
目前,使用 STYLE,我可以在高度上使用 width: 100%
和 auto
(反之亦然),但我仍然无法将图像限制在特定位置,分别是太宽或太高。
有什么想法吗?
Currently, with STYLE, I can use width: 100%
and auto
on the height (or vice versa), but I still can't constrain the image into a specific position, either being too wide or too tall, respectively.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(17)
如果您仅在图像上定义一个维度,则图像长宽比将始终保留。
问题是图像比您喜欢的更大/更高吗?
您可以将其放在一个 DIV 中,该 DIV 设置为您想要的图像最大高度/宽度,然后设置溢出:隐藏。这会裁剪超出您想要的任何内容。
如果图像的宽度和高度为 100%:自动,并且您认为它太高,那是因为纵横比被保留。您需要裁剪或更改纵横比。
请提供更多有关您具体想要实现的目标的信息,我将尽力提供更多帮助!
--- 根据反馈进行编辑 ---
您熟悉 max-width 和 max-height 属性?你总是可以设置这些。如果您没有设置任何最小值并设置了最大高度和宽度,那么您的图像将不会变形(将保留纵横比),并且它不会大于最长的尺寸并达到其最大值。
If you only define one dimension on an image the image aspect ratio will always be preserved.
Is the issue that the image is bigger/taller than you prefer?
You could put it inside a DIV that is set to the maximum height/width that you want for the image, and then set overflow:hidden. That would crop anything beyond what you want.
If an image is 100% wide and height:auto and you think it's too tall, that is specifically because the aspect ratio is preserved. You'll need to crop, or to change the aspect ratio.
Please provide some more information about what you're specifically trying to accomplish and I'll try to help more!
--- EDIT BASED ON FEEDBACK ---
Are you familiar with the max-width and max-height properties? You could always set those instead. If you don't set any minimum and you set a max height and width then your image will not be distorted (aspect ratio will be preserved) and it will not be any larger than whichever dimension is longest and hits its max.
几年后,在寻找相同的需求时,我发现了一个使用背景大小的 CSS 选项。
它应该可以在现代浏览器(IE9+)中工作。
样式:
参考:http://www.w3schools.com/cssref/css3_pr_background-size.asp
演示:http://www.w3schools.com/cssref/playit.asp?文件名=playcss_background-size
Some years later, looking for the same requirement, I found a CSS option using background-size.
It is supposed to work in modern browsers (IE9+).
And the style:
The reference: http://www.w3schools.com/cssref/css3_pr_background-size.asp
And the demo: http://www.w3schools.com/cssref/playit.asp?filename=playcss_background-size
简单优雅的工作解决方案:
Simple elegant working solution:
通过将 CSS
max-width
属性设置为100%
,图像将填充其父元素的宽度,但不会呈现大于其实际大小的尺寸,从而保留解决。将
height
属性设置为auto
可以保持图像的纵横比,使用此技术可以覆盖静态高度,并使图像能够在所有方向上按比例弯曲。By setting the CSS
max-width
property to100%
, an image will fill the width of it's parenting element, but won’t render larger than it's actual size, thus preserving resolution.Setting the
height
property toauto
maintains the aspect ratio of the image, using this technique allows static height to be overridden and enables the image to flex proportionally in all directions.有同样的问题。对我来说,问题是 height 属性也已经在其他地方定义了,修复如下:
Had the same issue. The problem for me was that the height property was also already defined elsewhere, fixed it like this:
简单的解决方案:
顺便说一句,如果你想将它放在父 div 容器中,你可以添加这些 css 属性:
它应该真正按预期工作:)
Simple solution:
By the way, if you want to center it in a parent div container, you can add those css properties:
It should really work as expected :)
要尝试 100% 的宽度并强制使用最大高度而不扭曲纵横比,您可以添加一个
max-height
声明并结合 <代码>对象适合:包含。To attempt a width of 100% and force a maximum height without distorting the aspect ratio, you can add a
max-height
declaration combined withobject-fit:contain
.答案之一包括我非常喜欢的 background-size: contains css 语句,因为它简单地说明了您想要执行的操作,并且浏览器会执行它。
不幸的是,迟早您将需要应用阴影,不幸的是,它不能很好地与背景图像解决方案配合使用。
假设您有一个响应式容器,但它具有明确定义的最小和最大宽度和高度边界。
并且容器有一个 img,它必须知道容器高度的像素,以避免获得溢出或被裁剪的非常高的图像。
img 还应该定义 100% 的最大宽度,首先是为了允许渲染较小的宽度,其次是基于百分比,这使得它成为参数化的。
请注意,它有一个漂亮的阴影;)
在这个小提琴上享受一个生动的例子: http://jsfiddle.net /pligor/gx8qthqL/2/
One of the answers includes the background-size: contain css statement which I like a lot because it is straightforward statement of what you want to do and the browser just does it.
Unfortunately sooner or later you are going to need to apply a shadow which unfortunately will not play well with background image solutions.
So let's say that you have a container which is responsive but it has well defined boundaries for min and max width and height.
And the container has an img which must be aware of the pixels of the height of the container in order to avoid getting a very high image which overflows or is cropped.
The img should also define a max-width of 100% in order first to allow smaller widths to be rendered and secondly to be percentage based which makes it parametric.
Note that it has a nice shadow ;)
Enjoy a live example at this fiddle: http://jsfiddle.net/pligor/gx8qthqL/2/
现在可以使用
vw
和vh
单位,它们代表 viewport 的 width 和 1% >h分别为八。https://css-tricks.com/fun-viewport-units/
所以,例如:
... 将使图像尽可能宽,保持宽高比,但不会比视口的 100% 更宽或更高。
Nowadays one can use
vw
andvh
units, which represent 1% of the viewport's width and height respectively.https://css-tricks.com/fun-viewport-units/
So, for example:
... will make the image as wide as tall as possible, maintaining aspect ratio, but without being wider or higher than 100% of the viewport.
我将其用于高度和宽度固定但图像尺寸不同的矩形容器。
I use this for a rectangular container with height and width fixed, but with images of different sizes.
最好在应尊重纵横比的尺寸上使用 auto。如果您不将其他属性设置为 auto,现在大多数浏览器都会假设您想要尊重宽高比,但不是所有浏览器(例如,Windows Phone 8 上的 IE10 就不会)
Its best to use auto on the dimension that should respect the aspect ratio. If you do not set the other property to auto, most browsers nowadays will assume that you want to respect the aspect ration, but not all of them (IE10 on windows phone 8 does not, for example)
这是一个非常简单的解决方案,是我在关注 conca 的链接并查看背景大小后想到的。它将图像放大,然后将其置于外部容器的中心,而不进行缩放。
This is a very straightforward solution that I came up with after following conca's link and looking at background size. It blows the image up and then fits it centered into the outer container w/o scaling it.
不是要跳到一个老问题,但是...
尽管这不合适,因为您在高度上使用 !important 覆盖,但如果您使用像 WordPress 这样的 CMS 来为您设置高度和宽度,那么这很有效。
Not to jump into an old issue, but...
Even though this is not proper as you use the !important override on the height, if you're using a CMS like WordPress that sets the height and width for you, this works well.
现代方法使用纵横比。
如果父级具有明确的尺寸,则可以使用嵌套纵横比来约束宽度和高度。
例如,如果有一个全屏页面,并且想要在其中间有一个方形棋盘,则可以使用此技术。
The modern approach uses
aspect-ratio
.If one has a parent with a well-defined size, then nesting
aspect-ratio
can be used to constrain to both the width and the height.For example, if one has a fullscreen page, and wants a square chess-board in the middle of it, then this technique can be used.
使用 JQuery 左右,因为 CSS 是一种普遍的误解(这里关于简单设计目标的无数问题和讨论表明了这一点)。
CSS 不可能做你想要的事情:图像的宽度应为 100%,但如果此宽度导致高度太大,则应应用最大高度 - 当然,正确的比例应为保存下来。
Use JQuery or so, as CSS is a general misconception (the countless questions and discussions here about simple design goals show that).
It is not possible with CSS to do what you seem to wish: image shall have width of 100%, but if this width results in a height that is too large, a max-height shall apply - and of course the correct proportions shall be preserved.
自 2023 年以来最简单的答案是使用 CSS 容器查询。这些让您说“这个元素是一个容器”,然后“使用容器的宽度/高度”。
The simplest answer since 2023 is using the CSS container queries. These let you say "this element is a container" and then "use the width/height of the container".
我想这就是你在寻找的东西,我自己也在寻找它,但后来我在找到代码之前又想起了它。
数字化变革正在进行中。
I think this is what your looking for, i was looking for it my self, but then i remembered it again befor i found the code.
digital evolution is on its way.