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;
}
Class Selector: Targets elements with a specific class attribute.
.className {
color: blue;
}
ID Selector: Targets an element with a specific ID attribute.
#idname {
color: green;
}
Universal Selector: Targets all elements.
* {
color: blue;
}
Attribute Selector: Targets elements with a specific attribute.
[type="text"] {
border: 2px solid black;
}
Pseudo-class Selector: Targets elements in a specific state.
a:hover {
color: green;
}
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;
}
Child Selector: Targets direct child elements of a specified element.
div > p {
color: blue;
}
CSS Selectors – Interview Questions
Q 1: What are CSS selectors?
Q 2: Name common types of CSS selectors.
Q 3: What is the class selector?
Q 4: What is the ID selector?
Q 5: Which selector has higher priority: class or ID?
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?