简体   繁体   English

DIV height接受内部img的高度,而不是使用CSS设置的高度

[英]DIV height takes the height of img inside and not the height set using CSS

<script type="text/javascript">  
    $(document).ready(function(e){  
       var dheight=$(document).innerHeight();  
       $(".background").css({  
          "height": dheight,  
          "overflow":"hidden"  
       });  
    });  
</script>

<section id="box">
    <section class="heading">
        <div class="head">SiteName</div>
    </section>
    <!--heading-->
    <div class="background">
        <img src="images/hdimage.jpg" width="100%" />
    </div>
    <!--background-->
</section>
<!--box-->

The problem is in the Internet Explorer (even IE 10) <div class="background"> takes the aspect height of the image, even I set height , max-height with overflow: hidden . 问题出在Internet Explorer(甚至是IE 10)中,即使我设置了heightmax-height并带有overflow: hidden ,该图像的宽高也是<div class="background">

In Google Chrome, Firefox there is no problem. 在Google Chrome浏览器中,Firefox没问题。 On inspecting in IE I find IE adds inline style with height of the image to the <div> . 在IE中进行检查时,我发现IE将具有图像高度的内联样式添加到<div> How to resolve this issue? 如何解决这个问题?

I'm guessing your trying to achieve full screen backgrounds, if you are your code is quite far from being complete and working in all instances and browsers. 我猜您是在尝试获得全屏背景,如果您的代码离完成和在所有实例和浏览器上的工作还差得远。

I think your code may not be working due to the missing closing tag on your img /> 我认为您的代码可能由于img />上缺少结束标记而无法正常工作

Id suggest you take a look at the following code found on this article 编号建议您看看下面的代码上找到这篇文章

.background{

         background: url(images/bg.jpg) no-repeat center center fixed; 
          -webkit-background-size: cover;
          -moz-background-size: cover;
          -o-background-size: cover;
          background-size: cover;
          filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='.myBackground.jpg', sizingMethod='scale');
          -ms-filter: "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='myBackground.jpg', sizingMethod='scale')";

}

In my opinion thеse lines are unpredictable 我认为这些行是无法预测的

$(document).ready(function(e){  
       var dheight=$(document).innerHeight();

When you deal with images since their height is yet unavailable you should wait for them to load. 由于图像的高度尚不可用,因此在处理图像时,应等待它们加载。

$(window).on('load',fumction(){
   var dheight=$(document).innerHeight();
   ...
})

Is the better option 是更好的选择

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

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