1. 程式人生 > >html中class和id的區別

html中class和id的區別

class用來宣告類別, 主要是指向css表中的類,可以給html元素賦予多個class, 例如:

<html>

<head>

<style type="text/css">

h1.intro

{

color:blue;

text-align:center;

}

.important {background-color:yellow;}

</style>

</head>

<body>

<h1 class="intro important">Header 1</h1>

<p>A paragraph.</p>

</body>

</html>

 

 

id是為了唯一標識html元素, css/js中使用時用#引用,例如:

<html>

<head>

<style>

#myHeader

{

color:red;

text-align:center;

}

</style>

</head>

<body>

<h1 id="myHeader">W3School is the best!</h1>

</body>

</html>