我需要的是在Internet Explorer中將圖像寬度的限制設置爲100%,就像其他瀏覽器使用max-width
一樣。也就是說,如果圖像的寬度大於包含區域的寬度,則會縮小圖像以適應包含區域的寬度,但如果圖像較小,則其大小不會更改。同樣,如果圖像位於表格單元格(td
)內並且比單元格大,我希望它縮放到單元格的大小,而不是展開它。在Internet Explorer中設置最大寬度
雖然還有其他問題和答案似乎與此有關,但我無法讓他們中的任何一個人工作。例如,該解決方案通常建議效仿最大寬度在Internet Explorer中:
http://www.svendtofte.com/code/max_width_in_ie/
在本質上使用此:
width:expression(
document.body.clientWidth > (500/12) *
parseInt(document.body.currentStyle.fontSize)?
"30em":
"auto");
}
然而,當我嘗試它,我沒有得到預期結果。在某些情況下,當我檢查Firebug或類似的東西時,我的寬度值爲-1,並且完全沒有顯示圖像。
我看不出解決方案如何工作。
編輯:
根據要求,這裏是一些示例代碼:
<table cellpadding="4" cellspacing="0"
summary="" id="Push12Matt__simpletable_rph_vch_32" border="0" class="simpletable">
<tr class="strow">
<td valign="top" class="stentry" width="50%">
<div class="fig fignone" id="Push12Matt__fig_6a268fd9-2a26-474f-83f5-528ffbab70d3"><a
name="Push12Matt__fig_6a268fd9-2a26-474f-83f5-528ffbab70d3"><!-- --></a><p class="figcap"
>Bild 1. Uponor Push 12</p>
<a name="Push12Matt__image_4dd4d9ef-f95c-41f1-b423-7ddd3a2b0c06"><!-- --></a><img
class="image" id="Push12Matt__image_4dd4d9ef-f95c-41f1-b423-7ddd3a2b0c06"
src="/handbok/images/Push12/Push12_byggmatt.jpg" />
</div>
</td>
<td valign="top" class="stentry" width="50%">
<div class="fig fignone" id="Push12Matt__fig_689a2b08-ffbb-4f92-9a27-010e99665959"><a
name="Push12Matt__fig_689a2b08-ffbb-4f92-9a27-010e99665959"><!-- --></a><p class="figcap"
>Bild 2. Uponor ElPush 12</p>
<a name="Push12Matt__image_f6d7c2fa-8ab3-4e46-b79c-e7881dff03e9"><!-- --></a><img
class="image" id="Push12Matt__image_f6d7c2fa-8ab3-4e46-b79c-e7881dff03e9"
src="/handbok/images/Push12/Push12Electronic_byggmatt.jpg" />
</div>
</td>
</tr>
</table>
而簡單的CSS(除IE的所有瀏覽器中工作):
img
{
max-width : 100%;
max-height : 100%;
}
但它不在IE中無法使用此代碼。也許這事做的事實,它被放置在一個表中,我不知道,但是當我嘗試這個div上W3Schools的例子,它工作正常: http://www.w3schools.com/cssref/playit.asp?filename=playcss_max-width&preval=50%25
編輯2:
示例完整HTML頁面:
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="sv-se" xml:lang="sv-se">
<head>
<title>Image test</title>
<style type="text/css">
body {
max-height : 100%;
max-width : 100%;
width : 500px;
}
img {
max-height : 100%;
max-width : 100%;
width : auto;
height : auto;
}
td
{
max-height : 100%;
max-width : 500px;
display : block;
}</style>
</head>
<body id="frontpage">
<h1 class="title topictitle1">Image test</h1>
<div class="body conbody">
<table>
<tbody>
<tr>
<td>
<img src="Push12_byggmatt.jpg" />
</td>
<td>
<img src="Push12Electronic_byggmatt.jpg" />
</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
此頁面顯示同樣的問題。圖像不會縮小以適應表格單元格。在其他瀏覽器中,我可以根據自己的需要調整窗口大小,並且圖像只是保持縮小。 IE8和9沒有。所以看起來IE8和9支持最大寬度和最大高度,但只支持像素值,而不是百分比值 - 即只支持部分支持......如果我是正確的,我真的很驚訝它很難在互聯網上找到關於此的任何信息。每個人都只是在談論這些瀏覽器,因爲最終支持它後,IE6沒有...
無論如何,我已經寫了一個jQuery的解決方法,但我寧願不需要它。所以如果有人可以告訴我我錯了,並告訴我,IE8和9實際上支持最大寬度百分比值,我會很高興出錯:-)
如果你提供你已有的代碼,你的問題更可能得到一些答案。就目前而言,如果典型的選擇不起作用,那麼很可能會干擾技術或其他衝突。 – Shauna 2012-01-10 18:32:57
從某種意義上說,您可能是對的,請參閱我的關於W3Shools示例的編輯。但它不適用於我自己的網頁... – Anders 2012-01-11 14:38:15
您是否在談論IE6?較新的版本支持最大寬度。 – duri 2012-01-11 14:39:56