Front-end · formation gratuite

CSS3 Training

Master web styles and design

  • Débutant
  • 14 chapitres
  • Exemples de code
  • 100 % gratuit
Commencer le cours

Introduction to CSS3

CSS3 (Cascading Style Sheets) is the language used to style and format web pages. It controls the appearance, layout, and design of your site.

🚀 Why learn CSS3?

  • ✅ Total design control - Customize your site's appearance
  • ✅ Responsive Design - Create sites adapted to all screens
  • ✅ Animations and effects - Bring your pages to life
  • ✅ Optimized performance - Lightweight and fast code
  • ✅ Essential - Core skill for every web developer

📝 CSS Syntax

A CSS rule consists of a selector and a declaration block. Each declaration contains a property and a value.

CSS rule structure

sélecteur {
  propriété: valeur;
  propriété: valeur;
}

Concrete example

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

Three ways to add CSS

  • Inline CSS - In the style attribute of the HTML tag
  • Internal CSS - In the <style> tag in <head>
  • External CSS - In a separate .css file (recommended)

🎯 CSS Selectors

Selectors allow you to target HTML elements you want to style. There are several types of selectors, each with its specificity.

Basic selectors

/* Sélecteur d'élément - Cible tous les éléments du type */
p { color: red; }

/* Sélecteur de classe - Cible les éléments avec class="ma-classe" */
.ma-classe { color: blue; }

/* Sélecteur d'ID - Cible l'élément avec id="mon-id" (unique) */
#mon-id { color: green; }

/* Sélecteur universel - Cible TOUS les éléments */
* { margin: 0; }

/* Sélecteur de groupe - Cible plusieurs éléments */
h1, h2, h3 { font-family: Arial; }

Combinator selectors

/* Descendant - Tous les p dans div */
div p { color: blue; }

/* Enfant direct - p directement dans div */
div > p { color: red; }

/* Adjacent - p immédiatement après div */
div + p { margin-top: 20px; }

/* Frères suivants - Tous les p après div */
div ~ p { color: gray; }

Pseudo-classes

/* États interactifs */
a:hover { color: red; } /* Survol */
a:active { color: green; } /* Clic */
input:focus { border: 2px solid blue; } /* Focus */

