2012-03-09 89 views
1

我有一些HTML和CSS代碼像下面優先的內嵌樣式和內部樣式

<html> 
<head> 
<style type="text/css"> 
img.normal 
{ 
height:auto; 
} 
img.big 
{ 
height:120px; 
} 
p.ex 
{ 
height:100px; 
width:100px; 
} 
</style> 
</head> 

<body> 
<img class="normal" src="logocss.gif" width="95" height="84" /><br /> 
<img class="big" src="logocss.gif" width="95" height="84" /> 
<p class="ex">The height and width of this paragraph is 100px.</p> 
<p>This is some text in a paragraph. This is some text in a paragraph. 
This is some text in a paragraph. This is some text in a paragraph. 
This is some text in a paragraph. This is some text in a paragraph.</p> 
</body> 
</html> 

我記得三個樣式的優先級是直列>內部>外部。但上面的代碼中,圖片名爲'big',內聯樣式的高度爲84px,寬度爲95,爲什麼內部樣式會影響結果樣式?任何人都可以幫忙?

回答

2
<img class="big" src="logocss.gif" width="95" height="84" /> 

在上文中,widthheight不樣式規則,它們是元素(CSS應該替換)的屬性。我會猜想這些屬性會覆蓋樣式規則,但也許是相反的。無論哪種方式,你的問題的基本答案是那些不是內聯樣式,所以你對優先級的理解仍然是正確的。請嘗試以下操作:

<img class="big" src="logocss.gif" style="width:95px; height:84px;" /> 

並查看應用了什麼。

+0

謝謝!它運作良好! – James 2012-03-09 08:09:19