Introduction

CSS (Cascading Style Sheets) is a stylesheet language that controls how HTML elements are presented. It separates content (HTML) from design (CSS), making websites easier to maintain, more flexible, and visually consistent. Core principles:

  • Cascade → the order of rules matters.
  • Specificity → selectors have different strengths.
  • Inheritance→ some properties (e.g., color, font) are passed from parent to child elements.
  • Basic Syntax and Selectors

    A CSS rule has two parts:

  • Selector → targets one or more elements (p, .class, #id).
  • Declaration block → properties and values inside { }.
  • Example: p { color: blue; font-size: 16px; }

    Common Selectors

  • Universal (*) → applies to all elements.
  • Type (h1, p) → selects by element name.
  • Class (.container)→ reusable styling.
  • ID (#header) → unique styling.
  • Descendant (.nav a) → selects elements inside another.
  • Pseudo-classes → :hover, :focus, :nth-child().
  • Attribute selectors → [type="text"], [href^="https"].
  • Best practice: Use classes instead of IDs for styling, to keep styles flexible.

    The Box Model

    Every element is a box made of:

    1. Content → the text or image.
    2. Padding → space between content and border.
    3. Border → line around padding and content.
    4. Margin → space outside the element.
    The total size of an element = width + padding + border + margin. Tip: box-sizing * { box-sizing: border-box; } This makes the declared width include padding and borders, simplifying layout.

    Flexbox Layout

    Flexbox provides a way to align and distribute space inside a container.

    Main Concepts

  • Container: display: flex;
  • Axes:
    • Main axis → defined by flex-direction: row | column.
    • Cross axis → perpendicular direction.

    Key Properties

  • justify-content → alignment along main axis (flex-start, center, space-between).
  • align-items → alignment along cross axis (stretch, center).
  • flex-wrap → allow items to wrap to multiple lines.
  • flex-grow, flex-shrink, flex-basis → control item sizing.
  • Use cases

  • Navigation bars
  • Centering elements
  • Equal-height columns
  • Grid Layout

    CSS Grid is a two-dimensional system for layouts with rows and columns.

    Main Concepts

  • Container: display: grid;.
  • Define tracks:
    • grid-template-columns: 1fr 1fr 1fr;
    • grid-template-rows: auto 100px;
  • Place items:
    • grid-column: 1 / 3;
    • grid-row: 2 / 3;

    Features

  • grid-gap for spacing.
  • grid-template-areas for semantic layouts.
  • Combines well with media queries for responsive designs.
  • Use cases

  • Page templates
  • Image galleries
  • Dashboards
  • Typography and Text

    CSS controls the look and readability of text.

    Properties

  • Font:
    • font-family: Arial, sans-serif;
    • Import fonts via Google Fonts or @font-face.
  • Size:
    • font-size: 16px; (absolute) or 1.2em, 1rem (relative).
  • Weight & Style:
    • font-weight: bold;, font-style: italic;.
  • Spacing:
    • line-height: 1.5;
    • letter-spacing: 0.1em;
  • Alignment:
    • text-align: center;, justify;.

    Best Practices

  • Use relative units for responsiveness.
  • Ensure high contrast for accessibility.
  • Colors and Visuals

    CSS supports multiple ways to define colors:

  • HEX (#3498db)
  • RGB/RGBA (rgba(52, 152, 219, 0.8))
  • HSL/HSLA (hsl(207, 70%, 54%))
  • Effects

  • Shadows: box-shadow, text-shadow.
  • Gradients: linear-gradient, radial-gradient.
  • Transparency: opacity.
  • Variables

    :root { --main-color: #3498db; } button { background: var(--main-color); }

    Images and Backgrounds
  • Backgrounds:
    • background-image: url("image.jpg");
    • background-size: cover | contain;
    • background-position: center;
  • Images inside content:
    • object-fit: cover; ensures responsive scaling.
  • Optimization: Use modern formats (WebP, AVIF) and compress images.
  • Responsive Design and Media Queries

    Responsive design adapts websites to different screen sizes.

    Media Queries

    @media (max-width: 768px) { body { font-size: 14px; } }

    Breakpoints (common)

  • Small (mobile): up to 480px
  • Medium (tablet): 768px
  • Large (desktop): 1024px+
  • Best practice: Mobile-first → design for small screens, then add larger breakpoints.

    Transitions and Animations

  • Transitions animate changes in property values.
  • Example: button hover.
  • Animations use @keyframes for more complex effects.
  • Properties

  • transition: property duration timing-function;
  • animation-name, animation-duration, animation-iteration-count.
  • Performance tip: Animate transform and opacity for smoother results.

    States and Pseudo Classes

    Pseudo-classes define special states of elements.

  • Interaction:
    • :hover → when mouse hovers
    • :focus → when element is focused (keyboard/tab)
    • :active → when clicked
  • Structural:
    • :first-child, :last-child, :nth-child(2n)
  • Pseudo-elements:
    • ::before and ::after → insert content before/after an element

    Best Practices
  • Use classes for reusable styles.
  • Follow naming conventions (e.g., BEM: block__element--modifier).
  • Group related rules and keep files modular.
  • Minify CSS for production.
  • Compatibility and Tools
  • Use Can I Use to check browser support.
  • Provide fallbacks for older browsers.
  • Use @supports for feature detection.
  • Debug with browser DevTools (Chrome, Firefox, Edge).
  • Useful Resources

    All the documentation in this page is taken from :

  • MDN CSS Reference
  • CSS Tricks