CSS class selector # And Id selector.

Source: Internet
Author: User

CSS syntax consists of three parts: selector, attribute, and value:

selector {property: value}

Selector is usually the HTML element or tag you want to define. Property is the attribute you want to change and each attribute has a value. The attribute and value are separated by colons and surrounded by curly brackets, thus forming a complete style Declaration (Declaration ):

body {color: blue}

The above line of code defines the text color in the body element as blue. In the above example, the body is the selector, and the part included in the curly brackets is the declaration. The Declaration is composed of two parts: attribute and value, color is attribute, and blue is value.

CSS class selector and ID selector:

Id selector "#"

The ID selector can specify a specific style for HTML elements labeled with a specific ID.

The ID selector is defined.

The following two ID selectors can be used to define the color of the first element as red, and the color of the second element as Green:

#red {color:red;}
#green {color:green;}

In the following HTML code, the P element whose ID attribute is red is displayed in red, while the P element whose ID attribute is green is displayed in green.

<Pid="red"> This paragraph is in red. </P>
<Pid="green"> This section is green. </P>

In CSS, the class selector is displayed with a dot:

.center {text-align: center}

In the preceding example, all HTML elements with the center class are centered.

In the following HTML code, both H1 and P elements have center classes. This means both of them will comply with the rules in the ". Center" selector.

class="center">
This heading will be center-aligned

<p class="center">
This paragraph will also be center-aligned.
</p>

Note: the first character of the class name cannot contain numbers! It does not work in Mozilla or Firefox.

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.