Propriété backgroundde Style

Style 对象参考手册Objet Style

Définition et utilisation

La propriété background définit ou renvoie jusqu'à cinq propriétés d'arrière-plan distinctes sous forme abrégée.

Grâce à cette propriété, vous pouvez définir/renvoyer :

  • background-color
  • background-image
  • background-repeat
  • background-attachment
  • background-position

Les propriétés ci-dessus peuvent également être définies comme des propriétés de style individuelles. Il est fortement recommandé d'utiliser des propriétés séparées pour un meilleur contrôle.

Syntaxe

Définition de la propriété background :

Object.style.background="color image repeat attachment position"

Retour de la propriété background :

Object.style.background

Astuce :La valeur par défaut de la propriété background est : transparent none repeat scroll 0% 0%.

Paramètre Description
color Définit la couleur d'arrière-plan d'un élément
image Définit l'image d'arrière-plan d'un élément
repeat Définit comment l'image d'arrière-plan est répétée
attachment Définit si l'image d'arrière-plan est fixe ou défile avec le reste de la page
position Définit la position de départ de l'image d'arrière-plan


Prise en charge des navigateurs

Internet ExplorerFirefoxOperaGoogle ChromeSafari

Tous les principaux navigateurs prennent en charge la propriété background.


Exemple

Exemple

Définit le style de l'arrière-plan du document :

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Tutoriel d'exemple(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()">Définir l'arrière-plan</button>

</body>
</html>

Essayez-le vous-même »


Style 对象参考手册Objet Style Autres extensions