Home  >  Article  >  Web Front-end  >  How to define CSS id selector

How to define CSS id selector

(*-*)浩
(*-*)浩Original
2019-11-23 13:22:574049browse

How to define CSS id selector

CSS id selector

The id selector can specify a specific style for HTML elements marked with a specific id .

id selector is defined with "#". (Recommended learning: CSS Introduction Tutorial)

The following two id selectors, the first one can define the color of the element as red, and the second one can define the color of the 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, and the p element whose id attribute is green is displayed in green.

<p id="red">这个段落是红色。</p>
<p id="green">这个段落是绿色。</p>

Note: The id attribute can only appear once in each HTML document.

Individual selector

The id selector can function independently even if it is not used to create a derived selector:

#sidebar {
border: 1px dotted #000;
padding: 10px;
}

According to According to this rule, the element with the id of sidebar will have a pixel-wide black dotted border, and there will be 10 pixels of padding around it.

Older versions of Windows/IE browsers may ignore this rule unless you specifically define the element to which this selector belongs:

div#sidebar {
border: 1px dotted #000;
padding: 10px;
}

The above is the detailed content of How to define CSS id selector. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn