html,css - 图片下面的奇怪的隐形边距

29 css image margin

我在这里去香蕉,不知怎的,在我的页面中我的所有图像下面都有一个间隙,代码中没有边距.甚至Firebug都看不到它,但Firefox和Safari正在渲染它 - 即使没有CSS也没有!

从来没有发生在我身上......!

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Paranoid</title>
<link rel="stylesheet" href="includes/style.css" type="text/css" />
</head>
<body>

    <div id="container">
        <div id="sidebar">
            <img src="images/logo.png" id="logo" />
            <ul id="menu">
                <li class="menu1">Main</li>
                <li class="menu1">System</li>
                <li class="menu1">View</li>
                <li class="menu1">Policy</li>
            </ul>
            <div id="sidebar_bottom"></div>
        </div>
        <div id="main_content"></div>
        <div class="clear"></div>
    </div>

</body>
</html>

body{
    background: #497e9f url(../images/bg.png) repeat-x top;
}
#container{
    width:864px;
    margin: 8px auto 0 auto;
}
#sidebar{
    /*width:139px;*/
    float: left;
}
#sidebar_bottom{
    height:10px;
    background: url(../images/sidebar_bottom_bg.png) bottom left no-repeat;
}
#sidebar #logo{
    margin-bottom: 2px;
}
#sidebar #menu{
    background: #f2f2f2;
    border: 0 1px solid #cecece;
    margin: 0;
    list-style: none;
}
Run Code Online (Sandbox Code Playgroud)

Guf*_*ffa 79

这实际上并不罕见.这是因为图像是内联元素,因此在图像底部(文本基线和文本行的底部)之间存在一些空间.

最简单的解决方案是简单地使用display:block;将图像转换为块元素.使用float:left;或浮动图像float:right;也会将其转换为块元素.

调整像这样的属性vertical-align,font-sizeline-height可能会影响距离,但它并不像它真正消除效果那样强大.它可能仍然出现在某些情况下.

相关问题:
XHTML 1.0中的图像下方不需要的间距严格
为什么我的图像有额外的间距?
IE图像间距问题

  • 在这个答案最初写出 12 年后,设置 `display: block` 仍然是一个有用的建议。 (2认同)

小智 15

很抱歉3年后回答这个问题,但这个页面在第一个谷歌页面,我觉得有责任.....回答:只添加"vertical-align:top;" 的img标记内一个标签.


ley*_*nnx 6

为了我

font-size: 0;
line-height: 0;
Run Code Online (Sandbox Code Playgroud)

在包装容器上做了伎俩.


小智 -3

嗯,我明白了。

这是一个组合

font-size: 0px;
line-height: 1;
Run Code Online (Sandbox Code Playgroud)