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 Syntax

CSS Syntax

Understanding CSS Syntax: Selectors and Declarations

CSS's syntax is simple. It defines "where to change (selector)" and "how to change (declaration)".

Selector

The selector defines which part of HTML elements will be styled. There are several types of CSS selectors. The HTML tag is the most simple approach to styling elements. You can utilize class or id as a CSS selector. There are several other rules about the CSS selector. We'll explain the basic ones in this chapter.

Declaration

The declaration defines how the selected HTML elements will be styled. The declaration consists of property and property value.

Property

There are many properties. You don't need to memorize all of them; however, it is good to memorize key ones. Here are some select examples:

  • Sizing and spacing related: width, height, margin, padding
  • Text related: color, font-size, font-family, line-height
  • Background related: background-color, background-image, background-position
  • Border related: border-style, border-color, border-width
  • Layout related: display, position
  • List design related: list-style-type, list-style-position, list-style-image

Property Value

What type of property value is applied depends on each property. In some cases, there are different formats of property value. For example, size (length) and color have different formats like the ones below.

  • Size (length): px (pixel), %, em, rem (root em)
  • Color: HEX, RGB, RGBA, color name

There are pros and cons for each format. You can choose one of them depending on your coding needs. We'll explain these points later.

Multiple Declaration

You can set more than one declaration for one selector. To add another declaration, you can use a semi-colon.

Example:

h1{
  color: blue;
  font-size: 10px;
}

Practice

Objective:
Understand how CSS works

This is a very simple practice – changing font color and background color of H1 Text.

1. Create a new HTML file for this chapter

  • Create a new file chapter8.html under the html-css-introduction directory.
  • Type ! and hit tab or enter to create an html template.
  • Change the <title> section to 8. CSS Basics.

2. Type HTML code

Type the following HTML code in the body element.

chapter8.html
<body>
  <h1>Chapter 8. CSS Basics</h1>
</body>

After editing the file, make sure that you save it (⌘ + S for Mac, Ctrl + S for Windows).

3. Open the HTML file with a browser

Open chapter8.html with a browser. You'll see plain text like shown below.

HTML element without CSS

4. Add CSS code

There are different locations where you can write CSS code. We'll explain different locations to write CSS in the next section. In this practice, use the <head> section of the HTML document. Write CSS code between <style> and </style>.

chapter8.html
    :
  <title>8. CSS Basics</title>
  <style>
  h1{
    color:blue;
    text-align: center;
    background-color: lightblue;
    border-color: blue;
    border-style: solid;
    border-width: 5px;
    border-radius: 5px;
    margin:20px;
    padding:20px;
    width: 200px;
  }
  </style>
</head>
<body>
  <h1>Chapter 8. CSS Basics</h1>
</body>

5. Check the result with a browser

Open chapter8.html with a browser.
You can see that the text is styled as shown below.

HTML element with CSS

IdeaTips: Use semi-colon and line break effectively

As web browsers skip line breaks and spaces in CSS code, you can format CSS code in a structured way.

For example, the following two sets of CSS code return the same results; however, case 2 is easier to read and you can minimize coding errors.

Case 1
h1{color:white;background-color:green;}
Case 2 (Recommended)
h1 {
  color: white;
  background-color:green;
}

The last ; (semi-colon) after the last declaration is not necessary, but it is better to keep it as you may add another declaration later.


You can also learn this topic offline. Click AmazonKindle.

More Topics to Explore

Managing Background Image Repetition in CSS

background-repeat

Creating Rounded Corners with border-radius

border-radius

Responsive Design Example: CSS Display Grid

Responsive Design Example: CSS Display Grid

Designing Structured Layouts in CSS

Layout Before and After

Managing Background Image Repetition in CSS

background-repeat

Creating Rounded Corners with border-radius

border-radius

Responsive Design Example: CSS Display Grid

Responsive Design Example: CSS Display Grid

Designing Structured Layouts in CSS

Layout Before and After

Tags:

Selector

Property Value

Declaration

Property

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;