Mastering CSS Syntax: A Beginner's Guide to Crafting Stylish Websites- w9school

Unlock the world of CSS syntax and create stunning websites with our beginner-friendly guide. Start your styling journey today!

Mastering CSS Syntax: A Beginner's Guide to Crafting Stylish Websites- w9school

CSS Syntax

CSS (Cascading Style Sheets) syntax defines how styles are written in CSS to control the presentation and layout of HTML or XML documents. CSS syntax consists of rulesets, declarations, selectors, properties, and values.CSS syntax is a set of rules that you have to follow whenever you are writing code in CSS. If you don't follow the syntax then the system will not run and apply the style on your webpage

Here's an overview of each component of CSS syntax:

1- Ruleset:

A ruleset is the fundamental building block of CSS and consists of a selector followed by a declaration block. It specifies which HTML elements the styles will apply to.

selector {
  /* Declaration block goes here */
}

2- Selector:

The selector targets HTML elements that you want to style. There are various types of selectors in CSS, including:

  • Element Selector: Selects HTML elements by their tag name.
p {
  /* Styles for paragraphs */
}
  • Class Selector: Selects elements with a specific class attribute.
.highlight {
  /* Styles for elements with class="highlight" */
}
  • ID Selector: Selects an element with a particular attribute of ID.
#header { /* Styles for the element with id="header" */ }
  • Descendant Selector: Selects an element that is a descendant of another element.
nav ul li { /* Styles for li elements inside ul elements inside a nav element */ }

CSS selectors will be further explained later in this article.

3- Declaration Block:

The declaration block is a set of property-value pairs enclosed in curly braces {}. Each property defines a specific style attribute, and its value specifies how that attribute should be applied.

selector {
 property1: value1; 
property2: value2;
 /* ... */
 }

4- Property:

The property represents a style attribute that you want to apply to the selected HTML elements. There are numerous CSS properties available, such as color, font-size, margin, padding, background, etc.

p {
  color: blue;
  font-size: 16px;
}

5- Value:

The value is the setting you assign to a property. It defines how the selected elements should appear according to the specified property.

h1 {
  font-size: 24px;
}

6- Comments:

You can add comments in CSS using /* */. Comments are ignored by the browser and are useful for documenting your code.

/* This is a comment in CSS */
p {
  /* This is another comment */
  color: red;
}

CSS syntax allows you to define styles for various HTML elements, classes, and IDs, enabling you to create visually appealing and consistent layouts for web pages.

These are the basic components of CSS syntax. By combining selectors, properties, and values, you can create various styles to control the appearance of your HTML elements on the web page.

CSS Syntax Overview 

  • The selector points to the HTML element you want to style.

  • The declaration block contains one or more declarations separated by semicolons.

  • Each declaration includes a CSS property name and a value, separated by a colon.

  • Multiple CSS declarations are separated with semicolons, and declaration blocks are surrounded by curly braces.

Example

In this example all elements will be center-aligned, with a red text color:

p {
  color: red;
  text-align: center;
}‚Äč

Example Explained:

  • p is a selector in CSS (it points to the HTML element you want to style:).

  • color is a property, and red is the property value

  • text-align is a property, and center is the property value

CSS Selectors

A CSS selector selects the HTML element(s) you want to style.

CSS Selectors

CSS selectors are used to "find" (or select) the HTML elements you want to style.We can divide CSS selectors into five categories:

  • Simple selectors (select elements based on name, id, class)

  • Combinator selectors (select elements based on a specific relationship between them)

  • Pseudo-class selectors (select elements based on a certain state)

  • Pseudo-elements selectors (select and style a part of an element)

  • Attribute selectors (select elements based on an attribute or attribute value)

This page will explain the most basic CSS selectors.

The CSS element Selector

The element selector selects HTML elements based on the element name.

Example

Here, all elements on the page will be center-aligned, with a red text color: 

p {
  text-align: center;
  color: red;
}

The CSS id Selector

The id selector uses the id attribute of an HTML element to select a specific element.

The id of an element is unique within a page, so the id selector is used to select one unique element!

To select an element with a specific id, write a hash (#) character, followed by the id of the element.

Example

The CSS rule below will be applied to the HTML element with id="para1": 

#para1 {
  text-align: center;
  color: red;
}
Note: An id name cannot start with a number!
The CSS class Selector

The class selector selects HTML elements with a specific class attribute.

To select elements with a specific class, write a period (.) character, followed by the class name.

Example

In this example all HTML elements with class="center" will be red and center-aligned: 

.center {
  text-align: center;
  color: red;
}
You can also specify that only specific HTML elements should be affected by a class.

Example

In this example only elements with class="center" will be red and center-aligned: 

p.center {
  text-align: center;
  color: red;
}

HTML elements can also refer to more than one class.

Example

In this example theelement will be styled according to class="center" and to class="large":

/*<p class="center large">This paragraph refers to two classes.</p>*/
Note: A class name cannot start with a number!

The CSS Universal Selector

The universal selector (*) selects all HTML elements on the page.

Example

The CSS rule below will affect every HTML element on the page: 

* {
  text-align: center;
  color: blue;
}

The CSS Grouping Selector

The grouping selector selects all the HTML elements with the same style definitions.

Look at the following CSS code (the h1, h2, and p elements have the same style definitions):

h1 {
  text-align: center;
  color: red;
}

h2 {
  text-align: center;
  color: red;
}

p {
  text-align: center;
  color: red;
}

It will be better to group the selectors, to minimize the code.

To group selectors, separate each selector with a comma.

Example

In this example we have grouped the selectors from the code above: 

h1, h2, p {
  text-align: center;
  color: red;
}

All CSS Simple Selectors

Selector Example Example description

#id

#firstname

Selects the element with id="firstname"

.class

.intro

Selects all elements with class="intro"

element.class

p.intro

Selects only

elements with class="intro"

*

*

Selects all elements

element

p

Selects all

elements

element,element,..

div, p

Selects all

elements and all

elements

Test Yourself with CSS Exercises

Are You Ready?

If Yes! then Click Here

What's Your Reaction?

like

dislike

love

funny

angry

sad

wow