CSS INTRODUCTION

css3

Before talking about the post “Simple Selectors in CSS” let’s give the definition of CSS. CSS stands for Cascading Style Sheets, with CSS we describe how we want the elements of our HTML pages to appear, we customize for example fonts, colors, backgrounds etc. Created by the World Wide Web Consortium (W3C) as a “complementary” to HTML.

THE RULE OF CSS

For each selector we have a key-value pair called a declaration. We will talk about selectors in detail later.

CSS selector

HOW TO ADD CSS TO OUR HTML PAGES

There are three ways to insert a style sheet into an HTML page.

EXTERNAL CSS

With an external style sheet, you can change the look of an entire website by changing just one file. Each HTML page must include a reference to the style sheet with the <link> tag inserted within the head section.

External CSS

INTERNAL CSS

An internal style sheet can be used if a single HTML page has a unique style. The internal style is defined inside the <style> element, of the head section.

Internal CSS

INLINE CSS 

An inline style can be used to apply a unique style to a single element. To use inline styles, add the style attribute to the relevant element. This attribute can contain any CSS property.

inline CSS

MULTIPLE STYLES

Continuing with the topic “Simple Selectors in CSS” the question arises: what style will be used when more than one style is specified for an HTML element? All styles on a page will be applied “cascading” into a new “virtual” style sheet according to the following rules, where number one has the highest priority:

  1. Inline style (within an HTML element)
  2. External and internal style sheets (in the head section)
  3. Default browser settings

Hence, an inline style has the highest priority and will override the external and internal styles and browser defaults. Let’s check this with the Chrome inspector. In the example code I have defined a tag having the color property overwritten several times. We verify that the inline style prevails.

CHROME INSPECTOR

Inspector

METHOD

  • Open the sample HTML page
  • Right click in any area and select “inspect
  • Select the Elements tab
  • Inside the inspector select the square with the arrow at the top left
  • Select the <h1> SIMPLE SELECTOR element
  • You will see in the CSS sheet that the overridden settings are highlighted as crossed out and the color of the inline style prevails.

CSS COMMENTS

Comments are used to explain the code and can be helpful when editing the source code later. Comments are ignored by browsers. A CSS comment begins with /* and ends with */.

CSS SELECTORS

A CSS selector selects the HTML elements you want to style. We can divide CSS selectors into five categories:

  • Simple selectors (select items 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 model a part of an element)
  • Attribute selectors (select elements based on an attribute or attribute value)

In this first article we will look at Simple selectors.

CSS ELEMENT SELECTOR

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

CSS ID SELECTOR

The id selector uses the id attribute of an HTML element to select a specific element. The id of an element must be unique within a page; then, the id selector is used to select a unique item! To select an element with a specific id, write a hash (#) character, followed by the element’s id.

CSS CLASS SELECTOR

The class selector selects HTML elements based on a specific class attribute. To select elements with a class attribute, write a period (.) Character, followed by the name of the class.

CSS UNIVERSAL SELECTOR

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

CSS GROUPING SELECTOR

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

NB: FOR ALL THE VARIOUS SELECTORS I HAVE GIVEN AN EXAMPLE CODE.

Copy to Clipboard
Copy to Clipboard

LINKS TO PREVIOUS POST

PREVIOUS POST LINKS

LINK TO THE CODE ON GITHUB

GITHUB