简体   繁体   English

将宽度设置为 100% 但高度为默认值

[英]Setting width to 100% but height to be default

I wish to change the width of a pic to 100%.我希望将图片的宽度更改为 100%。 But don't want the height specified or affecting the width anyway and causing the image to distort.但无论如何都不希望指定高度或影响宽度并导致图像扭曲。

What is the term to null/zero a height.将高度归零/归零的术语是什么。 I have been using /* height: 68px;我一直在使用 /* height: 68px; */ as a fix in the stylesheet for now. */ 作为样式表中的修复程序。 But there must be a better way?但一定有更好的方法吗?

Before

height: 68px;
width: 482px;

After /* height: 68px; /* 高度之后:68px; */ width:100% */ 宽度:100%

Remove the height from the image all together and if the image inherits a height you can reset this using height: auto;一起从图像中删除高度,如果图像继承了高度,您可以使用height: auto;重置它height: auto; also make sure to not forget resetting the min-height and max-height properties to auto if these are inherited / set.还要确保不要忘记将min-heightmax-height属性重置为auto如果这些是继承/设置的。

The reason this works is because images maintain aspect ratio automatically so if you set a height only, the width will auto-adjust, if you set the width only, the height will auto adjust.这样做的原因是因为图像会自动保持纵横比,所以如果只设置高度,宽度会自动调整,如果只设置宽度,高度会自动调整。

That's pretty much all there is to it!这就是它的全部内容!

Also - if you're looking for more advanced image effects I'd recommend replacing the image tag with a div containing the background-image property - it will allow you to be flexible in different ways such as allowing an image to scale until it covers the entire background ( background-size: cover; ) or to scale to whatever is possible ( background-size: contain ).此外 - 如果您正在寻找更高级的图像效果,我建议您用包含background-image属性的 div 替换图像标记 - 它将使您以不同的方式灵活,例如允许图像缩放直到它覆盖整个背景( background-size: cover; )或缩放到任何可能的( background-size: contain )。

Good luck!祝你好运!

Just remove the height property from the css, for the image, and it should work.只需从 css 中删除图像的 height 属性,它应该可以工作。 You cal also set image height to auto.您还可以将图像高度设置为自动。

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

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