Explore 100's Of Free Video Tutorials

Keep Learning, Keep Coding & Keep Sharing

CSS

CSS id selector

Notes:

CSS id selector demo:
To select tags by their “id attribute value” and apply styles on them we use id selector.

Syntax of CSS rule-set / rule:
selector
{
declaration list;
}

To implement id selector, In place of selector, we write id attribute value preceded by hash(#) symbol. In CSS # symbol indicates ID selector.

Syntax of id selector:
tagname id=”idattributevalue”
#idattributevalue
{
declaration list;
}
It selects every html tag which has specified id attribute value and applies styles on them.

Example for id selector:
h1 id=”solidborder”
# solidborder
{
border: 2px solid red;
}
It selects every html tag available on the page with a specified id attribute value and applies specified styles on them.

You should not specify same id attribute value to more than one tag.
(I.e. id attribute value should be unique in the page).
If you want to identify an html element uniquely in the page use id selector.

An html element should not have list of id attribute values separated by white space.
ID attribute value is not only used to identify an html element uniquely and apply styles on it.
It is also used access an html element in JavaScript and allows us to add behavior to it.

Interview Questions:

1. To select tags by their id attribute value which selector is used?
a. class selector
b. id selector
c. tag selector
d. sibling selector
Answer: b