Web Technology - Old Questions

6. What is CSS selector? Compare id selector with class selector.

5 marks | Asked in Model Question

A CSS selector is the part of a CSS rule set that actually selects the content we want to style. In CSS, selectors are used to target the HTML elements on our web pages that we want to style.

E.g.

Introduction to CSS ยป WebNots

Comparison between id selector and class selector

id selectorclass selector
A selector in CSS that styles the element with a specified id.A selector in CSS that styles the element with a specified class.

Syntax is:

#id{

       css declarations;

    }

Syntax is:

.class{

       css declarations;

    }

Used to apply styling to one specific element.Used to apply styling to multiple elements.
It is less used.It is most used on several elements.

E.g.

#main {

    background: #000;

    padding: 10px;

    color: #fff;

    width: 100px;

}


<div id="main">

    Welcome

</div>

E.g.

.main {

    background: #000;

    padding: 10px;

    color: #fff;

    width: 100px;

}


<div class="main">

    Welcome

</div>