CSS syntax

Author: MDBootstrap

CSS Syntax

A CSS rule-set consists of a selector and a declaration block:

h3 {color:green;}


h3 = selector

{color:green;} = declaration

color = property

green = value

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

Each declaration includes a CSS property name and a value.

A CSS declaration always ends with a semicolon ( ; ), and declaration blocks are surrounded by curly brackets ( {} ).

Note: You can also write the above example in this way:

                        h3 {
                          color: green;

Live preview

I am a green heading colored via CSS

The latter method is clearer and we will use it the most often. However, do not be surprised if you see a CSS code written in the first method.

CSS Selectors

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

The element Selector

The element selector selects elements based on the element name.

You can select all <h3> elements on a page like this (in this case, all <h3p> elements will be green):

                        h3 {
                            color: green;

                    <h3>I am h3 element colored via CSS</h3>

Live preview

I am h3 element colored via CSS

The id Selector

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

The id of an element should be 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.

The style rule below will be applied to the HTML element with id="test-ID":

                      #test-ID {
                        color: blue;

                    <p id="test-ID">I am paragraph with the ID "test-ID"</p>

Live preview

I am paragraph with the ID "test-ID"

Note: An id name cannot start with a number.

The class Selector

The class selector selects elements with a specific class attribute.

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

In the example below, all HTML elements with class="center" will be center-aligned:

                      .center {
                        text-align: center;

                      <h3 class="center">I am heading with the class "center"</h3>
                      <p class="center">I am paragraph with the class "center" </p>

Live preview

I am heading with the class "center"

I am paragraph with the class "center"

HTML elements can also refer to more than one class.

In the example below, the <p> element will be styled according to class="center" and to class="large":

                          .center {
                            text-align: center;
                          .large {
                            font-size: 30px;

                      <p class="center large">I am paragraph with the class "center" and "large" </p>

Live preview

I am paragraph with the class "center" and "large"

CSS Comments

Comments are used to explain the code, and may help when you edit the source code at a later date.

Comments are ignored by browsers.

A CSS comment starts with /* and ends with */. Comments can also span multiple lines:

                      p {
                        color: red;
                        /* This is a single-line comment */
                        text-align: center;
                      /* This is
                      a multi-line
                      comment */

Previous lesson Next lesson

Spread the word:
Do you need help? Use our support forum

About the author