Introduction

CSS (Cascading Style Sheets) is a style sheet language used to describe the presentation and formatting of HTML documents.

CSS is used to style and layout web pages by altering the font, color, size, and spacing of your content, split it into multiple columns, or add animations and other decorative features.

By keeping the styling separate from the HTML content, it becomes easier to update the appearance of multiple pages without modifying the underlying structure.

Prerequisites

This guide assumes you have the following background:

CSS Syntax

In CSS, selectors are used to target HTML elements that require styling. Common selectors include element selectors, class selectors, ID selectors, and attribute selectors.

          
/* Element Selector */

p {
  color: blue;
}
          
          
          
/* Class Selector */

.my-class {
  font-size: 16px;
}
          
          
          
/* ID Selector */

#header {
  background-color: #f2f2f2;
}
          
          
          
/* Attribute Selector */

input[type="text"] {
  border: 1px solid #ccc;
}
          
          

CSS properties define the various styling options, and values determine the appearance of the selected elements. Properties and values are written in pairs, separated by a colon.

          
/* Property: Value */

body {
  font-family: "Arial", sans-serif;
  background-color: #f9f9f9;
}
          
          

Box Model

Box Model

Layout and Positioning

Positioning allows you to take elements out of normal document flow and make them behave differently, for example, by sitting on top of one another or by always remaining in the same place inside the browser viewport.

The display property determines how an element should be rendered in the document flow.

          
.element {
  display: block; /* or inline, inline-block, flex, grid, etc. */
}
          
          

The position property controls the positioning of an element relative to its containing element. The top, right, bottom, and left properties determine the final location of positioned elements.

          
.element {
  position: relative; /* or absolute, fixed, sticky, static */
  top: 20px;
  left: 10px;
}
          
          

Flexbox is a layout model that provides an efficient way to align and distribute space among items in a container.

          
.container {
  display: flex;
  justify-content: center;
  align-items: center;
}
          
          

Responsive Web Design

Media queries enable developers to apply different styles based on the device's screen size and resolution.

          
@media (max-width: 768px) {
  /* Styles for smaller screens */
}
          
          

The viewport meta tag ensures that a web page scales properly on different devices.

          
meta name="viewport" content="width=device-width, initial-scale=1.0"
          
          

Reference