2023年6月21日发(作者:)
CSS强制图像调整⼤⼩并保持纵横⽐本⽂翻译⾃:I am working with images, and I ran across a problem with aspect ratios. 我正在使⽤图像,我遇到了纵横⽐问题。我正在使⽤图像,我遇到了纵横⽐问题。As you can see,
height and
width are already specified. 如您所见,已指定如您所见,已指定height和width 。 。I added CSS rule for images: 我为图我为图⽚添加了CSS规则:img { max-width:500px;}But for
big_ , I receive
width=500 and
height=600 . 但是对于但是对于big_ ,我收到width=500和height=600 。 。How Ican set images to be re-sized, while keeping their aspect ratios. 我如何设置图像以重新调整⼤⼩,同时保持其纵横⽐。我如何设置图像以重新调整⼤⼩,同时保持其纵横⽐。#1楼#2楼Remove the "height" property. 删除“⾼度”属性。删除“⾼度”属性。By specifying both you are changing the aspect ratio of the image. 通过指定两者,您正在更改图像的纵横⽐。通过指定两者,您正在更改图像的纵横⽐。
Just settingone will resize but preserve the aspect ratio. 只需设置⼀个将调整⼤⼩但保留纵横⽐。只需设置⼀个将调整⼤⼩但保留纵横⽐。Optionally, to restrict oversizings: (可选)限制夸⼤:(可选)限制夸⼤:#3楼There is no standard way to preserve aspect ratio for images with
width ,
height and
max-width specified together. 没有标准⽅没有标准⽅法来保持width , height和max-width⼀起指定的图像的宽⾼⽐。So we are forced either to specify
width and
height to prevent page “jumps” during loading images, or to use
max-width andnot specify dimensions for images. 所以我们被迫指定所以我们被迫指定width和height以防⽌在加载图像期间页⾯“跳跃”,或者使⽤max-width⽽不指定图像的尺⼨。Specifying just
width (without
height ) typically makes not much sense, but you can try to override the
height HTML-attributeby adding a rule like
IMG {height: auto; } 指定只是指定只是width (没有height )通常没有多⼤意义,但您可以尝试通过添加IMG {height:auto; }等规则来覆盖height HTML属性IMG {height: auto; } IMG {height: auto; } into your stylesheet. IMG {height: auto; }到你的样式表。See also the related Firefox . 另请参阅相关的Firefox 另请参阅相关的Firefox 。。#4楼The background-size property is ie>=9 only, but if that is fine with you, you can use a div with
background-image and setbackground-size: contain : background-size属性仅为> = 9,但如果你没问题,可以使⽤带background-size属性仅为> = 9,但如果你没问题,可以使⽤带background-image的div并设置background-size: contain :{ background-image: url("your/url/here"); background-size: contain; background-repeat: no-repeat; background-position: center;}Now you can just set your div size to whatever you want and not only will the image keep its aspect ratio it will also becentralized both vertically and horizontally within the div. 现在您可以将div⼤⼩设置为您想要的任何值,不仅图像保持其宽⾼现在您可以将div⼤⼩设置为您想要的任何值,不仅图像保持其宽⾼⽐,它还将在div中垂直和⽔平集中。 ⽐,它还将在div中垂直和⽔平集中。Just don't forget to set the sizes on the css since divs don't have the width/heightattribute on the tag itself. 只是不要忘记在css上设置⼤⼩,因为div在标签本⾝上没有width / height属性。只是不要忘记在css上设置⼤⼩,因为div在标签本⾝上没有width / height属性。This approach is different than setecs answer, using this the image area will be constant and defined by you (leaving emptyspaces either horizontally or vertically depending on the div size and image aspect ratio), while setecs answer will get you abox that exactly the size of the scaled image (without empty spaces). 这种⽅法与setecs的答案不同,使⽤此⽅法,图像区域将这种⽅法与setecs的答案不同,使⽤此⽅法,图像区域将是恒定的并由您定义(根据div⼤⼩和图像宽⾼⽐在⽔平或垂直⽅向留下空⽩区域),⽽setecs答案将为您提供⼀个完全相同的⽅框缩放图像的⼤⼩(没有空格)。Edit: According to the you can simulate the background-size property in IE8 using a proprietary filter declaration: 编辑:根据编辑:根据您可以使⽤专有过滤器声明模拟IE8中的background-size属性:Though Internet Explorer 8 doesn't support the background-size property, it is possible to emulate some of itsfunctionality using the non-standard -ms-filter function: 虽然Internet Explorer 8不⽀持background-size属性,但可虽然Internet Explorer 8不⽀持background-size属性,但可以使⽤⾮标准-ms-filter函数模拟其某些功能:-ms-filter: "progid:mageLoader(src='path_relative_to_the_HTML_file', sizingMethod='scale')";#5楼
img { display: block; max-width:230px; max-height:95px; width: auto; height: auto; }
This image is originally 400x400 pixels, but should get resized by the CSS:
This will make image shrink if it's too big for specified area (as downside, it will not enlarge image). 如果图像对于指定区域来如果图像对于指定区域来说太⼤(这将使图像缩⼩,则会使图像缩⼩)。#6楼The solutions below will allow scaling up and scaling down of the image , depending on the parent box width. 下⾯的解决⽅案下⾯的解决⽅案将允许放⼤和缩⼩图像 ,具体取决于⽗框宽度。All images have a parent container with a fixed width for demonstration purposes only . 所有图像都有⼀个具有固定宽度的⽗所有图像都有⼀个具有固定宽度的⽗容器,
仅⽤于演⽰⽬的 。 。In production, this will be the width of the parent box. 在⽣产中,这将是⽗框的宽度。在⽣产中,这将是⽗框的宽度。Best Practice (2018): 最佳实践(2018年):This solution tells the browser to render the image with max available width and adjust the height as a percentage of thatwidth. 此解决⽅案告诉浏览器渲染具有最⼤可⽤宽度的图像,并将⾼度调整为该宽度的百分⽐。此解决⽅案告诉浏览器渲染具有最⼤可⽤宽度的图像,并将⾼度调整为该宽度的百分⽐。
.parent { width: 100px; } img { display: block; width: 100%; height: auto; }
This image is originally 400x400 pixels, but should get resized by the CSS:
Fancier Solution: 发烧友解决⽅案:With the fancier solution, you'll be able to crop the image regardless of its size and add a background color to compensatefor the cropping. 使⽤更⾼级的解决⽅案,您将能够裁剪图像,⽆论其⼤⼩如何,并添加背景颜⾊以补偿裁剪。使⽤更⾼级的解决⽅案,您将能够裁剪图像,⽆论其⼤⼩如何,并添加背景颜⾊以补偿裁剪。
.parent { width: 100px; } .container { display: block; width: 100%; height: auto; position: relative; overflow: hidden; padding: 34.37% 0 0 0; /* 34.37% = 10
This image is originally 640x220, but should get resized by the CSS:
For the line specifying padding, you need to calculate the aspect ratio of the image, for example: 对于指定填充的⾏,您需要对于指定填充的⾏,您需要计算图像的纵横⽐,例如:640px (w) = 100%220px (h) = ?640/220 = 2.909100/2.909 = 34.37%So, top padding = 34.37%. 因此,顶部填充= 34.37%。因此,顶部填充= 34.37%。
2023年6月21日发(作者:)
CSS强制图像调整⼤⼩并保持纵横⽐本⽂翻译⾃:I am working with images, and I ran across a problem with aspect ratios. 我正在使⽤图像,我遇到了纵横⽐问题。我正在使⽤图像,我遇到了纵横⽐问题。As you can see,
height and
width are already specified. 如您所见,已指定如您所见,已指定height和width 。 。I added CSS rule for images: 我为图我为图⽚添加了CSS规则:img { max-width:500px;}But for
big_ , I receive
width=500 and
height=600 . 但是对于但是对于big_ ,我收到width=500和height=600 。 。How Ican set images to be re-sized, while keeping their aspect ratios. 我如何设置图像以重新调整⼤⼩,同时保持其纵横⽐。我如何设置图像以重新调整⼤⼩,同时保持其纵横⽐。#1楼#2楼Remove the "height" property. 删除“⾼度”属性。删除“⾼度”属性。By specifying both you are changing the aspect ratio of the image. 通过指定两者,您正在更改图像的纵横⽐。通过指定两者,您正在更改图像的纵横⽐。
Just settingone will resize but preserve the aspect ratio. 只需设置⼀个将调整⼤⼩但保留纵横⽐。只需设置⼀个将调整⼤⼩但保留纵横⽐。Optionally, to restrict oversizings: (可选)限制夸⼤:(可选)限制夸⼤:#3楼There is no standard way to preserve aspect ratio for images with
width ,
height and
max-width specified together. 没有标准⽅没有标准⽅法来保持width , height和max-width⼀起指定的图像的宽⾼⽐。So we are forced either to specify
width and
height to prevent page “jumps” during loading images, or to use
max-width andnot specify dimensions for images. 所以我们被迫指定所以我们被迫指定width和height以防⽌在加载图像期间页⾯“跳跃”,或者使⽤max-width⽽不指定图像的尺⼨。Specifying just
width (without
height ) typically makes not much sense, but you can try to override the
height HTML-attributeby adding a rule like
IMG {height: auto; } 指定只是指定只是width (没有height )通常没有多⼤意义,但您可以尝试通过添加IMG {height:auto; }等规则来覆盖height HTML属性IMG {height: auto; } IMG {height: auto; } into your stylesheet. IMG {height: auto; }到你的样式表。See also the related Firefox . 另请参阅相关的Firefox 另请参阅相关的Firefox 。。#4楼The background-size property is ie>=9 only, but if that is fine with you, you can use a div with
background-image and setbackground-size: contain : background-size属性仅为> = 9,但如果你没问题,可以使⽤带background-size属性仅为> = 9,但如果你没问题,可以使⽤带background-image的div并设置background-size: contain :{ background-image: url("your/url/here"); background-size: contain; background-repeat: no-repeat; background-position: center;}Now you can just set your div size to whatever you want and not only will the image keep its aspect ratio it will also becentralized both vertically and horizontally within the div. 现在您可以将div⼤⼩设置为您想要的任何值,不仅图像保持其宽⾼现在您可以将div⼤⼩设置为您想要的任何值,不仅图像保持其宽⾼⽐,它还将在div中垂直和⽔平集中。 ⽐,它还将在div中垂直和⽔平集中。Just don't forget to set the sizes on the css since divs don't have the width/heightattribute on the tag itself. 只是不要忘记在css上设置⼤⼩,因为div在标签本⾝上没有width / height属性。只是不要忘记在css上设置⼤⼩,因为div在标签本⾝上没有width / height属性。This approach is different than setecs answer, using this the image area will be constant and defined by you (leaving emptyspaces either horizontally or vertically depending on the div size and image aspect ratio), while setecs answer will get you abox that exactly the size of the scaled image (without empty spaces). 这种⽅法与setecs的答案不同,使⽤此⽅法,图像区域将这种⽅法与setecs的答案不同,使⽤此⽅法,图像区域将是恒定的并由您定义(根据div⼤⼩和图像宽⾼⽐在⽔平或垂直⽅向留下空⽩区域),⽽setecs答案将为您提供⼀个完全相同的⽅框缩放图像的⼤⼩(没有空格)。Edit: According to the you can simulate the background-size property in IE8 using a proprietary filter declaration: 编辑:根据编辑:根据您可以使⽤专有过滤器声明模拟IE8中的background-size属性:Though Internet Explorer 8 doesn't support the background-size property, it is possible to emulate some of itsfunctionality using the non-standard -ms-filter function: 虽然Internet Explorer 8不⽀持background-size属性,但可虽然Internet Explorer 8不⽀持background-size属性,但可以使⽤⾮标准-ms-filter函数模拟其某些功能:-ms-filter: "progid:mageLoader(src='path_relative_to_the_HTML_file', sizingMethod='scale')";#5楼
img { display: block; max-width:230px; max-height:95px; width: auto; height: auto; }
This image is originally 400x400 pixels, but should get resized by the CSS:
This will make image shrink if it's too big for specified area (as downside, it will not enlarge image). 如果图像对于指定区域来如果图像对于指定区域来说太⼤(这将使图像缩⼩,则会使图像缩⼩)。#6楼The solutions below will allow scaling up and scaling down of the image , depending on the parent box width. 下⾯的解决⽅案下⾯的解决⽅案将允许放⼤和缩⼩图像 ,具体取决于⽗框宽度。All images have a parent container with a fixed width for demonstration purposes only . 所有图像都有⼀个具有固定宽度的⽗所有图像都有⼀个具有固定宽度的⽗容器,
仅⽤于演⽰⽬的 。 。In production, this will be the width of the parent box. 在⽣产中,这将是⽗框的宽度。在⽣产中,这将是⽗框的宽度。Best Practice (2018): 最佳实践(2018年):This solution tells the browser to render the image with max available width and adjust the height as a percentage of thatwidth. 此解决⽅案告诉浏览器渲染具有最⼤可⽤宽度的图像,并将⾼度调整为该宽度的百分⽐。此解决⽅案告诉浏览器渲染具有最⼤可⽤宽度的图像,并将⾼度调整为该宽度的百分⽐。
.parent { width: 100px; } img { display: block; width: 100%; height: auto; }
This image is originally 400x400 pixels, but should get resized by the CSS:
Fancier Solution: 发烧友解决⽅案:With the fancier solution, you'll be able to crop the image regardless of its size and add a background color to compensatefor the cropping. 使⽤更⾼级的解决⽅案,您将能够裁剪图像,⽆论其⼤⼩如何,并添加背景颜⾊以补偿裁剪。使⽤更⾼级的解决⽅案,您将能够裁剪图像,⽆论其⼤⼩如何,并添加背景颜⾊以补偿裁剪。
.parent { width: 100px; } .container { display: block; width: 100%; height: auto; position: relative; overflow: hidden; padding: 34.37% 0 0 0; /* 34.37% = 10
This image is originally 640x220, but should get resized by the CSS:
For the line specifying padding, you need to calculate the aspect ratio of the image, for example: 对于指定填充的⾏,您需要对于指定填充的⾏,您需要计算图像的纵横⽐,例如:640px (w) = 100%220px (h) = ?640/220 = 2.909100/2.909 = 34.37%So, top padding = 34.37%. 因此,顶部填充= 34.37%。因此,顶部填充= 34.37%。
发布评论