class id class

id and class selectors
(1), id selectors
id selectors can specify a specific style for HTML elements marked with a specific id, and the id attribute is unique. HTML elements use the id attribute to set the id selector, and the id selector in CSS is defined by "#". Note: The id attribute cannot start with a number.

<head>
<style>
#heading{
     color:red;
     text-align:center;
}
</style>
</head>
<body>
<h1 id="heading">CSS selector</h1>
</body>
class selector
  class selector is used to describe the style of a group of elements, also called class selector, class can be used in multiple elements, and one element can also specify multiple class names. A class selector is represented by the class attribute in HTML, and in CSS, a class selector is defined by a dot ".". You can set all HTML elements with the specified class, or you can specify specific HTML elements to use the class. Note: The first character of the class name also cannot use a number.
<head>
<style>
.center{
     text-align:center;


     color:red;
}
.font{
     font-size:18px;
     font-family:"Microsoft YaHei";
}
</style>
</head>
<body>
<h1 class="center">class selector</h1>
<p class="center col">I am a paragraph. </p>
<p class="center font">I am another paragraph. </p >
</body>
What's the difference between them?
ID selectors can only be used once in a document. Unlike class selectors, ID selectors can only be used once, and only once, within an HTML document. While the class selector can be used multiple times, there is
also a priority id priority greater than class

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326589592&siteId=291194637