--- title: background-attachment slug: Web/CSS/background-attachment tags: - CSS - Propriété - Reference translation_of: Web/CSS/background-attachment ---
La propriété background-attachment
définit si la position de l'image d'arrière-plan est fixée dans la zone d'affichage (viewport) ou si celle-ci défile avec le bloc englobant.
Le code source de cet exemple interactif est disponible dans un dépôt GitHub. Si vous souhaitez contribuez à ces exemples, n'hésitez pas à cloner https://github.com/mdn/interactive-examples et à envoyer une pull request !
/* Valeurs avec un mot-clé */ background-attachment: scroll; background-attachment: fixed; background-attachment: local; /* Valeurs globales */ background-attachment: inherit; background-attachment: initial; background-attachment: unset;
La propriété background-attachment
est définie avec un des mots-clés de la liste suivante.
fixed
local
scroll
p { background-image: url("https://mdn.mozillademos.org/files/12057/starsolid.gif"); background-attachment: fixed; }
<p> There were doors all round the hall, but they were all locked; and when Alice had been all the way down one side and up the other, trying every door, she walked sadly down the middle, wondering how she was ever to get out again. </p>
{{EmbedLiveSample("Exemple_simple")}}
On peut utiliser cette propriété lorsqu'on travaille avec plusieurs images en arrière-plan. On peut définir, pour chaque image, un background-attachment
spécifique. Pour cela, on utilisera une liste, séparée par des virgules. Les images seront associées dans l'ordre à chaque propriété d'attachement.
p { background-image: url("https://mdn.mozillademos.org/files/12057/starsolid.gif"), url("https://mdn.mozillademos.org/files/12059/startransparent.gif"); background-attachment: fixed, scroll; background-repeat: no-repeat, repeat-y; }
<p> There were doors all round the hall, but they were all locked; and when Alice had been all the way down one side and up the other, trying every door, she walked sadly down the middle, wondering how she was ever to get out again. Suddenly she came upon a little three-legged table, all made of solid glass; there was nothing on it except a tiny golden key, and Alice's first thought was that it might belong to one of the doors of the hall; but, alas! either the locks were too large, or the key was too small, but at any rate it would not open any of them. However, on the second time round, she came upon a low curtain she had not noticed before, and behind it was a little door about fifteen inches high: she tried the little golden key in the lock, and to her great delight it fitted! </p>
{{EmbedLiveSample("Gestion_de_plusieurs_arrière-plans")}}
Spécification | État | Commentaires |
---|---|---|
{{SpecName('CSS3 Backgrounds', '#the-background-attachment', 'background-attachment')}} | {{Spec2('CSS3 Backgrounds')}} | La propriété a été étendue pour gérer plusieurs arrière-plans et la valeur local . |
{{SpecName('CSS2.1', 'colors.html#propdef-background-attachment', 'background-attachment')}} | {{Spec2('CSS2.1')}} | Aucune modification significative. |
{{SpecName('CSS1', '#background-attachment', 'background-attachment')}} | {{Spec2('CSS1')}} | Définition initiale. |
{{cssinfo}}
{{Compat("css.properties.background-attachment")}}