Practice Questions — CSS Selectors and Specificity
← Back to NotesTopic-Specific Questions
Question 1
Easy
Which selector targets all paragraphs?
Use the tag name.
pQuestion 2
Easy
Which symbol starts a class selector?
Dot.
. (period)Question 3
Easy
Which symbol starts an ID selector?
Hash.
# (hash)Question 4
Easy
Write CSS to make all elements with class 'danger' red.
Use .danger.
.danger {
color: red;
}Question 5
Easy
Write CSS to style the element with id 'header' with a blue background.
Use #header.
#header {
background-color: blue;
}Question 6
Easy
Which has higher specificity:
p or .note?Classes beat elements.
.note (class specificity 10 > element specificity 1).Question 7
Easy
Which selector matches every element on the page?
An asterisk.
* (universal selector)Question 8
Easy
Write CSS to remove the margin and padding from every element on the page.
Use the universal selector.
* {
margin: 0;
padding: 0;
}Question 9
Easy
What does
ul li mean?Space is the descendant selector.
Every
<li> that is inside a <ul>, at any depth.Question 10
Easy
What is the difference between
ul li and ul > li?Child vs descendant.
ul li matches all li inside ul at any depth. ul > li matches only DIRECT li children of ul.Question 11
Easy
Write CSS to style all paragraphs inside articles with a grey color.
Use descendant selector.
article p {
color: grey;
}Question 12
Easy
What is the specificity of
#main?IDs are 100.
100
Question 13
Easy
Why does this CSS not style the element?
highlight {
color: yellow;
}Missing symbol.
Missing dot. Should be
.highlight { color: yellow; }Question 14
Easy
Write CSS to group h1, h2, and h3 and give them all a red color.
Use commas.
h1, h2, h3 {
color: red;
}Question 15
Medium
What does
h1 + p match?Adjacent sibling.
The first
<p> element that comes immediately after an <h1>, sharing the same parent.Question 16
Medium
What does
h1 ~ p match?General sibling.
Every
<p> that comes after an <h1>, sharing the same parent (not just the first one).Question 17
Medium
Write CSS to target only the
li elements that are direct children of nav (not nested ones).Use the child combinator.
nav > li {
color: blue;
}Question 18
Medium
Which of these has higher specificity:
#main or .btn.large?ID is 100; two classes are 20.
#main (100 > 20).Question 19
Medium
Write a complete HTML page where every other list item has a grey background using nth-child.
Use li:nth-child(even).
<!DOCTYPE html>
<html>
<head>
<style>
li:nth-child(even) {
background-color: lightgrey;
}
</style>
</head>
<body>
<ul>
<li>One</li>
<li>Two</li>
<li>Three</li>
<li>Four</li>
</ul>
</body>
</html>Question 20
Medium
Given
.red { color: red; } and p { color: blue; }, what color is <p class="red">Text</p>?Class wins over element.
Red. The class (10) beats the element (1).
Question 21
Medium
Write CSS using attribute selector to make all email inputs have a green border.
Use [type="email"].
input[type="email"] {
border: 2px solid green;
}Question 22
Medium
Priya's CSS does not work. Why?
ul > li li {
color: red;
}Actually, it might work — analyse it.
The CSS is valid. It targets every li that is a descendant of a direct li child of ul. So it styles nested li elements.
Question 23
Medium
Write CSS targeting the first li in a list using the :first-child pseudo-class.
Use li:first-child.
li:first-child {
font-weight: bold;
}Question 24
Medium
What is the specificity of
#main .card p?Add up: 100 + 10 + 1.
111
Question 25
Hard
Build a complete HTML page where links to PDFs show in bold red with a 'PDF' label automatically. Use an attribute selector.
Use a[href$=".pdf"].
<!DOCTYPE html>
<html>
<head>
<style>
a[href$=".pdf"] {
color: red;
font-weight: bold;
}
</style>
</head>
<body>
<p><a href="https://example.com">Regular link</a></p>
<p><a href="document.pdf">Download PDF</a></p>
</body>
</html>Question 26
Hard
Given:
p { color: red; } p.note { color: blue; } #special { color: green; }. What color is <p id="special" class="note">Text</p>?Compute specificity for each.
Green. #special has specificity 100, p.note has 11, p has 1. #special wins.
Question 27
Hard
Write CSS to target every third item in a list using nth-child.
Use 3n.
li:nth-child(3n) {
color: purple;
}Question 28
Hard
The text should be green, but it shows red. Why?
<style>
.note { color: green; }
</style>
<p class="note" style="color: red;">Text</p>Inline CSS.
Inline styles have specificity 1000, which beats the class's 10. Inline CSS wins.
Mixed & Application Questions
Question 1
Easy
What does the universal selector * target?
Everything.
Every element on the page.
Question 2
Easy
Write a class selector to make all elements with class 'big' have a 24px font size.
Start with a dot.
.big {
font-size: 24px;
}Question 3
Easy
Can multiple elements share the same class?
Yes.
Yes, classes are designed to be reused on many elements.
Question 4
Easy
Can multiple elements share the same ID?
No.
No, IDs must be unique on a page.
Question 5
Easy
Why is this class selector wrong?
#highlight { color: yellow; }Wrong symbol.
If highlight is a class, use
.highlight with a dot, not #.Question 6
Medium
Write CSS to style all elements with class 'card' AND also have the class 'featured'.
Chain the class selectors without a space.
.card.featured {
border: 3px solid gold;
}Question 7
Medium
What is the specificity of
.btn.large?Two classes.
20
Question 8
Medium
Write a CSS rule to target all buttons that have the 'disabled' attribute.
Use [disabled].
button[disabled] {
opacity: 0.5;
cursor: not-allowed;
}Question 9
Medium
What does
a[href^="https"] match?^ means starts with.
All
<a> tags whose href attribute starts with 'https'.Question 10
Medium
The second-level list items are also being styled, but Aarav only wants the top-level ones. Fix:
.menu li { color: red; }Change descendant to child.
Use
.menu > li to target only direct children.Question 11
Medium
Write a complete HTML page styling the first letter of a paragraph in red with a bigger font. Use ::first-letter pseudo-element.
Use p::first-letter.
<!DOCTYPE html>
<html>
<head>
<style>
p::first-letter {
color: red;
font-size: 32px;
font-weight: bold;
}
</style>
</head>
<body>
<p>This paragraph has a fancy first letter.</p>
</body>
</html>Question 12
Hard
Given:
p { color: red !important; } #id p { color: blue; }. What wins?!important trumps specificity.
Red wins because !important overrides specificity.
Question 13
Hard
Build a complete HTML page with a styled nav menu where only the FIRST link is blue (use :first-child).
Use li:first-child a.
<!DOCTYPE html>
<html>
<head>
<style>
nav ul { list-style: none; display: flex; gap: 20px; }
nav li:first-child a { color: blue; font-weight: bold; }
</style>
</head>
<body>
<nav>
<ul>
<li><a href="/">Home</a></li>
<li><a href="/about">About</a></li>
<li><a href="/contact">Contact</a></li>
</ul>
</nav>
</body>
</html>Question 14
Hard
What is the specificity of
body #main .card p.bold?Count each type: 1 element + 1 ID + 2 classes + 1 element.
112 (1 element + 100 ID + 10 class + 10 class + 1 element).
Question 15
Hard
Aarav adds a class to override a style but it does not work. Fix:
<style>
#main p { color: red; }
.override { color: blue; }
</style>
<div id="main">
<p class="override">Text</p>
</div>ID has higher specificity.
#main p has specificity 101, .override has 10. The ID wins. To fix, use #main p.override or add more specificity.
Multiple Choice Questions
MCQ 1
Which symbol starts a class selector?
Answer: B
B is correct. Class selectors start with a dot:
B is correct. Class selectors start with a dot:
.classname. The # is for IDs.MCQ 2
Which symbol starts an ID selector?
Answer: B
B is correct. ID selectors start with a hash:
B is correct. ID selectors start with a hash:
#idname. The . is for classes.MCQ 3
What does the universal selector * do?
Answer: B
B is correct. The asterisk matches every element. Used in CSS resets to clear default margins and padding.
B is correct. The asterisk matches every element. Used in CSS resets to clear default margins and padding.
MCQ 4
Which has higher specificity: a class or an element selector?
Answer: B
B is correct. Classes have specificity 10, elements have specificity 1. Classes win when both apply.
B is correct. Classes have specificity 10, elements have specificity 1. Classes win when both apply.
MCQ 5
What does
ul li target?Answer: B
B is correct. The space is the descendant selector, matching elements at any depth inside their ancestor.
B is correct. The space is the descendant selector, matching elements at any depth inside their ancestor.
MCQ 6
What does
ul > li target?Answer: B
B is correct. The > combinator means 'direct child only'. Nested li elements are not matched.
B is correct. The > combinator means 'direct child only'. Nested li elements are not matched.
MCQ 7
How do you group selectors to share the same rule?
Answer: C
C is correct. Commas group selectors:
C is correct. Commas group selectors:
h1, h2, h3 { color: red; }MCQ 8
Can multiple HTML elements share the same ID?
Answer: B
B is correct. IDs are meant to be unique. If you need to target multiple elements, use a class instead.
B is correct. IDs are meant to be unique. If you need to target multiple elements, use a class instead.
MCQ 9
What is the specificity of an element selector like
p?Answer: B
B is correct. Element selectors have specificity 1. Classes are 10, IDs are 100, inline is 1000.
B is correct. Element selectors have specificity 1. Classes are 10, IDs are 100, inline is 1000.
MCQ 10
What is the specificity of a class selector like
.btn?Answer: B
B is correct. Classes, attributes, and pseudo-classes all have specificity 10.
B is correct. Classes, attributes, and pseudo-classes all have specificity 10.
MCQ 11
What is the specificity of an ID selector like
#main?Answer: C
C is correct. ID selectors have specificity 100. Only inline styles and !important can normally override them.
C is correct. ID selectors have specificity 100. Only inline styles and !important can normally override them.
MCQ 12
Which selector targets all input elements with type='email'?
Answer: C
C is correct. Attribute selectors use square brackets:
C is correct. Attribute selectors use square brackets:
input[type='email']MCQ 13
What does
h1 + p select?Answer: C
C is correct. The + is the adjacent sibling combinator. It matches the FIRST p immediately after each h1.
C is correct. The + is the adjacent sibling combinator. It matches the FIRST p immediately after each h1.
MCQ 14
What does
h1 ~ p select?Answer: B
B is correct. The ~ is the general sibling combinator. It matches every p that comes after an h1 (same parent).
B is correct. The ~ is the general sibling combinator. It matches every p that comes after an h1 (same parent).
MCQ 15
Which has higher specificity:
.btn or button?Answer: B
B is correct. Class (10) beats element (1).
B is correct. Class (10) beats element (1).
.btn wins.MCQ 16
What is the specificity of
#main .card p?Answer: B
B is correct. 1 ID (100) + 1 class (10) + 1 element (1) = 111.
B is correct. 1 ID (100) + 1 class (10) + 1 element (1) = 111.
MCQ 17
What does
li:nth-child(even) target?Answer: C
C is correct.
C is correct.
:nth-child(even) matches even-numbered children (2nd, 4th, 6th, etc.).MCQ 18
Which of the following rules applies to
<p class="red" id="special">Hi</p>: .red { color: red; } #special { color: blue; }Answer: B
B is correct. #special has specificity 100, .red has 10. The ID wins. Blue paragraph.
B is correct. #special has specificity 100, .red has 10. The ID wins. Blue paragraph.
MCQ 19
What does
a[href^="https"] target?Answer: B
B is correct.
B is correct.
^= means 'starts with'. Use $= for 'ends with' and *= for 'contains'.MCQ 20
What is the specificity of an inline style?
Answer: C
C is correct. Inline styles have specificity 1000. Only !important can override them.
C is correct. Inline styles have specificity 1000. Only !important can override them.
MCQ 21
Which statement about !important is TRUE?
Answer: C
C is correct.
C is correct.
!important overrides normal specificity, but overusing it leads to unmaintainable CSS. Use proper specificity instead.MCQ 22
When two CSS rules have the same specificity, which wins?
Answer: B
B is correct. With equal specificity, the later rule wins. This is why order matters in CSS files.
B is correct. With equal specificity, the later rule wins. This is why order matters in CSS files.
MCQ 23
What is the specificity of
.nav > li.active a?Answer: A
A is correct. 1 class (10) + 1 element (1) + 1 class (10) + 1 element (1) = 22. Remember that > is not counted.
A is correct. 1 class (10) + 1 element (1) + 1 class (10) + 1 element (1) = 22. Remember that > is not counted.
MCQ 24
Which property is NOT inherited by default?
Answer: C
C is correct. Margin, padding, border, background — these are not inherited. Text-related properties (color, font, line-height) are inherited.
C is correct. Margin, padding, border, background — these are not inherited. Text-related properties (color, font, line-height) are inherited.
MCQ 25
Which of the following has the HIGHEST specificity?
Answer: D
D is correct. Inline styles have specificity 1000, beating all the others. A has 111, B has 40, C has 105.
D is correct. Inline styles have specificity 1000, beating all the others. A has 111, B has 40, C has 105.
Coding Challenges
Challenge 1: Class and ID Styling
EasyBuild a page with 3 paragraphs. One uses id 'intro' (blue), one uses class 'warning' (red), and one has no class or id (default black).
Sample Input
(No input required)
Sample Output
Three paragraphs in blue, red, and black.
Use both class and ID selectors. Internal CSS.
<!DOCTYPE html>
<html>
<head>
<title>Class and ID</title>
<style>
#intro { color: blue; font-size: 20px; }
.warning { color: red; font-weight: bold; }
</style>
</head>
<body>
<p id="intro">Welcome to the page!</p>
<p class="warning">Warning: Please save your work.</p>
<p>This is a normal paragraph.</p>
</body>
</html>Challenge 2: Descendant Selector
EasyBuild a page with an article containing 3 paragraphs, and a separate div with 2 paragraphs. Use descendant selector to make only the paragraphs inside the article red.
Sample Input
(No input required)
Sample Output
Only article paragraphs are red. Div paragraphs stay default.
Use <code>article p</code> as the selector.
<!DOCTYPE html>
<html>
<head>
<title>Descendant Selector</title>
<style>
article p { color: red; }
</style>
</head>
<body>
<article>
<p>Red paragraph 1</p>
<p>Red paragraph 2</p>
<p>Red paragraph 3</p>
</article>
<div>
<p>Normal paragraph</p>
<p>Another normal paragraph</p>
</div>
</body>
</html>Challenge 3: Zebra Stripe Table
MediumBuild a table of 5 student rows (Aarav, Priya, Rohan, Ananya, Vikram) with a zebra stripe pattern using nth-child. Every other row should have a grey background.
Sample Input
(No input required)
Sample Output
A table with alternating white and grey rows.
Use <code>tr:nth-child(even)</code> for striping.
<!DOCTYPE html>
<html>
<head>
<title>Zebra Table</title>
<style>
table { border-collapse: collapse; margin: 20px; font-family: Arial; }
th, td { padding: 10px 15px; border: 1px solid #ccc; }
thead { background-color: #2c3e50; color: white; }
tr:nth-child(even) { background-color: #f5f5f5; }
</style>
</head>
<body>
<table>
<thead>
<tr><th>Name</th><th>Class</th><th>Marks</th></tr>
</thead>
<tbody>
<tr><td>Aarav</td><td>8A</td><td>92</td></tr>
<tr><td>Priya</td><td>8A</td><td>95</td></tr>
<tr><td>Rohan</td><td>8B</td><td>78</td></tr>
<tr><td>Ananya</td><td>8A</td><td>88</td></tr>
<tr><td>Vikram</td><td>8B</td><td>85</td></tr>
</tbody>
</table>
</body>
</html>Challenge 4: Navigation with First-child
MediumBuild a horizontal navigation menu with 5 links. Use :first-child to style the first link with a highlight color, and :last-child to style the last link with a different color.
Sample Input
(No input required)
Sample Output
5 nav links where the first is one color and the last is another color.
Use :first-child and :last-child pseudo-classes.
<!DOCTYPE html>
<html>
<head>
<title>Nav</title>
<style>
nav ul { list-style: none; display: flex; gap: 20px; padding: 20px; background: #333; }
nav a { color: white; text-decoration: none; padding: 8px 12px; }
nav li:first-child a { background: #ff4081; border-radius: 5px; }
nav li:last-child a { background: #22c55e; border-radius: 5px; }
</style>
</head>
<body>
<nav>
<ul>
<li><a href="/">Home</a></li>
<li><a href="/about">About</a></li>
<li><a href="/blog">Blog</a></li>
<li><a href="/work">Work</a></li>
<li><a href="/contact">Contact</a></li>
</ul>
</nav>
</body>
</html>Challenge 5: Attribute Selector for Inputs
MediumBuild a form with text, email, password, and number inputs. Use attribute selectors to style each type with a different border color (blue, green, red, purple).
Sample Input
(No input required)
Sample Output
Four inputs with differently-colored borders based on type.
Use <code>input[type='...']</code> for each.
<!DOCTYPE html>
<html>
<head>
<title>Attribute Selectors</title>
<style>
body { font-family: Arial; padding: 30px; }
label { display: block; margin-top: 10px; }
input { padding: 8px; font-size: 16px; }
input[type="text"] { border: 2px solid blue; }
input[type="email"] { border: 2px solid green; }
input[type="password"] { border: 2px solid red; }
input[type="number"] { border: 2px solid purple; }
</style>
</head>
<body>
<form>
<label>Name: <input type="text"></label>
<label>Email: <input type="email"></label>
<label>Password: <input type="password"></label>
<label>Age: <input type="number"></label>
</form>
</body>
</html>Challenge 6: Specificity Demo
MediumBuild a page that demonstrates specificity: create one paragraph that has an ID, a class, and an inline style. Each rule specifies a different color. Show which one wins.
Sample Input
(No input required)
Sample Output
A paragraph where the inline style wins over the ID and class.
Include 4 different color rules at different specificities. Comment each rule with its specificity score.
<!DOCTYPE html>
<html>
<head>
<title>Specificity Demo</title>
<style>
/* Specificity 1 */
p { color: black; }
/* Specificity 10 */
.note { color: green; }
/* Specificity 100 */
#special { color: purple; }
/* Specificity 111 */
p#special.note { color: orange; }
</style>
</head>
<body>
<p>Plain (black, specificity 1)</p>
<p class="note">Class only (green, specificity 10)</p>
<p id="special">ID only (purple, specificity 100)</p>
<p id="special" class="note" style="color: red;">All + inline (red wins, specificity 1000)</p>
</body>
</html>Challenge 7: Advanced Selector Combo
HardBuild a blog post page. Style: (1) only the first paragraph after h1 with a larger font, (2) every link that ends with .pdf in red bold, (3) every third li in a list with a grey background.
Sample Input
(No input required)
Sample Output
A page showing all three selector techniques in action.
Use adjacent sibling (+), attribute selector ($=), and nth-child.
<!DOCTYPE html>
<html>
<head>
<title>Blog Post</title>
<style>
body { font-family: Arial; max-width: 700px; margin: auto; padding: 30px; }
h1 + p {
font-size: 22px;
font-weight: bold;
color: #2c3e50;
}
a[href$=".pdf"] {
color: red;
font-weight: bold;
}
li:nth-child(3n) {
background-color: #f0f0f0;
padding: 5px;
}
</style>
</head>
<body>
<h1>Learning CSS Selectors</h1>
<p>This is the first paragraph (bold and bigger).</p>
<p>This is a normal paragraph.</p>
<p>Another normal paragraph.</p>
<ul>
<li>One</li>
<li>Two</li>
<li>Three (grey)</li>
<li>Four</li>
<li>Five</li>
<li>Six (grey)</li>
</ul>
<p>Download: <a href="guide.pdf">CSS Guide PDF</a></p>
<p>Visit: <a href="https://example.com">Example Site</a></p>
</body>
</html>Challenge 8: Complete Card Grid with Variants
HardBuild a page with 6 card divs. Each card has a class 'card'. Two cards also have class 'featured'. One card has class 'premium'. Style all cards, then use chained selectors (.card.featured and .card.premium) to apply extra styles to the special ones.
Sample Input
(No input required)
Sample Output
A grid of 6 cards where 2 featured cards and 1 premium card look different from the rest.
Use chained class selectors without space to apply to elements with multiple classes.
<!DOCTYPE html>
<html>
<head>
<title>Card Grid</title>
<style>
body { font-family: Arial; background: #eee; padding: 20px; }
.grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 15px; max-width: 800px; margin: auto; }
.card {
background: white;
padding: 20px;
border-radius: 8px;
border: 2px solid #ddd;
}
.card.featured {
border-color: #ff4081;
background: #fff0f5;
}
.card.premium {
border-color: gold;
background: #fffacd;
box-shadow: 0 0 20px gold;
}
h3 { margin: 0 0 10px; }
</style>
</head>
<body>
<div class="grid">
<div class="card"><h3>Card 1</h3><p>Normal card</p></div>
<div class="card featured"><h3>Card 2</h3><p>Featured card (pink)</p></div>
<div class="card"><h3>Card 3</h3><p>Normal card</p></div>
<div class="card premium"><h3>Card 4</h3><p>Premium card (gold glow)</p></div>
<div class="card featured"><h3>Card 5</h3><p>Another featured</p></div>
<div class="card"><h3>Card 6</h3><p>Normal card</p></div>
</div>
</body>
</html>Need to Review the Concepts?
Go back to the detailed notes for this chapter.
Read Chapter NotesWant to learn web development with a live mentor?
Explore our Frontend Masterclass