Loading...

Go Back

Next page
Go Back Course Outline

CSS Full Course


CSS Basics and Syntax

CSS Basics and Syntax

CSS Rule Structure

CSS is made up of rules that define how HTML elements should be styled. A CSS rule consists of two main parts: a selector and a declaration block.

Syntax Example:

selector {
  property: value;
}

For example:

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

This will style the h1 tag with a blue color and font size of 24px.



Selectors

Element (Type) Selectors

Element selectors, also called type selectors, are used in CSS to target and style all HTML elements of a specific type.

How They Work:

You simply write the name of the HTML element you want to style — no class (.) or ID (#) needed.

p {
  color: red;
}

This is a paragraph that will turn red due to the element selector.

Class Selectors

Class selectors in CSS are used to target and style one or more HTML elements that share the same class name.

How They Work:

You use a dot (.) followed by the class name to define the style.

.intro {
  font-size: 18px;
  color: green;
}

This is an intro paragraph.

ID Selectors

ID selectors in CSS are used to style a single, unique HTML element. Each ID must be unique within a page.

How They Work:

You use a hash symbol (#) followed by the ID name to define the style.

#main-header {
  background-color: #333;
  color: white;
}

Welcome to my website

Attribute Selectors

Attribute selectors in CSS allow you to target HTML elements based on the presence or value of their attributes.

How They Work:

You use square brackets [] to select elements with specific attributes or attribute values.

a[target="_blank"] {
  color: red;
}

Pseudo-class Selectors

Pseudo-class selectors in CSS are used to define a special state of an element, such as when a user hovers over it, visits a link, or selects a form field.

How They Work:

Pseudo-classes start with a colon (:) followed by the pseudo-class name.

a:hover {
  color: orange;
}


Specificity and Cascade Principles

Specificity is a measure of how specific a CSS rule is. The more specific the selector, the higher priority it gets.

Specificity levels (from low to high):

  • Type selectors (e.g., p, h1) – low specificity
  • Class, attribute, and pseudo-class selectors (e.g., .btn, [type="text"], :hover) – medium specificity
  • ID selectors (e.g., #header) – high specificity
  • Inline styles (e.g., style="color: red;") – highest specificity
h1 {
  color: blue;
}
#header {
  color: red;
}

The text in the element with id="header" will be red due to the higher specificity of the ID selector.

Inheritance in CSS

Inheritance in CSS means that some properties applied to a parent element are automatically passed down to its child elements.

How It Works:

Not all CSS properties are inherited. Mostly, text-related properties like color, font-family, and line-height are inherited by default.

div {
  color: red;
}
p {
  font-size: 16px;
}

This is a paragraph inside a div. The text will be red due to inheritance from the div.

Non-Inherited Properties

In CSS, non-inherited properties are those that do not automatically pass from a parent element to its child elements.

These properties usually affect the layout, box model, positioning, or visual appearance of an element, and must be set individually if needed on each element.

Examples of Non-Inherited Properties:
  • margin
  • padding
  • border
  • width and height
  • display
  • position
  • background
  • box-shadow
  • flex or grid properties
div {
  margin: 20px;
}
p {
  margin: 10px;
}

Overriding Inherited Properties

Sometimes, you may want to override inherited properties in CSS. You can do this by specifying the property again in a child element's style.

p {
  color: green;
}

This is a paragraph where the color is overridden by the child selector, making it green.

Go Back

Next page