简体   繁体   English

高度CSS百分比不起作用

[英]Height CSS Percent Not Working

I'm trying to adjust the height of a picture by a percentage, rather than a pixel. 我试图用一个百分比来调整图片的高度,而不是一个像素。 However, when I use height: 30%; 但是,当我使用height: 30%; it doesn't work, but height: 30px; 它不起作用,但height: 30px; does work. 确实有效。 What am I doing wrong? 我究竟做错了什么?

My snippet is mind boggingly easy. 我的片段很简单。

 .imagebanner { height: 30%; width: 100%; } 
 <img src="http://localhost/wordpress/wp-content/uploads/2016/11/Welding-banner.jpg" alt="welding-banner" class="imagebanner" /> 

If you use a percentage value for height , the parent element needs to have a defined height (for example 100%), and this goes up to the body and html, so as a start you can begin with adding 如果你使用百分比值作为height ,父元素需要有一个定义的高度(例如100%),这将上升到body和html,所以作为一个开始你可以开始添加

html, body { height: 100%; }

and also give height definitions to all the elements in between body and your image. body和图像之间的所有元素提供高度定义。

Update your browser and then try. 更新您的浏览器,然后尝试。 Sometimes if you're using old browser. 有时,如果您使用的是旧版浏览器。 New features of HTML don't work in old browser. HTML的新功能在旧浏览器中不起作用。

You need to set a 100% height on the parent element. 您需要在父元素上设置100%的高度。

 .imagebanner { height: 30%; width: 100%; background: red; } .wrapper { height: 200px; width: 200px; background: grey; } 
 <div class="wrapper"> <img src="http://christiancomputerrepair.com/wp-content/themes/christiancomputerrepair/images/home_computer.png" class="imagebanner"> </div> 

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

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