CSS Syntax and Selectors

A CSS rule consists of a set of style rules that are interpreted by the browser and then applied to the corresponding elements in the document.

The selector points to the HTML element you want to style.The declaration block contains one or more declarations separated by semicolons.

Selectors are used to declare which of the markup elements a style applies to.

Each declaration includes a CSS property name and a value, separated by a colon.A CSS declaration always ends with a semicolon, and declaration blocks are surrounded by curly braces.

Each declaration consists of a property and a value.

In the following example all

elements will be center-aligned, with a red text color:.

Example -

Once you understand the very fundamentals of HTML, we recommend that you learn HTML and CSS at the same time, moving back and forth between the two topics. This is because HTML is far more interesting and much more fun to learn when you apply CSS, and you can't really learn CSS without knowing HTML.

CSS Selectors

A CSS selector is the part of a CSS rule set that actually selects the content you want to style. Let’s look at all the different kinds of selectors available, with a brief description of each.CSS selectors are used to "find" (or select) HTML elements based on their element name, id, class, attribute, and more.

The element Selector

Also referred to simply as a “type selector,” this selector must match one or more HTML elements of the same name. Thus, a selector of nav would match all HTML nav elements, and a selector of <ul> would match all HTML unordered lists, or <ul> elements.

The element selector selects elements based on the element name.You can select all

elements on a page like this (in this case, all

elements will be center-aligned, with a red text color):

The id Selector

An ID selector is declared using a hash, or pound symbol (#) preceding a string of characters. The string of characters is defined by the developer. This selector matches any HTML element that has an ID attribute with the same value as that of the selector, but minus the hash symbol.

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

The class Selector

The class selector is the most useful of all CSS selectors. It’s declared with a dot preceding a string of one or more characters. Just as is the case with an ID selector, this string of characters is defined by the developer. The class selector also matches all elements on the page that have their class attribute set to the same value as the class, minus the dot.

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=".box" will be blue and center-aligned:

Grouping Selectors

If you have elements with the same style definitions, like this:

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

To group selectors, separate each selector with a comma.

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

Grouping Selectors

If you have elements with the same style definitions, like this:

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: