CSS ID and Class selector

Source: Internet
Author: User

If you want to set CSS styles in HTML elements, you need to set the "id" and "class" selectors in the element.

ID Selector

The ID selector can specify a specific style for an HTML element that is labeled with a specific ID.

The HTML element sets the ID selector with the id attribute, and the ID selector in the CSS is defined as "#".

The following style rules apply to element properties Id= "PARA1":

<!DOCTYPE HTML><HTML><Head><style>#para1{text-align:Center;Color:Red;} </style></Head><Body><PID= "Para1">Hello world!</P><P>This paragraph isn't affected by the style.</P></Body></HTML>    

The id attribute does not start with a number, and the ID at the beginning of the number does not work in the Mozilla/firefox browser.

Class Selector

The class selector is used to describe the style of a set of elements, which differs from the ID selector, and class can be used in multiple elements.

The class selector is represented in the HTML as a class attribute, and in CSS, the category selector takes a point "." Number display:

In the following example, all HTML elements that have a center class are centered.

<!DOCTYPE HTML><HTML><Head><style>. Center{text-align:Center;}</style></Head><Body><H1class= "Center">Center Title</H1><Pclass= "Center">The paragraph is centered.</P> </Body></HTML>        

You can also specify a specific HTML element to use class.

In the following instance, all P elements use class= "center" to center the text of the element:

<!DOCTYPE HTML><HTML><Head><style>P.center{text-align:Center;}</style></Head><Body><H1class= "Center">This heading won't be affected</H1><Pclass= "Center">This paragraph would be center-aligned.</P> </Body></HTML>        

The first character of a class name cannot use a number! It doesn't work in Mozilla or Firefox.

CSS ID and Class selector

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.