2016-11-17 39 views
2

根據此鏈接元素元素的CSS規則http://www.w3schools.com/cssref/css_selectors.asp不適用於以下代碼。這有什麼問題?css不適用於使用此規則的「元素元素」。這個代碼有什麼問題?

<!DOCTYPE html> 
 
    <html> 
 
    <head> 
 
    <style> 
 
    p div{ 
 
     text-align: center; 
 
     color: red; 
 
    } 
 
    </style> 
 
    </head> 
 
    <body> 
 
    
 
    <h1 class="">This heading will not be affected</h1> 
 
    <p><div class="center">This paragraph will be red and center-aligned.</div></p> 
 
    
 
    </body> 
 
    </html>

+0

這可能重複:http://stackoverflow.com/questions/8397852/why-p-tag-cant-contain-div-tag-inside-it –

回答

4

這不是規則本身,它的HTML,這是無效的。您不能在<p>內包裝<div>。將<p>放在<div>之內,並將規則交換爲div p {,它將起作用。

+0

是的,這給了我清楚的理解,爲什麼問題,謝謝:) –

2

小的語法變化;-)

<!DOCTYPE html> 
<html> 
<head> 
    <style> 
     p.red{ 
      text-align: center; 
      color: red; 
     } 
    </style> 
</head> 
<body> 

<h1 class="">This heading will not be affected</h1> 
<p class="red">This paragraph will be red and center-aligned</p> 

</body> 
</html> 
+0

我知道這種方式在那裏,但我只想知道爲什麼我的代碼沒有工作:) –

+1

這是因爲語法不正確。 –

相關問題