2010-03-01 39 views
1

为什么这个工作:CSS,为什么一件事情可以工作,而不是另一件事情?

<div style="background-color: #ccc; 
    -moz-border-radius: 5px; 
    -webkit-border-radius: 5px; 
    border: 1px solid #000; 
    padding: 10px;">testing 10,9,8,7 
</div> 

这不?

<div style="roundedCornerBox"> 
    testing 10,9,8,7 
</div> 

当我创建了一个CSS文件,上面写着:

body { 
    background-color: #FFFFFF; 
    text-align: center; 
} 

roundedCornerBox { 
    background-color: #ccc; 
    -moz-border-radius: 5px; 
    -webkit-border-radius: 5px; 
    border: 1px solid #000; 
    padding: 10px; 
} 

这里是我的HTML代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html> 
<head><title></title> 
    <link rel="stylesheet" href="lib/css/style.css" />  
</head> 

<body> 
<div style="roundedCornerBox"> 
    testing 10,9,8,7 
</div> 

<div style="background-color: #ccc; 
    -moz-border-radius: 5px; 
    -webkit-border-radius: 5px; 
    border: 1px solid #000; 
    padding: 10px;"> 
     testing 10,9,8,7 
</div> 
</body> 
</html> 

回答

8

您确实不意味:

<div class="roundedCornerBox"> 
    testing 10,9,8,7 
</div> 

.roundedCornerBox { 
    background-color: #ccc; 
    -moz-border-radius: 5px; 
    -webkit-border-radius: 5px; 
    border: 1px solid #000; 
    padding: 10px; 
} 

您需要的点来识别一个CSS类和使用CSS类,你需要将类名的dom元素的类属性。

+0

http://www.w3.org/TR/REC-html40/present/styles.html#adef-style 样式属性的定义。 较少的技术和再次有用的w3schools细分的差异:http://www.w3schools.com/CSS/css_howto.asp – Kzqai 2010-03-01 23:50:46

3

类开始以点!

.roundedCornerBox { 
    background-color: #ccc; 
    -moz-border-radius: 5px; 
    -webkit-border-radius: 5px; 
    border: 1px solid #000; 
    padding: 10px; 
} 

<div class="roundedCornerBox"> 

没有点,它正在寻找<roundedCornerBox />元素的样式。 style=也需要为class=

+0

仍然无法工作。仔细检查他的代码。他将类名放在style属性中。 – 2010-03-01 23:48:32

+0

@DN - 我也有这个修正... – 2010-03-01 23:48:53

+0

@Tchalvak - 公平点,更新更明确。 – 2010-03-01 23:49:35

0

您想在标记中使用class="roundedCornerBox",在CSS中使用.roundedCornerBox

0

div标记在您的第二个示例中不正确。这可能是

<div id="roundedCornerBox"> 
    testing 10,9,8,7 
</div> 

#roundedCornerBox { 
    background-color: #ccc; 
    -moz-border-radius: 5px; 
    -webkit-border-radius: 5px; 
    border: 1px solid #000; 
    padding: 10px; 
} 
+0

他只是忘了'.';) – casraf 2010-03-02 00:04:54

+0

这就是说,如果'roundedCornerBox'是一个'class '。另一个语法正确的选项是使其成为'id'。 (*语义*,或许作为'class'更好。无论如何。) – 2010-03-02 01:20:15

相关问题