Menu

Log in

Sign up

From beginner to master of web design, coding, infrastructure operation, business development and marketing

  • COURSES
  • HTML & CSS Introduction
  • HTML & CSS Coding with AI
  • Linux Introduction
  • Docker Basics
  • Git & GitHub Introduction
  • JavaScript Coding with AI
  • Django Introduction
  • AWS Basics
  • Figma Introduction
  • SEO Tutorial for Beginners
  • SEO with AI
  • OTHERS
  • About
  • Terms of Service
  • Privacy Policy

© 2024 D-Libro. All Rights Reserved

HTML & CSS IntroductionChapter 8. CSS Basics

CSS Basic Selectors

CSS Basic Selectors

The Essentials of CSS Basic Selectors

Utilizing various CSS selectors is one of the most important skills of CSS coding. There are several CSS selectors. The most basic ones are type selector, class selector, and ID selector. In this section, we'll explain three basic CSS selectors.

1. Type selector

This selector is the simplest selector. Use the element name (or tag name) to select elements in the HTML documents which you want to style. The elements with the same tag name will be styled in this approach. You can utilize this approach to create a standard CSS template; however, this approach is not useful for highly customized web pages as the same styles are applied to all the same-name tags.

IdeaTips: Selecting multiple elements

You can select multiple elements for the same declaration. Use "," comma to separate selector names like shown below.

Type selector - selecting multiple elements

2. Class selector

This selector gives you more flexibility in styling. Using a class selector, you can apply different styles to elements with the same tag name. For example, you can make some selected <h3> elements colored in blue while other <h3> elements colored in red. There are two steps to set a class sector.

1. Assign class attribute value in HTML document

Add a class attribute value in the start tag of the target HTML element.

HTML Example:
<li class="nav-link">xxx </li>

2. Apply CSS property and property value

Set CSS using the class selector you defined. For the class selector, you need to start with "." period.

CSS Example:
.nav-link{
  color: blue;
}

3. ID selector

This selector is similar to the class selector but it can be used for a more specific element. You can use the same class selector multiple times in the same HTML document; however, you can use the same id selector only once in the same HTML document. This is because the id is used to locate a unique element in the HTML document.

Class selector and ID selector

There are two steps to set an ID sector.

1. Assign id in HTML document

Similarly to the class attribute, add an id to the target HTML element.

HTML Example
<p id="highlight">xxx </p>

2. Apply CSS property and property value

Set CSS using the id selector you defined. For ID selector, you need to start with "#" hash.

CSS Example
#highlight{
  color: red;
}

IdeaTips: Set multiple classes and ID for the same HTML element

You can assign multiple class attributes or a class and id together to one HTML element as in the example below.

HTML Example
<div class="primary small" id="main">

To assign multiple class attributes, you can use a space between them.

Practice

Objective:
Try different selectors

1. Add new code in the body section of the HTML document

  • Open the chapter8.html file with VS Code.
  • Add the following <h2> and <ul> elements before the </body> tag.
chapter8.html
<body>
  <h1><span style="color:red">Chapter 8.</span> CSS Basics</h1>
  <h2 class="first-class">Basic Selectors</h2>
  <ul>
    <li>Type Selector : select elements by tags</li>
    <li class="first-class second-class">Class Selector : select elements by class </li>
    <li id="first-id">ID Selector : select an element by id</li>
  </ul>
</body>

2. Add new code in the CSS document

  • Open the practice.css file with VS Code.
  • Add the following code.
practice.css
li{
  color:blue;
}

.first-class{
  color:red;
font-weight: bold;
}

.second-class{
  background-color:aqua;
}

#first-id{
  color:green;
}

3. Check the result with a browser

  • Open chapter8.html with a browser (make sure that both HTML and CSS files are saved).
  • You can see that various styles were implemented.
How different selectors work

You can also check the sample result here (Demo Site). In this demo site, the background color may be different as we'll update the background color in the later part of this course.

link


You can also learn this topic offline. Click AmazonKindle.

More Topics to Explore

Understanding Component Creation and Styling in HTML & CSS

Chapter 17. Creating and Styling Components

Utilizing Padding in Web Design Layouts

padding

