Style backgroundPropiedad
Objeto Style
Definición y uso
La propiedad background establece o devuelve hasta cinco propiedades de fondo independientes en forma abreviada.
Con esta propiedad, puede establecer/devolver:
- background-color
- background-image
- background-repeat
- background-attachment
- background-position
Las propiedades mencionadas anteriormente también pueden establecerse como propiedades de estilo individuales. Se recomienda encarecidamente usar propiedades individuales para un mejor control.
Sintaxis
Establecer la propiedad background:
Object.style.background="color image
repeat attachment position"
Devolver la propiedad background:
Object.style.background
Consejo:El valor predeterminado de la propiedad background es: transparent none repeat scroll 0% 0%.
| Parámetro | Descripción |
|---|---|
| color | Establece el color de fondo de un elemento. |
| image | Establece la imagen de fondo de un elemento. |
| repeat | Establece cómo se repite la imagen de fondo. |
| attachment | Establece si la imagen de fondo está fija o se desplaza con el resto de la página. |
| position | Establece la posición inicial de la imagen de fondo. |
Soporte del navegador
![]()
![]()
![]()
![]()
![]()
Todos los navegadores principales admiten la propiedad background.
Ejemplo
Ejemplo
Establece el estilo de fondo del documento:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Tutorial de novatos (example.com)</title>
<script>
function displayResult(){
document.body.style.background="#f3f3f3 url('img_tree.png') no-repeat right top";
}
</script>
</head>
<body>
<h1>Hello World!</h1>
<br>
<button type="button" onclick="displayResult()">Establecer fondo</button>
</body>
</html>
<html>
<head>
<meta charset="utf-8">
<title>Tutorial de novatos (example.com)</title>
<script>
function displayResult(){
document.body.style.background="#f3f3f3 url('img_tree.png') no-repeat right top";
}
</script>
</head>
<body>
<h1>Hello World!</h1>
<br>
<button type="button" onclick="displayResult()">Establecer fondo</button>
</body>
</html>
Pruébalo »
Objeto Style Otras extensiones