ID(Identifiers):
Class(Classes):
ID的优势:
Class的优势:
#uniqueElement
。.commonStyle
。ID的应用场景:
Class的应用场景:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>CSS ID and Class Example</title>
<style>
#header {
background-color: #f1f1f1;
padding: 20px;
text-align: center;
}
.button {
background-color: #4CAF50; /* Green */
border: none;
color: white;
padding: 15px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 4px 2px;
cursor: pointer;
}
</style>
</head>
<body>
<div id="header">
<h1>Welcome to My Website</h1>
</div>
<button class="button">Learn More</button>
<button class="button">Contact Us</button>
</body>
</html>
在这个例子中,#header
是一个ID选择器,用于设置页眉的样式。.button
是一个类选择器,用于设置所有按钮的通用样式。
问题:为什么我为一个元素设置了ID样式,但是它没有生效?
原因:
解决方法:
参考链接: