2009-11-11 91 views
6

我在一行中包含三個單元格的表存在問題。其中兩個(左側和右側)具有固定的寬度/高度,並應顯示圖像(或在沒有圖像時縮小爲0)。中間的單元格佔用了表中固定總數的剩餘空間。在Safari中爲每個DIV表格元素獲取不需要的1px額外高度

問題:現在我結束了3px的額外空間,'圖像容器'似乎展開(單元格下方)。例如,你可以通過檢查'starredunread'元素來看到它,它的高度從20px增加到23px。我不知道發生了什麼事,因爲margin/padding已經被默認關閉了,並且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中工作。

回答

10

在嚴格模式下,默認情況下圖像是內聯的,因爲您嚴格使用,所以需要通過將display:block應用於圖像來手動解決此問題。

「在創業初期,具有嚴格的模式實驗不約而同地提出了 評論說,圖像突然接到了無法 刪除。其原因是,在嚴格的模式是一個內聯元素 一個奇怪的底部邊緣,這意味着一些空間應該保留爲可能的 下行字符,如g,j或q。當然,圖像沒有 下行字符,所以空間從未被使用,但它仍然必須保留 。

解決方案是顯式聲明圖像塊級元素: img {display:block}。「

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

+0

哇 - 這是快速,簡單和:有效!非常感謝,修復它! – 2009-11-11 18:39:07

0

我有一個類似的問題。通常,您必須使用以下標記禁止所有CSS元素的邊距和填充(最好在樣式表的開頭): * { 邊距:0; 填充:0; }

領先的'*'很重要。這可以節省數小時的沮喪。 之後,如果你有一個文本單元格,它看起來好像仍然有一半的填充位置在頂部,但這是因爲大多數文本字符比EM短。