Creating Lists and Tables in HTML

Chapter 5. HTML: Create Lists and Tables

Leveraging GitHub Pages for Website Hosting

GitHub Pages

Understanding Component Creation and Styling in HTML & CSS

Chapter 17. Creating and Styling Components

Utilizing Padding in Web Design Layouts

padding

Creating Lists and Tables in HTML

Chapter 5. HTML: Create Lists and Tables

Leveraging GitHub Pages for Website Hosting

GitHub Pages

Tags:

Selector

Class Selector

Type Selector

ID Selector

HTML & CSS Introduction
Course Content

Chapter 1. Overview of Website Development

How Websites Work?

Designing, Building and Publishing Websites

Designing Websites

Building Websites – Frontend and Backend Coding

Web App vs. Website

Frontend Coding

Web Framework and CMS

Publishing Websites (Hosting Services)

Chapter 2. Preparing for Website Coding

Two Key Tools to Start Coding Websites

How Browsers Display Web Pages

File Name Rules

Website Directory Structure

Absolute Path vs. Relative Path

Chapter 3. HTML Basics

What Is HTML?

HTML Element

Attribute

HTML Document Structure

Layout Semantics

Heading and Paragraph Tag

Text-Level Semantics

Space, Line-break and Tag in Content

Comments in HTML Document

Chapter 4. HTML: Add Links and Images

Overview of Adding Links and Images

Embed Images – <img>

Image File Format

Add Hyperlinks – <a>

Add Hyperlinks to Images

Add Hyperlinks to Specific Location on Web Page

Link Tag – <link>

Script Tag – <script>

Chapter 5. HTML: Create Lists and Tables

Create Lists

Create Tables

Combine Table Cells

Chapter 6. HTML: Create Forms

Create Forms

Create Text Input Forms and Submit Button

Radio Button and Checkbox

Select Box

Labels

Auto Complete and Disabled

Chapter 7. Bridging HTML and CSS

Block Element vs. Inline Element

Nesting Elements – Parent Elements and Child Elements

Div vs. Span

Global Attribute – Class, ID and Style

Accordion – <Details> and <Summary>

Chapter 8. CSS Basics

What Is CSS?

CSS Syntax

Where To Type CSS?

CSS Basic Selectors

Descendant Selector

Inheritance

Specificity

Reset CSS

Browser Developer Tools for CSS

Comments in CSS Document

Chapter 9. Web Design Basics

Design Element Representation in HTML and CSS

Key Design Points by CSS

Length

Color Code – HEX and RGB

Color Theme

Chapter 10. CSS: Sizing and Spacing

CSS Box Model

width and height

padding

margin

Margin and Padding for Specific Side

margin: auto

box-sizing

Chapter 11. CSS: Styling Text and Images

Text Styling Properties

font-size

color

font-family

Web Font and Google Font

font-weight and font-style

text-decoration

line-height and letter-spacing

text-align

vertical-align

Styling Images

float: left and right

Chapter 12. CSS: Styling Backgrounds

background-color

background-image

background-size

background-repeat

background-position

background-attachment

background (Multiple Properties)

Chapter 13. CSS: Styling Borders and Drawing Lines

border-style

border-color

border-width

border-radius

Border (Multiple Properties)

Borders on Specific Side

Border Radius on Specific Side

Chapter 14. CSS: Layout – Key Concepts and Display Property

Layout Before and After

Layout Key Design Points

Display Property

inline, block and inline-block

display: none

Chapter 15. CSS: Layout – Flex Box

Flex Box – display: flex

flex-direction

Main Axis and Cross Axis

flex-wrap

justify-content

align-items

align-content

align-self

flex-grow

flex-shrink

flex-basis

margin: auto with Flex Box

Inline Flex Box

Nested Flex Box

Chapter 16. CSS: Styling Lists

List Styling Properties

list-style-type

list-style-image

list-style-position

Chapter 17. Creating and Styling Components

Components and Layout

Buttons

Cards

Top Bar

Footer and Bottom Bar

Chapter 18. Completing Website Development

Website Structure Design

Live Server

Home (Landing) Page Development

Main (List) Page Development

Content (Detail) Page Development

