syntax

CSS Syntax

Understanding CSS Syntax

Introduction to CSS Syntax

CSS (Cascading Style Sheets) syntax consists of rules that define the appearance of elements on a web page. Each rule contains a selector, properties, and values that control the styling of HTML elements.

Basic CSS Syntax Structure

The structure of a CSS rule includes a selector, a declaration block, and one or more declarations:

selector {
  property: value;
  property: value;
}

In this structure:

Example of a CSS Rule Set

h1 {
  color: blue;
  font-size: 24px;
  text-align: center;
}

CSS Selectors

Selectors determine which HTML elements the CSS rules will apply to. Here are some common types of selectors:

CSS Properties and Values

CSS properties control the style of HTML elements, while values define how those properties are applied. Here are some common CSS properties:

Example with Multiple Properties

p {
  color: darkgray;
  font-size: 18px;
  padding: 10px;
  background-color: #f0f0f0;
}

Adding Comments in CSS

Comments in CSS help you explain or organize your code. They start with /* and end with */.

/* This styles the main paragraph */
p {
  color: black;
}

Summary

CSS syntax allows you to target HTML elements and style them with properties and values. Understanding selectors, properties, and values is essential for writing effective CSS code and creating visually appealing web pages.