Propriété CSS background-image
Exemple
Définissez une image d'arrière-plan pour l'élément <body> :
body {
background-image: url("paper.gif");
background-color: #cccccc;
}
Exemple
Définissez deux images d'arrière-plan pour l'élément <body> :
body {
background-image: url("img_tree.gif"), url("paper.gif");
background-color: #cccccc;
}
Plus d'exemples "Essayez-le vous-même" ci-dessous.
Définition et utilisation
La background-image
propriété définit une ou plusieurs images d'arrière-plan pour un élément.
Par défaut, une image d'arrière-plan est placée dans le coin supérieur gauche d'un élément et répétée à la fois verticalement et horizontalement.
Conseil : L'arrière-plan d'un élément correspond à la taille totale de l'élément, y compris le rembourrage et la bordure (mais pas la marge).
Conseil : Définissez toujours une couleur d'arrière-plan à utiliser si l'image n'est pas disponible.
Valeur par défaut: | rien |
---|---|
Hérité: | non |
Animable : | non. En savoir plus sur l'animation |
Version: | CSS1 + nouvelles valeurs dans CSS3 |
Syntaxe JavaScript : | objet .style.backgroundImage="url(img_tree.gif)" |
Prise en charge du navigateur
Les nombres dans le tableau indiquent la première version du navigateur qui prend entièrement en charge la propriété.
Property | |||||
---|---|---|---|---|---|
background-image | 1.0 | 4.0 | 1.0 | 1.0 | 3.5 |
Remarque : IE8 et versions antérieures ne prennent pas en charge plusieurs images d'arrière-plan.
Syntaxe CSS
background-image: url|none|initial|inherit;
Valeurs de propriété
Value | Description |
---|---|
url('URL') | The URL to the image. To specify more than one image, separate the URLs with a comma |
none | No background image will be displayed. This is default |
conic-gradient() | Sets a conic gradient as the background image. Define at least two colors |
linear-gradient() | Sets a linear gradient as the background image. Define at least two colors (top to bottom) |
radial-gradient() | Sets a radial gradient as the background image. Define at least two colors (center to edges) |
repeating-conic-gradient() | Repeats a conic gradient |
repeating-linear-gradient() | Repeats a linear gradient |
repeating-radial-gradient() | Repeats a radial gradient |
initial | Sets this property to its default value. Read about initial |
inherit | Inherits this property from its parent element. Read about inherit |
Plus d'exemples
Exemple
Définit deux images d'arrière-plan pour l'élément <body>. Laissez la première image apparaître une seule fois (sans répétition) et laissez la seconde image se répéter :
body {
background-image: url("img_tree.gif"), url("paper.gif");
background-repeat: no-repeat, repeat;
background-color: #cccccc;
}
Exemple
Utilisez différentes propriétés d'arrière-plan pour créer une image "héro":
.hero-image {
background-image: url("photographer.jpg"); /* The
image used */
background-color: #cccccc; /* Used if the image is
unavailable */
height: 500px; /* You must set a specified height */
background-position: center; /* Center the image */
background-repeat: no-repeat; /* Do not repeat the image */
background-size: cover; /* Resize the background image to cover the entire container */
}
Exemple
Définit un dégradé linéaire (deux couleurs) comme image d'arrière-plan pour un élément <div> :
#grad1 {
height: 200px;
background-color: #cccccc;
background-image:
linear-gradient(red, yellow);
}
Exemple
Définit un dégradé linéaire (trois couleurs) comme image d'arrière-plan pour un élément <div> :
#grad1 {
height: 200px;
background-color: #cccccc;
background-image:
linear-gradient(red, yellow, green);
}
Exemple
La fonction repeating-linear-gradient() est utilisée pour répéter des dégradés linéaires :
#grad1 {
height: 200px;
background-color: #cccccc;
background-image:
repeating-linear-gradient(red, yellow 10%, green 20%);
}
Exemple
Définit un dégradé radial (deux couleurs) comme image d'arrière-plan pour un élément <div> :
#grad1 {
height: 200px;
background-color: #cccccc;
background-image:
radial-gradient(red, yellow);
}
Exemple
Définit un dégradé radial (trois couleurs) comme image d'arrière-plan pour un élément <div> :
#grad1 {
height: 200px;
background-color: #cccccc;
background-image:
radial-gradient(red, yellow, green);
}
Exemple
La fonction repeating-radial-gradient() est utilisée pour répéter les dégradés radiaux :
#grad1 {
height: 200px;
background-color: #cccccc;
background-image:
repeating-radial-gradient(red, yellow 10%, green 20%);
}
Pages connexes
Tutoriel CSS : Arrière -plan CSS , Arrière- plans CSS (avancé) , Dégradés CSS
Référence HTML DOM : propriété backgroundImage