CSS BACKGROUND /// IMAGES /// GRADIENTS /// BACKGROUND-SIZE /// COVER /// CSS BACKGROUND /// IMAGES /// GRADIENTS /// BACKGROUND-SIZE /// COVER ///

Background Images

Give depth to your layouts. Master the art of layering images, configuring patterns, and painting dynamic gradients directly with CSS.

backgrounds.css
1 / 15
12345
🖼️

Tutor:Backgrounds are more than just solid colors. With CSS, you can layer images, patterns, and gradients to create depth.


Skill Matrix

UNLOCK NODES BY MASTERING BACKGROUNDS.

Concept: URL Linking

Images are loaded via the `url()` functional notation in the `background-image` property.

System Check

What happens by default if a background image is smaller than its container element?


Community Holo-Net

Showcase Your Designs

ACTIVE

Created a wild gradient or complex layered background? Share your codepens and get feedback!

CSS Background Image: Layering Pixels

Frontend Instructor en Code Syllabus

Pascual Vila

Frontend Instructor // Code Syllabus

A website without rich backgrounds is like a canvas without paint. Mastering the CSS `background-image` property (and its companions) is fundamental for creating immersive, modern web designs. Let's dig deep.

The Basics: The url() Function

To load an external image file, you pass a relative or absolute path into the url() function.

background-image: url('../images/hero.jpg');

Always ensure your image paths are correct relative to where your CSS file is located, not the HTML file (unless the styles are inline).

Controlling the Flow: Repeat, Size & Position

By default, the browser will tile an image if it's smaller than its container. We manipulate this using:

  • background-repeat: Set to no-repeat to stop tiling. You can also use repeat-x (horizontal only) or repeat-y.
  • background-size: The powerhouse property. cover ensures the whole div is filled (cropping if needed), while contain ensures the whole image is visible (leaving blank space if needed).
  • background-position: Aligns the image. center center perfectly centers it, but you can also use pixels or percentages (e.g., top left or 50% 100%).

Gradients: Painting with Math

CSS Gradients are technically treated as background-image. Because the browser renders them programmatically, they are incredibly performant and sharp at any resolution.

linear-gradient(to bottom right, #ff0099, #00f0ff);

View Accessibility & Performance Tips+

Fallback Colors: Always provide a background-color fallback. If the user's connection is slow, or the image link breaks, the solid color will render first so your text remains readable.

Image Optimization: Don't load a 5MB background image. Compress your images and serve modern formats like WebP or AVIF whenever possible to ensure fast loading times.

Frequently Asked Questions

¿Cuál es la diferencia entre background-size cover y contain?

cover: Escala la imagen para cubrir completamente el contenedor, recortándola si es necesario. Perfecto para imágenes hero o fondos que deben llenar todo el espacio sin dejar espacios vacíos.

contain: Escala la imagen manteniéndola completamente visible dentro del contenedor. Puede dejar espacios vacíos (generalmente rellenados con el background-color). Ideal cuando necesitas ver toda la imagen sin recortes.

/* Cover: rellena todo, puede recortar */ background-size: cover; /* Contain: muestra toda la imagen, puede dejar espacios */ background-size: contain;
¿Cuándo debo usar cover y cuándo contain?

Usa cover cuando:

  • Necesitas una imagen hero o banner de fondo
  • El recorte de la imagen no afecta el contenido importante
  • Quieres un diseño moderno y limpio sin espacios vacíos

Usa contain cuando:

  • Es crítico que se vea toda la imagen sin recortes
  • La imagen contiene elementos importantes en los bordes
  • Trabajas con iconos o logos como fondos
¿Cómo centro correctamente una imagen de fondo?

Combina `background-size`, `background-position` y `background-repeat`:

.hero {background-image: url('image.jpg'); background-size: cover; background-position: center; background-repeat: no-repeat;}

Background Glossary

background-image
Sets one or more background images on an element. Can use url() for files or gradient functions.
snippet.css
background-size
Specifies the size of the background images. Keywords like 'cover' or 'contain' are incredibly useful.
snippet.css
background-position
Sets the initial position for each background image relative to the background position layer.
snippet.css
background-repeat
Sets if/how a background image will be repeated (tiled).
snippet.css
linear-gradient()
A CSS function that creates an image consisting of a progressive transition between two or more colors along a straight line.
snippet.css
background (shorthand)
Sets all background style properties at once, such as color, image, origin and size, or repeat method.
snippet.css