A brief introduction to the ID selector and class selector in CSS

Source: Internet
Author: User

<!--a selector in CSS
CSS has two selector IDs and class, in short if you want to set CSS properties in HTML elements,
You want to set the ID and class selector in the element. So now we're going to one of the two in the introduction of these selectors
ID selector:
element attributes in HTML set the ID selector with an ID, and the ID selector in the CSS is defined with "#".
Like what:
#para1
{
Text-align:center;
color:red;
}
This defines a selector, what is a selector, on-line search is the need to change the HTML elements, very correct, at the beginning of our internal style definition when we want to let the paragraph or the title of the style, we can define the style between the head, that is,
p{
color:red;
} This changes the color of the paragraphs in our document, but let's think about it, so that at the beginning of the definition, our paragraph needs to be defined, the title also needs to be defined, this is not a bit troublesome, we would like to define a style, in the title can be used in the style can also be used, it is easy to implement We can set the title and the paragraph's property ID and class by the time we write in the document, so that we can define a style, we all use it, in fact, the programming is not like this, create a we can use, so that you can save everyone's time, In fact, a public may be the beginning of programming ideas. So what is the common style of our definition of this definition of the process we have to consider the paragraph is the title when used, the value of the set to the ID or class value, corresponding to produce two selectors one is the ID selector, one is the class selector, they define the method is different.
Let's take a look at an example using the ID selector:-->
<! DOCTYPE html>
<meta charset= "Utf-8" >
<style type= "Text/css" >
#para1 {
Text-align:center;
color:red;
}
</style>
<title></title>
<body>
<p id= "Para1" >hello world</p>
&LT;H1 id= "Para1" >Hello<p>this is a paragraph who isn't affected by the style</p>
</body>
<!--above is the defined ID selector, where we continue to look at the class selector:
The class selector is used to describe the style of a set of elements, and the difference between the class selector and the ID selector is
The class selector can be used in multiple elements, and the class selector is a class attribute in HTML
, the class selector in CSS is "." Display the
<! DOCTYPE html>
<meta charset= "Utf-8" >
<title></title>
<style type= "Text/css" >
. center{
Text-align:center;
color:red;
}
</style>
<body>
&LT;H1 class= "center" > Title Center <p class= "Center" > Paragraph Center </p>
</body>

A brief introduction to the ID selector and class selector in CSS

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.