Style backgroundImagePropiedad
Objeto Style
Definición y uso
La propiedad backgroundImage establece o devuelve la imagen de fondo de un elemento.
Sintaxis
Establecer la propiedad backgroundImage:
Object.style.backgroundImage="url('URL')|none|inherit"
Devolver la propiedad backgroundImage:
Object.style.backgroundImage
| Valor | Descripción |
|---|---|
| url('URL') | La ubicación del archivo de imagen. |
| none | Por defecto. Sin imagen de fondo. |
| inherit | La configuración de la propiedad background-image se hereda del elemento padre. |
Soporte del navegador
![]()
![]()
![]()
![]()
![]()
Todos los navegadores principales soportan la propiedad backgroundImage.
Nota:IE7 y versiones anteriores no admiten el valor "inherit". IE8 solo admite "inherit" si se especifica !DOCTYPE. IE9 admite "inherit".
Consejos y notas
Consejo:Además de la imagen de fondo, también debe especificar un color de fondo. Si la imagen no está disponible, se utilizará el color de fondo.
Ejemplo
Ejemplo
Establecer imagen de fondo:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Tutorial para novatos (example.com)</title>
<script>
function displayResult(){
document.body.style.backgroundColor="#f3f3f3";
document.body.style.backgroundImage="url('img_tree.png')";
}
</script>
</head>
<body>
<h1>Hello World!</h1>
<br>
<button type="button" onclick="displayResult()">Establecer imagen de fondo</button>
</body>
</html>
<html>
<head>
<meta charset="utf-8">
<title>Tutorial para novatos (example.com)</title>
<script>
function displayResult(){
document.body.style.backgroundColor="#f3f3f3";
document.body.style.backgroundImage="url('img_tree.png')";
}
</script>
</head>
<body>
<h1>Hello World!</h1>
<br>
<button type="button" onclick="displayResult()">Establecer imagen de fondo</button>
</body>
</html>
Pruébalo »
Objeto Style Otras extensiones