一句话概括:为什么<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. github下拉刷新与上拉加载地址

    https://github.com/chrisbanes/Android-PullToRefresh

  2. C++关键字(保留字)

    C++ 关键字 点击下表以进入具体释义 __abstract 2 __alignof Operator __asm __assume __based __box 2 __cdecl __declspe ...

  3. Luogu P3806 点分治模板1

    题意: 给定一棵有n个点的树询问树上距离为k的点对是否存在. 分析: 这个题的询问和点数都不多(但是显然暴力是不太好过的,即使有人暴力过了) 这题应该怎么用点分治呢.显然,一个模板题,我们直接用套路, ...

  4. Linux 系统内存分析

    1. 内存基本介绍 1.计算机基本结构: 电脑之父--冯·诺伊曼提出了计算机的五大部件:输入设备.输出设备.存储器.运算器和控制器 如图: 输入设备:键盘鼠标等 CPU:是计算机的运算核心和控制核心, ...

  5. [LUOGU] P2920 [USACO08NOV]时间管理Time Management

    见7.3测试 #include<iostream> #include<algorithm> #include<cstdio> using namespace std ...

  6. Shell流程语句

    1. shell流程控制 流程控制是改变程序运行顺序的指令.linux shell有一套自己的流程控制语句,其中包括条件语句(if),循环语句(for,while),选择语句(case). 2. if ...

  7. Jvm:性能调优监控工具

    现实企业级Java开发中,有时候我们会碰到下面这些问题: OutOfMemoryError,内存不足 内存泄露 线程死锁 锁争用(Lock Contention) Java进程消耗CPU过高 .... ...

  8. HTML5编辑API之Range对象

    Range对象代表页面上的一段连续区域,通过Range对象,可以获取或修改页面上的任何区域,可以通过如下创建一个空的Range对象,如下: var  range = document.createRa ...

  9. Django之学员管理三

    Django之学员管理三 web框架的本质: 本质是客户端和服务端的交互.用socket实现. socket客户端(浏览器) 2.发送ip和端口,http://www.baidu.com:80/ind ...

  10. 杭电 1596 find the safest road (最小路径变形求最大安全度)

    Description XX星球有很多城市,每个城市之间有一条或多条飞行通道,但是并不是所有的路都是很安全的,每一条路有一个安全系数s,s是在 0 和 1 间的实数(包括0,1),一条从u 到 v 的 ...