CSS / CSS Selectors and Specificity

Using Basic CSS Selectors

This tutorial provides a practical introduction to using basic CSS selectors like type, class, and ID selectors. You'll learn how to target and style webpage elements using these …

Tutorial 1 of 5 5 resources in this section

Section overview

5 resources

Explains various CSS selectors and how specificity affects element styling.

Introduction

Goal of the Tutorial

This tutorial aims to give you a hands-on introduction to using basic CSS selectors. CSS selectors are the engine behind CSS - they determine how styles are applied to elements on a webpage.

Learning Outcome

By the end of this tutorial, you will have understood how to use type, class, and ID selectors in CSS, with the ability to target and style webpage elements using these foundational selectors.

Prerequisites

Basic knowledge of HTML and CSS is advised, but not strictly necessary.

Step-by-Step Guide

Understanding CSS Selectors

CSS selectors are used to "find" (or select) HTML elements based on their element name, id, class, attribute, and more.

The three basic selectors in CSS are:

  1. Type Selectors: These target elements by their node name.

  2. Class Selectors: These target elements by the class attribute. They're defined with a '.' followed by the class name.

  3. ID Selectors: These target elements by the id attribute. They're defined with a '#' followed by the id name.

Best Practices

  • Use type selectors for global styles.
  • Use class selectors when you want to style a group of elements.
  • Use ID selectors for unique elements.

Code Examples

Type Selector

/* This will style all the <h1> elements */
h1 {
  color: red;
}

Class Selector

/* This will style any element with class="intro" */
.intro {
  font-size: 20px;
}

ID Selector

/* This will style the element with id="first" */
#first {
  color: blue;
}

Summary

We've covered the basics of CSS selectors - the type, class, and id selectors. Remember:

  • Type selectors target by node name
  • Class selectors target by the class attribute
  • ID selectors target by the id attribute

For further learning, consider studying more advanced selectors like attribute selectors, pseudo-class selectors, and pseudo-element selectors.

Practice Exercises

  1. Exercise 1: Style all paragraphs (<p>) to have a font size of 18px.
  2. Exercise 2: Style any element with a class of .highlight to have a yellow background.
  3. Exercise 3: Style an element with an id of #main-title to be bold and underlined.

Solutions

  1. Solution 1:
p {
  font-size: 18px;
}
  1. Solution 2:
.highlight {
  background-color: yellow;
}
  1. Solution 3:
#main-title {
  font-weight: bold;
  text-decoration: underline;
}

Continue practicing with different HTML elements, classes, and ids to get a better grasp of CSS selectors. Happy styling!

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

PDF Compressor

Reduce the size of PDF files without losing quality.

Use tool

Random Name Generator

Generate realistic names with customizable options.

Use tool

Favicon Generator

Create favicons from images.

Use tool

File Size Checker

Check the size of uploaded files.

Use tool

PDF to Word Converter

Convert PDF files to editable Word documents.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help