CSS Specificity and Inheritance

Tutorial 4 of 5

CSS Specificity and Inheritance Tutorial

1. Introduction

In this tutorial, we will delve into the critical aspects of CSS (Cascading Style Sheets) specificity and inheritance. These concepts play a crucial role in controlling how styles are applied to elements on a web page. By the end of this tutorial, you will have a clear understanding of CSS specificity and inheritance, and you will be able to manage style conflicts more effectively.

Goals of this Tutorial:
- Understand CSS specificity and inheritance.
- Learn how to resolve style conflicts.
- Promote effective, consistent styling across a web page.

Prerequisites:
- Basic knowledge of HTML and CSS.

2. Step-by-Step Guide

CSS Specificity

CSS Specificity is a set of rules that browsers follow to decide which CSS styles are applied to elements. The specificity is calculated based on CSS selectors. The higher the specificity, the higher the priority.

Specificity Hierarchy:
- Inline styles (highest specificity)
- IDs
- Classes, attributes, and pseudo-classes
- Elements and pseudo-elements (lowest specificity)

CSS Inheritance

CSS Inheritance is a key feature of CSS. It controls how properties are passed from parent elements to their children. Some properties are inherited by default, while others are not.

Best Practices and Tips:
- Avoid using !important. It overrides any other declarations and can make debugging difficult.
- Be as specific as possible when targeting elements.
- Use inheritance to your advantage to reduce redundant code.

3. Code Examples

Example 1: CSS Specificity

/* Element selector (Low Specificity) */
p {
    color: blue;
}

/* Class selector (Higher Specificity) */
p.intro {
    color: green;
}

/* ID selector (Highest Specificity) */
p#unique {
    color: red;
}

In the example above, assuming all three selectors are targeting the same <p> element, the color will be red because the ID selector has the highest specificity.

Example 2: CSS Inheritance

/* Parent element */
div {
    color: blue;
}

/* Child element */
p {
    color: inherit;
}

In this example, the <p> element will inherit the color property of its parent <div> element, so the text color will be blue.

4. Summary

In this tutorial, we covered CSS specificity and inheritance. We learned how CSS specificity is calculated and how it impacts which styles are applied to elements. We also explored CSS inheritance and how properties are passed from parent elements to child elements.

To further deepen your understanding, try out different CSS selectors and experiment with inherited properties.

Additional Resources:
- CSS Specificity: Things You Should Know
- CSS Inheritance, Cascade and Specificity

5. Practice Exercises

Exercise 1: Style a <p> element with an ID of first and a class of intro such that the color of the text is red.

Solution:

p#first.intro {
    color: red;
}

Exercise 2: Create a div with a class parent that has a color of blue. Inside this div, create a <p> element with a class child that inherits the color from its parent.

Solution:

.parent {
    color: blue;
}

.child {
    color: inherit;
}

In the above solution, the <p> element with the class child will have a text color of blue, inherited from its parent div.

Tips for further practice: Experiment with different types of selectors and try combining them to see how specificity works. Try to use inheritance to reduce code redundancy.