CSS Selectors

Selectors are used to target HTML elements you want to style. Common types include:

Element Selector: Targets all elements of a specific type.


p {
    color: green;
}

Try it Yourself

Class Selector: Targets elements with a specific class attribute.


.className {
    color: blue;
}

Try it Yourself

ID Selector: Targets an element with a specific ID attribute.


#idname {
    color: green;
}

Try it Yourself

Universal Selector: Targets all elements.


* {
    color: blue;
}

Try it Yourself

Attribute Selector: Targets elements with a specific attribute.


[type="text"] {
    border: 2px solid black;
}

Try it Yourself

Pseudo-class Selector: Targets elements in a specific state.


a:hover {
    color: green;
}

Try it Yourself

Properties and Values


body {
    background-color: lightblue;
    font-size: 16px;
    margin: 0;
    padding: 0;
}

Combining Selectors

Selectors can be combined to target elements more precisely.

Descendant Selector: Targets elements that are descendants of a specified element.


div p {
    color: green;
}

Try it Yourself

Child Selector: Targets direct child elements of a specified element.


div > p {
    color: blue;
}

Try it Yourself

CSS Selectors – Interview Questions

Q 1: What are CSS selectors?

Ans: CSS selectors are used to select HTML elements for styling.

Q 2: Name common types of CSS selectors.

Ans: Element, Class, ID, Universal, and Attribute selectors.

Q 3: What is the class selector?

Ans: It selects elements using .classname.

Q 4: What is the ID selector?

Ans: It selects a unique element using #idname.

Q 5: Which selector has higher priority: class or ID?

Ans: ID selector has higher priority.

CSS Selectors – Objective Questions (MCQs)

Q1. Which selector selects all elements?






Q2. Which selector is used for class?






Q3. Which selector is used for ID?






Q4. Which selector selects all elements?






Q5. Which selector selects elements inside another element?






Related CSS Selectors Topics