Chapter 19. Publishing Websites

Key Steps to Publish Websites

Domain and DNS Server

Favicon

GitHub Pages

Chapter 20. Supplemental Topics

Horizontal Rule – <hr>

Line Break – <br>

Reserved Characters and HTML Entities

Non-breaking Space – &nbsp;

Chapter 1. Overview of Website Development

How Websites Work?

Designing, Building and Publishing Websites

Designing Websites

Building Websites – Frontend and Backend Coding

Web App vs. Website

Frontend Coding

Web Framework and CMS

Publishing Websites (Hosting Services)

Chapter 2. Preparing for Website Coding

Two Key Tools to Start Coding Websites

How Browsers Display Web Pages

File Name Rules

Website Directory Structure

Absolute Path vs. Relative Path

Chapter 3. HTML Basics

What Is HTML?

HTML Element

Attribute

HTML Document Structure

Layout Semantics

Heading and Paragraph Tag

Text-Level Semantics

Space, Line-break and Tag in Content

Comments in HTML Document

Chapter 4. HTML: Add Links and Images

Overview of Adding Links and Images

Embed Images – <img>

Image File Format

Add Hyperlinks – <a>

Add Hyperlinks to Images

Add Hyperlinks to Specific Location on Web Page

Link Tag – <link>

Script Tag – <script>

Chapter 5. HTML: Create Lists and Tables

Create Lists

Create Tables

Combine Table Cells

Chapter 6. HTML: Create Forms

Create Forms

Create Text Input Forms and Submit Button

Radio Button and Checkbox

Select Box

Labels

Auto Complete and Disabled

Chapter 7. Bridging HTML and CSS

Block Element vs. Inline Element

Nesting Elements – Parent Elements and Child Elements

Div vs. Span

Global Attribute – Class, ID and Style

Accordion – <Details> and <Summary>

Chapter 8. CSS Basics

What Is CSS?

CSS Syntax

Where To Type CSS?

CSS Basic Selectors

Descendant Selector

Inheritance

Specificity

Reset CSS

Browser Developer Tools for CSS

Comments in CSS Document

Chapter 9. Web Design Basics

Design Element Representation in HTML and CSS

Key Design Points by CSS

Length

Color Code – HEX and RGB

Color Theme

Chapter 10. CSS: Sizing and Spacing

CSS Box Model

width and height

padding

margin

Margin and Padding for Specific Side

margin: auto

box-sizing

Chapter 11. CSS: Styling Text and Images

Text Styling Properties

font-size

color

font-family

Web Font and Google Font

font-weight and font-style

text-decoration

line-height and letter-spacing

text-align

vertical-align

Styling Images

float: left and right

Chapter 12. CSS: Styling Backgrounds

background-color

background-image

background-size

background-repeat

background-position

background-attachment

background (Multiple Properties)

Chapter 13. CSS: Styling Borders and Drawing Lines

border-style

border-color

border-width

border-radius

Border (Multiple Properties)

Borders on Specific Side

Border Radius on Specific Side

Chapter 14. CSS: Layout – Key Concepts and Display Property

Layout Before and After

Layout Key Design Points

Display Property

inline, block and inline-block

display: none

Chapter 15. CSS: Layout – Flex Box

Flex Box – display: flex

flex-direction

Main Axis and Cross Axis

flex-wrap

justify-content

align-items

align-content

align-self

flex-grow

flex-shrink

flex-basis

margin: auto with Flex Box

Inline Flex Box

Nested Flex Box

Chapter 16. CSS: Styling Lists

List Styling Properties

list-style-type

list-style-image

list-style-position

Chapter 17. Creating and Styling Components

Components and Layout

Buttons

Cards

Top Bar

Footer and Bottom Bar

Chapter 18. Completing Website Development

Website Structure Design

Live Server

Home (Landing) Page Development

Main (List) Page Development

Content (Detail) Page Development

Chapter 19. Publishing Websites

Key Steps to Publish Websites

Domain and DNS Server

Favicon

GitHub Pages

Chapter 20. Supplemental Topics

Horizontal Rule – <hr>

Line Break – <br>

Reserved Characters and HTML Entities

Non-breaking Space – &nbsp;