简体   繁体   English

带有img的简单div中的额外高度像素

[英]Extra height pixels in simple div with img

I am having difficulty determining where the extra pixels of height are coming from for the div in this simple layout. 在这种简单的布局中,我很难确定div的高度额外像素来自何处。 My expectation would be that the image would determine the height of the div but instead it appears to be a few pixels larger. 我的期望是,图像将确定div的高度,但看起来似乎要大几个像素。 The pixels I am trying to get rid of are shown in blue. 我要摆脱的像素以蓝色显示。 Any ideas? 有任何想法吗?

Oh, I have added a button to explicitly set the height of the image parent div properly using javascript - my question is really where is it getting that extra height from in the first place, or how can I do this without javascript? 哦,我添加了一个按钮,以使用javascript显式正确设置图像父div的高度-我的问题是,实际上它是从哪里获得额外高度的?或者,如果没有javascript,该怎么办?

 function change() { var image = document.getElementById('image1ID'); var width = image.naturalWidth; var height = image.naturalHeight; var imageDIV = document.getElementById('image1DIV'); imageDIV.style.height = (100 * height / width) + "px"; } 
 <div id="image1DIV" style="background-color:#00f;"> <img id="image1ID" style="width:100px;" src="http://www.imageno.com/thumbs/20170330/73dlz3sr4pfy.jpg"> </div> <button onclick="change()">change height of parent div</button> 

Add vertical-align:middle to your img tag. 在您的img标签vertical-align:middle添加vertical-align:middle It removes extra space. 它删除了多余的空间。

 function change() { var image = document.getElementById('image1ID'); var width = image.naturalWidth; var height = image.naturalHeight; var imageDIV = document.getElementById('image1DIV'); imageDIV.style.height = (100 * height / width) + "px"; } 
 #image1ID { vertical-align: middle; } 
 <div id="image1DIV" style="background-color:#00f;"> <img id="image1ID" style="width:100px;" src="http://www.imageno.com/thumbs/20170330/73dlz3sr4pfy.jpg"> </div> <button onclick="change()">change height of parent div</button> 

the img tag height is not explicitly set to occupy 100% height of its parent. img标签的高度未明确设置为占据其父代的100%高度。 What you are seeing are parts of background color styled on the parent 您所看到的是父级样式的背景颜色的一部分

.flexrow img{
      height:100%;
    }

Snippet below 下面的代码段

 .flexrow { display: flex; flex-direction: row; flex-wrap: none; } .flexcolumn { display: flex; flex-direction: column; justify-content: center; align-items: center; } .textblock { width: 170px; } .flexrow img { height: 100%; } 
 <div class="flexcolumn"> <div class="flexrow"> <div style="background-color:#00f;"> <img style="width:100px;" src="http://www.imageno.com/thumbs/20170330/73dlz3sr4pfy.jpg"> </div> <div class="flexcolumn textblock" style="background-color:#eee;"> <div style="background-color:#e0e;">Title is here</div> <div style="background-color:#0ee;">Description is here</div> </div> </div> <div class="flexrow"> <div style="background-color:#0f0;"> <img style="width:100px;" src="http://www.imageno.com/thumbs/20170330/73dlz3sr4pfy.jpg"> </div> <div class="flexcolumn textblock" style="background-color:#ee0;"> <div style="background-color:#e0e;">Title is here</div> <div style="background-color:#0ee;">Description is here</div> </div> </div> </div> 

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

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