本文介绍了CSS:100%宽度或高度,同时保持宽高比?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,使用STYLE,我可以在高度上使用 width:100% auto ),但我仍然不能将图像限制在特定的位置,分别是太宽或太高。



任何想法?

解决方案

如果您只在图片上定义一个尺寸,图片宽高比将始终保留。



您可以将其放在一个DIV中,该DIV设置为您想要的最大高度/宽度为图像,然后设置overflow:hidden。



如果图片宽度为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?

解决方案

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:100%宽度或高度,同时保持宽高比?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 15:50