6

我对一行包含三个单元格的表格有疑问。其中两个(左和右)具有固定的宽度/高度,并且应该显示图像(或在没有图像时缩小到 0)。中间单元格占用表格中固定总数的剩余空间。

问题:现在我得到了 3px 的额外空间,“图像容器”似乎扩展了(在单元格下方)。例如,您可以通过检查“starredunread”元素来查看它,它的高度从 20px 增长到 23px。而且我不知道发生了什么,因为默认情况下边距/填充已经关闭,并且border:0和border-collapse:collapse没有帮助。

这是CSS文件:

/*------------------------------------------

Reset (from the Yahoo UI Library)

------------------------------------------*/



body,div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,form,fieldset,input,textarea,p,blockquote,th,td {margin:0;padding:0;}

table{border-collapse:collapse;}

fieldset,img{border:0;}

address,caption,cite,code,dfn,em,strong,th,var{font-style:normal;font-weight:normal;}

ol,ul{list-style:none;}

caption,th{text-align:left;}

h1,h2,h3,h4,h5,h6{font-size:100%;font-weight:normal;}

q:before,q:after{content:'';}

abbr,acronym{border:0}

a{outline: none;} 



/*------------------------------------------

Interface

------------------------------------------*/

body {

top: 0px;

left: 0px;

width: 320px;

height: 480px;

background-color: #f00;

color: #000;

font-family: helvetica, arial, sans-serif;

font-size: 12px;

}

.header {

display: table;

top: 0px;

left: 0px;

width: 320px;

height: 44px;

background-color: #738ba3;

color: #fff;

table-layout: fixed;

border: 0;

border-spacing: 0;

}

.headerrow {

display: table-row;

}


.text {

display: table-cell;

overflow: hidden;

text-overflow: ellipsis;

white-space: nowrap;

padding-left: 2px;

vertical-align: middle;

}

.text b {

font-weight: 700;

font-size: 11pt;

}

.date {

display: table-cell;

width: 50px;

vertical-align: middle;

text-align: right;

font-size: 8pt;

padding-top: 3px;

padding-right: 5px;

}

.date b {

font-weight: 700;

line-height: 11pt;

}


.starredunread {

display: table-cell;

top: 0px;

left: 0px;

width: 20px;

height: 44px;

vertical-align: middle;

}

.icon {

display: table-cell;

top: 0px;

right: 0px;

width: 44px;

height: 44px;

}


.content {

display: table;

width: 320px;

background-color: #fff;

color: #000;

font-size: 12pt;

text-align: left;

padding: 15px;

}

.sectionheader {

height: 20px;

}

.sectionheaderrow {

}

.sectionunread {

height: 20px;

}

.sectiontext {

font-weight: 700;

font-size: 9pt;

padding-top: 1px;

}

.smallicon {

width: 20px;

height: 20px;

}

这是 HTML 代码:

<!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>testHTML</title>

<link rel="stylesheet" href="test.css" type="text/css" media="screen" />

</head>

<body>

<div class="header">

    <div class="headerrow">

        <div class="starredunread">
            <img src="images/testimg_small.png">
        </div><!-- end starredunread -->

        <div class="text">
                <b>Testheader</b><br>
                Text for testing - this should overflow with an ellipsis if the text exceeds the size of the container...
        </div><!-- end text -->

        <div class="date">
                <b>11.11.09</b><br>
                18:54
        </div><!-- end date -->

        <div class="icon">
            <img src="images/testimg_44x44.png">
        </div><!-- end icon -->

    </div><!-- end headerrow -->

</div> <!-- end header -->

<div class="content">
    More text for testing purposes.<br><br>Not really relevant.<br><br> So don't waste your time reading this. :o)
</div><!-- end content -->

<div class="header sectionheader">

    <div class="headerrow sectionheaderrow">

        <div class="starredunread sectionunread">
            <img src="images/testheader.png">
        </div><!-- end sectionunread -->

        <div class="text sectiontext">
            Another Header
        </div><!-- end sectiontext" -->

        <div class="icon smallicon">
            <img src="images/testimg_20x20.png">
        </div><!-- end smallicon -->

    </div><!-- end sectionheaderrow -->

</div><!-- end sectionheader -->

</body> 

</html>

...没有图像看起来不太好,但无论如何它证明了我的问题。

知道我做错了什么吗?非常感谢您的意见!该代码只需要在 Safari / Webkit 中工作。

4

2 回答 2

10

在严格模式下,默认情况下图像是内联的,因为您使用的是严格模式,您需要通过应用display:block到您的图像来手动修复此问题。

“在早期,使用严格模式的实验总是会提出这样的评论,即图像突然出现无法删除的奇怪底部边距。原因是严格模式下是一个内联元素,这意味着应该为可能的下行字符,如 g、j 或 q。当然,图像没有下行字符,因此空间从未使用过,但仍必须保留。

解决方案是显式声明图像块级元素:img {display: block}。”

http://www.quirksmode.org/css/quirksmode.html

于 2009-11-11T18:33:25.000 回答
0

我有一个类似的问题。通常,您必须使用以下标记(最好在样式表的开头)禁止所有 css 元素的边距和填充: * { margins: 0; 填充:0;}

前导“*”很重要。这可以节省几个小时的挫败感。之后,如果您有一个文本单元格,顶部似乎还有半个 em 的填充,但这是因为大多数文本字符都比 em 短。

于 2010-11-13T10:21:51.063 回答