Welcome back! In our previous lesson, we reviewed how to structure content with semantic HTML and control layout behavior using the CSS display property. These concepts are the first step in controlling how elements appear on a page. Now, we'll build directly on that foundation to tackle two more fundamental CSS topics that are cornerstones of the QCM exam: selector specificity and the box model.
This lesson will teach you the precise rules the browser uses to decide which CSS style to apply when multiple rules conflict. Then, we will dissect the CSS box model, which governs the sizing and spacing of every element on the page. Mastering these concepts is not just about writing better CSS; it's about being able to predict the outcome of CSS code snippets, a skill that is frequently tested in technical interviews and multiple-choice exams.
Resolving Conflicts: CSS Selector Specificity
When you write CSS, it's common to have several rules that could potentially apply to the same HTML element. For example, you might have a general rule for all paragraphs (p) and a more specific one for paragraphs with a certain class (p.highlight). CSS specificity is the algorithm browsers use to determine which rule is the most "specific" and therefore wins out.
The hierarchy of specificity can be visualized as a set of weights. From highest to lowest, the order is:
- Inline styles (e.g.,
<p style="color: red;">) - IDs (e.g.,
#main-content) - Classes, attributes, and pseudo-classes (e.g.,
.my-class,[type="text"],:hover) - Elements and pseudo-elements (e.g.,
p,::before)

To truly understand how this calculation works in practice, the following video provides a clear, step-by-step tutorial with excellent examples.
Understanding Specificity in CSS - Tutorial
This video from dcode will walk you through the "point system" of specificity, how to compare selectors, and how special cases like !important and inline styles are handled.
Please watch these key segments: What is specificity?: An introduction to the concept. The Point System: This is the core of the lesson, explaining how values are assigned and compared. Tie-breaking: Learn what happens when two selectors have the exact same specificity. The !important Rule: Understand how this declaration breaks the normal specificity rules. Inline Styles: See where inline styles fit into the hierarchy and how they interact with !important.
To reinforce what you've learned from the video, a concise written explanation can be very helpful for revision. The "Front End Interview Handbook" offers a perfect summary.
CSS Interview Questions (Quiz) for Front End Interviews
This text provides a formal definition of specificity, including the four-value matrix (a, b, c, d) often cited in technical documentation. It's a great way to solidify the rules in your mind.
In the "CSS Interview Questions" article, please read the section titled "What is CSS selector specificity and how does it work?". Focus on how the matrix of values is compared from left to right.
A few key points about specificity that are crucial for QCMs:
- The universal selector (
*), combinators (>,+,~), and the negation pseudo-class (:not()) do not contribute to specificity themselves. However, the selectors inside:not()do. - A single ID will always beat any number of classes. A specificity of
0,1,0,0(one ID) is higher than0,0,10,10(ten classes and ten elements). - If multiple rules have the same specificity, the one that appears last in the CSS file wins. This is known as the "last-declared rule."
Everything is a Box: The CSS Box Model
Every element you create in HTML is rendered by the browser as a rectangular box. The CSS Box Model is the set of rules that defines how this box is structured and sized. It consists of four distinct layers, stacked from the inside out.

The four layers are:
- Content: The area containing the actual content, like text or an image. Its dimensions are set by the
widthandheightproperties. - Padding: The transparent space between the content and the border. It's used to give the content "breathing room" inside its container.
- Border: A line that is drawn around the padding. It has properties for
border-width,border-style, andborder-color. - Margin: The transparent space outside the border. It's used to push other elements away, creating space between boxes.
The following video provides an excellent, practical walkthrough of these properties and, most importantly, explains the critical box-sizing property.
Learn CSS BOX MODEL - With Real World Examples
This tutorial from Slaying The Dragon clearly explains each part of the box model and demonstrates the difference between content-box and border-box, which is one of the most important CSS concepts for creating predictable layouts.
Please watch the following segments: Box Model Components: A quick introduction to the four layers: content, padding, border, and margin. Applying Box Model Properties: A practical demonstration of setting width, height, padding, border, and margin, including the useful shorthand notations. The box-sizing Property: This is the most critical part of the video. Pay close attention to the difference between the default content-box and the much more intuitive border-box. The border-box Reset: Learn the standard best practice for applying border-box to all elements for consistent layout behavior.
To supplement the video, the Mozilla Developer Network (MDN) provides the definitive guide to the box model. Reading through it will give you a deeper, more technical understanding.
The box model - Learn web development | MDN
MDN is the gold standard for web documentation. This article details the standard and alternative box models and explains more advanced concepts like margin collapsing.
Please read the following sections: Start with "Parts of a box" for a quick recap of the layers. Read "The standard CSS box model" to understand the default behavior (box-sizing: content-box). Note the calculation for the element's total size. Next, read "The alternative CSS box model" to see how box-sizing: border-box makes sizing predictable. Finally, skim the section on "Margins, padding, and borders". Pay special attention to the sub-section on Margin collapsing, as this behavior is a common topic in exam questions.
Key Takeaways
This lesson covered the rules that govern how CSS styles are applied and how elements are sized. For your exam, focus on being able to answer questions like "Which color will this text be?" or "What is the final width of this div on the screen?".
- Specificity Hierarchy: Remember the order: Inline style > ID > Class / Attribute / Pseudo-class > Element / Pseudo-element.
- Specificity Calculation: A more specific selector (e.g., an ID) will always beat any number of less specific selectors (e.g., classes).
- The Box Model Layers: Every element has content, padding, border, and margin.
box-sizingis Key:content-box(default):width+padding+border= rendered width.border-box:width= rendered width (padding and border are included inside). This is the modern standard.
Next Steps
We have now solidified the core HTML and CSS concepts most likely to appear on your exam. In the next lesson, we will shift our focus from structure and styling to behavior by diving into JavaScript. We'll start with the building blocks: distinguishing between primitive and object data types and understanding the rules of implicit type coercion.