一句话概括:为什么<a>标签比里面的img高度多出4px 的问题,主要还是由于 img是inline element, it's height is caculated differently as related to the

default line-height value. On inline elements, the line-height css property specifies the height that is used in the calculation of the line box height.

on block level elements, line-height specifies the minimal height of line boxes within the element.

参考: http://stackoverflow.com/questions/11126685/why-does-container-div-insist-on-being-slightly-larger-than-img-or-svg-content

原文:

----------------------------------------------------------------------------------

Trying adding:

img { display: block; }

to your CSS. Since an <img> is an inline element by default, it's height is calculated differently as related to the default line-height value.

On inline elements, the line-height CSS property specifies the height that is used in the calculation of the line box height.

On block level elements, line-height specifies the minimal height of line boxes within the element.

Source: https://developer.mozilla.org/en/CSS/line-height

That does indeed fix it. Could you elaborate as to why it makes a difference? (I'm not holding out on accepting - SO won't let me until 11 more minutes have passed) – Chris Jun 20 '12 at 19:25 
 
 
@Chris - it's the reason I had given you: the img has an implicit line-height assigned to it, which gives it some vertical breathing room. Since line-height only applies to inline elements, setting the imgto display: block removes the line-height. – Joseph Silber Jun 20 '12 at 19:30
    
@Joseph - I hadn't seen your response when I made the above request. Since they are basically achieving the exact same thing directly and indirectly, there's not much to choose between the answers. It only seems fair to accept the answer that arrived first, but thanks for your explanation. – Chris Jun 20 '12 at 19:34
1  
This also works with inline-block. Thanks for the explanation.

css中高度比img多出4px的问题的更多相关文章

  1. css中的流,元素,基本尺寸

    流 元素 基本尺寸 流之所以影响整个css世界,是因为它影响了css世界的基石 --HTML HTML 常见的标签有虽然标签种类繁多,但通常我们就把它们分为两类: 块级元素(block-level e ...

  2. css中“~”和“>”

    css中“~” element1~element2 选择器匹配  出现在   element1 后面的    element2  .element1 和 element2 这两种元素必须具有相同的父元 ...

  3. css中".",",",“~”和“>”符号的意义

    css中“~” element1~element2 选择器匹配出现在element1后面的element2.element1和element2这两种元素必须具有相同的父元素,但element2不必紧跟 ...

  4. css中height 100vh的应用场景,动态高度百分比布局,浏览器视区大小单位

    css中height 100vh的应用场景,动态高度百分比布局,浏览器视区大小单位 height:100vh 一些只能vw, vh才能完成的应用场景: 1. 场景之:元素的尺寸限制 vw vh 主要是 ...

  5. css中如何实现左边的高度随着右边改变而改变

    css中如何实现左边的高度随着右边改变而改变 html结构: <div class="main"> <div class="main_left" ...

  6. CSS 中的高度百分比

    CSS 中可以使用%来给定指定元素的大小,也就是高度.宽度.margin,padding 等等,但是相信很多人都对百分比表示法的具体含义并不清楚,那么不懂就练,毕竟是检验真理的唯一标准(考研党举个手我 ...

  7. 对CSS中的Position、Float属性的一些深入探讨

    对CSS中的Position.Float属性的一些深入探讨 对于Position.Float我们在平时使用上可以说是使用频率非常高的两个CSS属性,对于这两个属性的使用上面可能大多数人存在一些模糊与不 ...

  8. 浅析CSS中的BFC和IFC

    1. 为什么会有BFC和IFC 首先要先了解两个概念:Box和formatting context: Box:CSS渲染的时候是以Box作为渲染的基本单位.Box的类型由元素的类型和display属性 ...

  9. CSS中一些常见的兼容性问题

    CSS中一些兼容性问题就是浏览器兼容,而这些浏览器兼容问题主要是Ie和FF之间的争斗. CSS hack中的一些事: 我们为了让页面形成统一的效果,要针对不同的浏览器或不同版本写出对应可解析的CSS样 ...

随机推荐

  1. window.onload和DOMContentLoaded的区别

    一.何时触发这两个事件? 1.当 onload 事件触发时,页面上所有的DOM,样式表,脚本,图片,flash都已经加载完成了. 2.当 DOMContentLoaded 事件触发时,仅当DOM加载完 ...

  2. 油猴和EX-百度脚本 百度网盘下载

    pansoso.com 搜网盘 油猴和EX-百度脚本.zip https://aleikeji.pipipan.com/fs/845023-331102839

  3. Deep_into_iris

    具体ipynb文件请移步Github #各种所需要的库函数首先加载 import numpy as np import pandas as pd import matplotlib.pyplot as ...

  4. struct获取不到值的小错误

    struct2 get set 这两个方法一定要用双骆驼命名法:getA() setA(),        而geta() seta()不行 我找了好久的错误,只能说框架这东西快捷方便,找起错误要人命

  5. P2964 [USACO09NOV]硬币的游戏A Coin Game (DP)

    题意:n颗硬币 两个人从前往后按顺序拿 如果上一个人拿了i颗 那么下一个可以拿1-2*i颗 问先手能获得的最大收益 题解:比较典型的最大最小最大最小..DP了 但是暴力做的话是n^3 所以就体现出了这 ...

  6. MySQL索引之博客荐读

    推荐博客: 寒江独钓. 浅谈算法和数据结构: 十 平衡查找树之B树 张洋. MySQL索引背后的数据结构及算法原理 漫画算法:什么是 B+ 树? B树和B+树的插入.删除图文详解 Jeremy Col ...

  7. 用python的Requests库模拟http请求

    一.先了解几个重要的http请求头或响应头信息 Request Headers: Host: 描述请求将被发送的目的地,包括,且仅仅包括域名和端口号. Origin: 说明请求从哪里发起的,包括,且仅 ...

  8. 《算法导论》— Chapter 9 中位数和顺序统计学

    序 在算法导论的第二部分主要探讨了排序和顺序统计学,第六章~第八章讨论了堆排序.快速排序以及三种线性排序算法.该部分的最后一个章节,将讨论顺序统计方面的知识. 在一个由n个元素组成的集合中,第i个顺序 ...

  9. 【HIHOCODER 1509 】 异或排序

    描述 给定一个长度为 n 的非负整数序列 a[1..n] 你需要求有多少个非负整数 S 满足以下两个条件: (1).0 ≤ S < 260 (2).对于所有 1 ≤ i < n ,有 (a ...

  10. 解决safari里面淘宝京东页面无法打开以及打开后乱码的问题!

    sqlite3 ~/Library/Keychains/*/ocspcache.sqlite3 'DELETE FROM responses WHERE responderURI LIKE " ...