简体   繁体   English

有关Chrome开发者工具中所示的元素尺寸的问题

[英]question about element dimensions as shown in Chrome Developer Tools

UPDATE 更新

Here's a fiddle to play with: 这是一个玩弄的小提琴:

http://jsfiddle.net/UnsungHero97/RQCt3/1/ http://jsfiddle.net/UnsungHero97/RQCt3/1/


Here are two screenshots that show what I'm encountering... 这是显示我所遇到的问题的两个屏幕截图...

将鼠标悬停在图片上

将鼠标悬停在div上

The weird thing I'm encountering is this. 我遇到的奇怪的事情是这个。 When I hover my mouse over the <img> element, it shows its dimension as 288x72, which is what I expect it to be since I set the height of the image as 72. 当我将鼠标悬停在<img>元素上时,它的尺寸显示为288x72,这是我期望的,因为我将图像的高度设置为72。

However, when I hover over the <div id="logo"> element, it shows its dimensions as 288x76, which means there is an extra 4 pixels somewhere contributing to this height. 但是,当我将鼠标悬停在<div id="logo">元素上时,它的尺寸显示为288x76,这意味着在此高度处还有4个像素。

If you notice the CSS properties for the <div id="logo"> element, there are no paddings or margins or heights specified. 如果您注意到<div id="logo">元素的CSS属性,则没有指定填充或边距或高度。

So where are these pixels coming from? 那么这些像素来自哪里呢? Why is this happening? 为什么会这样呢?

An easy way to fix this is: 解决此问题的一种简单方法是:

img{
    vertical-align: top;
}

Live example: http://jsfiddle.net/RQCt3/2/ 实时示例: http//jsfiddle.net/RQCt3/2/

The reason is that images in HTML are considered inline elements and so by default are aligned on the font base line. 原因是HTML中的图像被视为内联元素,因此默认情况下在字体基线上对齐。 The extra space is probably to allow for the bottom of letters etc. 多余的空间可能允许字母等的底部。

Similarly adding display:block will also do the job: 同样,添加display:block也可以完成此工作:

img{
    display:block;
}

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

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