/* Sélection structurelle */
li:first-child { font-weight: bold; }
li:last-child { color: red; }
li:nth-child(2n) { background: #f0f0f0; } /* Pairs */

Pseudo-elements

/* Ajouter du contenu avant/après */
p::before {
  content: "→ ";
  color: blue;
}

p::after {
  content: " ✓";
}

/* Première lettre/ligne */
p::first-letter { font-size: 2em; }
p::first-line { font-weight: bold; }

💡 Specificity: ID (100 points) > Class (10 points) > Element (1 point). The higher the specificity, the higher the style priority.

🌈 CSS Colors

CSS supports several formats to define colors. Each format has its advantages depending on the context.

Color formats

/* 1. Noms de couleurs (140 noms prédéfinis) */
.texte1 { color: red; }
.texte2 { color: dodgerblue; }

/* 2. Hexadécimal (#RRGGBB ou #RGB) */
.texte3 { color: #FF0000; } /* Rouge */
.texte4 { color: #F00; } /* Raccourci */

/* 3. RGB (Red, Green, Blue: 0-255) */
.texte5 { color: rgb(255, 0, 0); }

/* 4. RGBA (avec Alpha: 0-1 pour transparence) */
.texte6 { color: rgba(255, 0, 0, 0.5); } /* 50% transparent */

/* 5. HSL (Teinte, Saturation, Luminosité) */
.texte7 { color: hsl(0, 100%, 50%); } /* Rouge */

/* 6. HSLA (HSL avec transparence) */
.texte8 { color: hsla(120, 100%, 50%, 0.3); } /* Vert 30% */

Color properties

  • color - Text color
  • background-color - Background color
  • border-color - Border color
  • outline-color - Outline color
  • opacity - Global transparency (0 to 1)

💡 Tip: Use HSL to easily adjust brightness and saturation. Use RGBA/HSLA for transparent overlays.

🖼️ Backgrounds

CSS offers many properties to customize backgrounds.

.container {
  background-color: #f0f0f0;
  background-image: url('image.jpg');
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
}

CSS Gradients

/* Dégradé linéaire */
.gradient1 {
  background: linear-gradient(to right, #1E90FF, #00CED1);
}

/* Dégradé radial */
.gradient2 {
  background: radial-gradient(circle, #1E90FF, white);
}

🔲 Borders

Borders allow you to frame elements and add visual effects.

Border properties

.box {
  border: 2px solid black;
  border-radius: 10px;
  border-top: 5px dashed red;
  border-left-color: blue;
}

Shadows (Box Shadow)

.card {
  box-shadow: 0 4px 6px rgba(0,0,0,0.1);
}

/* Ombre multiple */
.card-hover {
  box-shadow: 0 10px 20px rgba(0,0,0,0.2),
                0 6px 6px rgba(0,0,0,0.1)
;
}

📏 Margins and Padding

Le Box Model est fondamental en CSS. Il définit comment les éléments occupent l'espace : contenu, padding, bordure et marge.

Comprendre le Box Model

  • Content - Le contenu de l'élément (texte, image)
  • Padding - Espace entre le contenu et la bordure (intérieur)
  • Border - La bordure autour du padding
  • Margin - Espace à l'extérieur de la bordure (sépare des autres éléments)

Margin and padding syntax

.element {
  /* 1 valeur - Tous les côtés */
  margin: 20px;

  /* 2 valeurs - Vertical | Horizontal */
  margin: 10px 20px;

  /* 3 valeurs - Top | Horizontal | Bottom */
  margin: 10px 20px 15px;

  /* 4 valeurs - Top | Right | Bottom | Left (sens horaire) */
  margin: 10px 20px 15px 5px;

  /* Propriétés individuelles */
  margin-top: 10px;
  margin-right: 20px;
  margin-bottom: 15px;
  margin-left: 5px;
}

Useful techniques

/* Centrer horizontalement */
.center {
  width: 800px;
  margin: 0 auto;
}

/* Marges négatives (superposition) */
.overlap {
  margin-top: -20px;
}

/* Box-sizing pour inclure padding dans la largeur */
* {
  box-sizing: border-box;
}

⚠️ Margin Collapse: Vertical margins of two adjacent elements merge. The larger margin wins.

✍️ CSS Text

CSS offers many properties to control the appearance and formatting of text.

Essential text properties

.text {
  /* Couleur du texte */
  color: #333;

  /* Alignement: left, right, center, justify */
  text-align: center;

  /* Décoration: none, underline, overline, line-through */
  text-decoration: underline;

  /* Transformation: uppercase, lowercase, capitalize */
  text-transform: uppercase;

  /* Espacement entre les lettres */
  letter-spacing: 2px;

  /* Espacement entre les mots */
  word-spacing: 5px;

  /* Hauteur de ligne (1.5 = 150% de la taille de police) */
  line-height: 1.6;

  /* Ombre du texte: x y flou couleur */
  text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
}

Text overflow handling

.truncate {
  white-space: nowrap; /* Pas de retour à la ligne */
  overflow: hidden; /* Cache le débordement */
  text-overflow: ellipsis; /* Ajoute ... */
}

.word-break {
  word-wrap: break-word; /* Coupe les mots longs */
  word-break: break-all;
}

🔤 CSS Fonts

Control the appearance of fonts on your site.

Font properties

.titre {
  font-family: 'Arial', sans-serif;
  font-size: 24px;
  font-weight: bold;
  font-style: italic;
}

Google Fonts

/* Dans le HTML */
<link href="https://fonts.googleapis.com/css2?family=Roboto" rel="stylesheet">

/* Dans le CSS */
body {
  font-family: 'Roboto', sans-serif;
}

📦 Flexbox

Flexbox is a powerful layout system for creating flexible and responsive layouts.

Flex Container - Properties

  • flex-direction - Direction des éléments (row, column, row-reverse, column-reverse)
  • justify-content - Alignement sur l'axe principal (flex-start, center, space-between, space-around)
  • align-items - Alignement sur l'axe secondaire (flex-start, center, stretch)
  • flex-wrap - Retour à la ligne (nowrap, wrap, wrap-reverse)
  • gap - Espacement entre les éléments
.container {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  align-items: center;
  gap: 20px;
  flex-wrap: wrap;
}

Flex Items - Properties

  • flex-grow - Facteur d'agrandissement (0 = ne grandit pas, 1+ = grandit)
  • flex-shrink - Facteur de rétrécissement (0 = ne rétrécit pas)
  • flex-basis - Taille de base avant distribution de l'espace
  • flex - Raccourci pour grow shrink basis (ex: flex: 1 0 200px)
  • align-self - Alignement individuel (override align-items)
.item {
  flex: 1; /* Équivalent à 1 1 0 */
}

.item-fixed {
  flex: 0 0 200px; /* Largeur fixe */
}

🎯 CSS Grid

CSS Grid est un système de mise en page bidimensionnel puissant pour créer des grilles complexes avec un contrôle précis sur les lignes et colonnes.

Propriétés du conteneur Grid

  • display: grid - Active le mode Grid
  • grid-template-columns - Définit les colonnes (px, %, fr, auto, repeat())
  • grid-template-rows - Définit les lignes
  • gap - Espacement entre cellules (row-gap, column-gap)
  • grid-auto-flow - Direction de placement (row, column, dense)
  • justify-items - Alignement horizontal des items (start, center, end, stretch)
  • align-items - Alignement vertical des items

Define a grid

.grid-container {
  display: grid;

  /* 3 colonnes de taille égale (fr = fraction) */
  grid-template-columns: 1fr 1fr 1fr;
  /* Raccourci avec repeat() */
  grid-template-columns: repeat(3, 1fr);

  /* Colonnes de tailles différentes */
  grid-template-columns: 200px 1fr 2fr;

  /* Lignes automatiques */
  grid-template-rows: auto;

  /* Espacement */
  gap: 20px; /* ou row-gap: 20px; column-gap: 10px; */
}

Placement des éléments

  • grid-column - Position sur les colonnes (start / end ou span nombre)
  • grid-row - Position sur les lignes
  • grid-area - Raccourci pour row-start / column-start / row-end / column-end
  • justify-self - Alignement horizontal individuel
  • align-self - Alignement vertical individuel
/* Placement avec lignes de début/fin */
.item1 {
  grid-column: 1 / 3; /* De la ligne 1 à 3 (2 colonnes) */
  grid-row: 1 / 2;
}

/* Placement avec span */
.item2 {
  grid-column: span 2; /* Occupe 2 colonnes */
  grid-row: span 2; /* Occupe 2 lignes */
}

/* Raccourci grid-area */
.item3 {
  grid-area: 1 / 1 / 3 / 3; /* row-start/col-start/row-end/col-end */
}

Grid with named areas

.container {
  display: grid;
  grid-template-areas:
    "header header header"
    "sidebar main main"
    "footer footer footer";
}

.header { grid-area: header; }
.sidebar { grid-area: sidebar; }
.main { grid-area: main; }

💡 fr vs % : L'unité fr distribue l'espace disponible APRÈS avoir soustrait les tailles fixes. Plus flexible que les pourcentages !

✨ CSS Transitions

Transitions allow you to smoothly animate CSS property changes when a state changes (hover, focus, etc.).

Transition properties

  • transition-property - Propriété(s) à animer (all, background, transform, etc.)
  • transition-duration - Durée de la transition (en s ou ms)
  • transition-timing-function - Courbe d'accélération (ease, linear, ease-in, ease-out, ease-in-out, cubic-bezier)
  • transition-delay - Délai avant le début (en s ou ms)
  • transition - Raccourci : property duration timing-function delay

Shorthand syntax

.button {
  background-color: #1E90FF;
  transform: scale(1);

  /* Transition sur toutes les propriétés */
  transition: all 0.3s ease;
}

.button:hover {
  background-color: #1873CC;
  transform: scale(1.05);
}

Multiple transitions

.card {
  /* Propriétés individuelles */
  transition-property: background-color, transform, box-shadow;
  transition-duration: 0.3s, 0.5s, 0.3s;
  transition-timing-function: ease, ease-out, ease;

  /* Ou en raccourci */
  transition: background-color 0.3s ease,
              transform 0.5s ease-out,
              box-shadow 0.3s ease
;
}

Timing functions

  • linear - Constant speed
  • ease - Slow at start and end (default)
  • ease-in - Slow at start, fast at end
  • ease-out - Fast at start, slow at end
  • ease-in-out - Slow at start and end
  • cubic-bezier(n,n,n,n) - Custom curve

⚡ Performance: Prefer transitions on transform and opacity for better performance (GPU acceleration).

🎬 CSS Animations

CSS animations allow you to create complex and automatic movements with @keyframes, without JavaScript.

Animation properties

  • animation-name - Nom de l'animation @keyframes
  • animation-duration - Durée (en s ou ms)
  • animation-timing-function - Courbe d'accélération (ease, linear, etc.)
  • animation-delay - Délai avant le début
  • animation-iteration-count - Nombre de répétitions (nombre ou infinite)
  • animation-direction - Direction (normal, reverse, alternate, alternate-reverse)
  • animation-fill-mode - État avant/après (none, forwards, backwards, both)
  • animation-play-state - État de lecture (running, paused)

Define an animation with @keyframes

/* Avec pourcentages */
@keyframes slideIn {
  0% {
    transform: translateX(-100%);
    opacity: 0;
  }
  50% {
    transform: translateX(-50%);
    opacity: 0.5;
  }
  100% {
    transform: translateX(0);
    opacity: 1;
  }
}

/* Avec from/to */
@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

Apply the animation

/* Syntaxe complète */
.box {
  animation-name: slideIn;
  animation-duration: 1s;
  animation-timing-function: ease-out;
  animation-delay: 0.5s;
  animation-iteration-count: 3; /* ou infinite */
  animation-direction: alternate;
  animation-fill-mode: forwards; /* Garde l'état final */
}

/* Raccourci */
.box {
  animation: slideIn 1s ease-out 0.5s 3 alternate forwards;
}

Multiple animations

.element {
  animation: slideIn 1s ease-out,
              fadeIn 0.5s ease,
              rotate 2s linear infinite
;
}

💡 fill-mode: forwards keeps the final state, backwards applies the initial state during delay, both combines both.

📱 Responsive Design

Media Queries allow you to adapt your site to different screen sizes, orientations, and device types.

Media Query syntax

  • @media (condition) - Applies styles based on a condition
  • min-width - Minimum width (Mobile First)
  • max-width - Maximum width (Desktop First)
  • orientation - Portrait or landscape
  • and - Combines multiple conditions

Common breakpoints

/* Mobile (0-768px) */
@media (max-width: 768px) {
  .container {
    flex-direction: column;
  }
}

/* Tablette (769px-1024px) */
@media (min-width: 769px) and (max-width: 1024px) {
  .container {
    grid-template-columns: repeat(2, 1fr);
  }
}

/* Desktop (1025px+) */
@media (min-width: 1025px) {
  .container {
    grid-template-columns: repeat(3, 1fr);
  }
}

Responsive units

  • vw - 1% of viewport width
  • vh - 1% of viewport height
  • vmin - 1% of smallest dimension
  • vmax - 1% of largest dimension
  • % - Percentage of parent
  • em - Relative to parent font size
  • rem - Relative to root font size (html)
  • clamp(min, préféré, max) - Fluid value with limits
.responsive {
  /* Taille fluide avec limites */
  font-size: clamp(16px, 2vw, 24px);
  width: 90vw;
  max-width: 1200px;
}

💡 Mobile First: Start with mobile design, then add Media Queries with min-width for larger screens. More maintainable!

🎓 Next Steps

Congratulations! You now have a solid foundation in CSS3.

✅ What you learned:

  • CSS syntax and selectors
  • CSS colors and formats
  • Backgrounds and gradients
  • Borders and shadows
  • Box Model (margins and padding)
  • Text formatting
  • Fonts and Google Fonts
  • Flexbox for flexible layouts
  • CSS Grid for complex grids
  • CSS transitions
  • Animations with @keyframes
  • Responsive Design with Media Queries
Cliquer pour discuter avec NiangProgrammeur
NiangProgrammeur
En ligne

Bonjour ! 👋

Comment puis-je vous aider aujourd'hui ?