CSS ID – Creating and using CSS ID

CSS ID Selector

In CSS you can also define your own selectors. These selectors are Class and ID selectors. This chapter explains only CSS ID selectors.

Alternatively we can select and style HTML elements by ID names. ID selectors are used when you need to apply style to single element.

Syntax of defining ID selector

The ID selector is defined as follows:

#ID_name

{

property: value;

property: value;

property: value;

}

i.e. a hash-tag (#) followed by id selector name. All definitions are enclosed in curly braces.

Example of defining and using ID selector

In example below we will define a test ID selector with certain properties. Then we will use this ID selector in HTML DIV element.

Experience this example online

As you can see, once a testid is defined this is used in <div> HTML element.

In HTML elements, id selector is referred by id=”idname”.