Saltar al contenido

Propiedad CSS background-image

La propiedad background-image especifica las imágenes de fondo para los elementos. Puedes especificar una o más imágenes.

Por defecto, una imagen de fondo se repite vertical y horizontalmente y se coloca en la esquina superior izquierda del elemento.

El fondo de un elemento es el tamaño total del mismo, incluyendo padding y border (no margin).

Si la imagen no está disponible, necesitas especificar un background-color.

Valor inicialnone
Se aplica aTodos los elementos. También se aplica a ::first-letter y ::first-line.
HeredableNo.
AnimableNo.
VersiónCSS1 + algunos valores nuevos en CSS3
Sintaxis DOMobject.style.backgroundImage = "url(img_tree.png)";

Sintaxis

Sintaxis de la propiedad CSS background-image

css
background-image: url | none | linear-gradient | radial-gradient | repeating-linear-gradient | repeating-radial-gradient | initial | inherit;

Ejemplo de la propiedad background-image:

Ejemplo de la propiedad CSS background-image

html
<!DOCTYPE html>
<html>
  <head>
    <title>The title of the document</title>
    <style>
      body {
        background-image: url("https://es.w3docs.com/uploads/media/default/0001/02/55a2f152f59bf42a99b576d44a4578ec9daa0ab6.png");
        background-color: #8ebf42;
      }
    </style>
  </head>
  <body>
    <h2>Background-image property example</h2>
    <p>Hello World!</p>
  </body>
</html>

Resultado

Propiedad CSS background-image

Consulta otro ejemplo donde se utilizan dos imágenes y se especifican mediante la propiedad background-position.

Ejemplo de la propiedad background-image con otras propiedades de fondo:

Ejemplo de la propiedad CSS background-image con 2 imágenes

html
<!DOCTYPE html>
<html>
  <head>
    <title>The title of the document</title>
    <style>
      body {
        padding: 100px;
        background-image: url("https://es.w3docs.com/uploads/media/default/0001/02/55a2f152f59bf42a99b576d44a4578ec9daa0ab6.png"), url("https://es.w3docs.com/uploads/media/default/0001/01/b408569013c0bb32b2afb0f0d45e93e982347951.jpeg");
        background-attachment: fixed;
        background-position: 5px 50px;
        background-repeat: no-repeat, repeat;
      }
    </style>
  </head>
  <body>
    <h2>Background-image property example</h2>
    <p>The background image is positioned 5px from the left, and 50px down from the top.</p>
  </body>
</html>

En este ejemplo, se especifica un "linear-gradient" con dos colores como imagen de fondo para un elemento <div>:

Ejemplo de la propiedad background-image con el valor "linear-gradient":

Ejemplo de la propiedad CSS background-image con el valor linear-gradient

html
<!DOCTYPE html>
<html>
  <head>
    <style>
      div {
        height: 300px;
        background-image: linear-gradient(#eee, #1c87c9);
      }
    </style>
  </head>
  <body>
    <h2>Linear gradient as a background image example</h2>
    <p>This linear gradient starts at the top. It starts gray, transitioning to blue:</p>
    <div></div>
  </body>
</html>

Ejemplo de la propiedad background-image con el valor "repeating-radial-gradient":

Ejemplo de la propiedad CSS background-image con el valor repeating-radial-gradient

html
<!DOCTYPE html>
<html>
  <head>
    <title>The title of the document</title>
    <style>
      div {
        height: 300px;
        background-color: #cccccc;
        background-image: repeating-radial-gradient(#8ebf42, #eee 15%, #ccc 30%);
      }
    </style>
  </head>
  <body>
    <h2>Radial gradient as a background image example</h2>
    <div></div>
  </body>
</html>

Valores

ValorDescripción
urlDefine la url de la imagen. Se pueden especificar más de una imagen separadas por comas.
noneNo habrá ninguna imagen de fondo. Es el valor predeterminado.
linear-gradientSe especifica un degradado lineal como imagen de fondo.
radial-gradientSe especifica un degradado radial como imagen de fondo.
repeating-linear-gradientRepite un degradado lineal.
repeating-radial-gradientRepite un degradado radial.
initialEstablece la propiedad en su valor predeterminado.
inheritHereda la propiedad de su elemento padre.

Práctica

¿Cuáles son las propiedades de CSS que se pueden usar para controlar la imagen de fondo de un elemento?

¿Te resulta útil?

Vista previa dual-run — compárala con las rutas Symfony en producción.