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

How to define CSS id selector

(*-*)浩
Release: 2019-11-23 18:01:31
Original
4119 people have browsed it

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;}
Copy after login

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>
Copy after login

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;
}
Copy after login

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;
}
Copy after login

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!

Related labels:
css
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template