background-attachment
Die CSS Eigenschaft background-attachment
legt fest, ob die Position eines Hintergrundbildes innerhalb des viewport fixiert ist, oder ob es mit seinem enthaltenden Block scrollt.
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
Syntax
/* Keyword values */
background-attachment: scroll;
background-attachment: fixed;
background-attachment: local;
/* Global values */
background-attachment: inherit;
background-attachment: initial;
background-attachment: unset;
Die Eigenschaft background-attachment
wird als einer der Schlüsselwortwerte aus der folgenden Liste angegeben.
Werte
fixed
- Der Hintergrund ist relativ zum Ansichtsfenster fixiert. Selbst wenn ein Element über einen Bildlaufmechanismus verfügt, bewegt sich der Hintergrund nicht mit dem Element. (Dies ist nicht kompatibel mit
background-clip: text
.) local
- Der Hintergrund ist relativ zum Inhalt des Elements fixiert. Wenn das Element über einen Bildlaufmechanismus verfügt, wird der Hintergrund mit dem Inhalt des Elements gescrollt, und der Hintergrundmalbereich und der Hintergrundpositionierungsbereich sind relativ zum scrollbaren Bereich des Elements und nicht zum Rand, der sie umrahmt.
scroll
- Der Hintergrund ist relativ zum Element selbst fixiert und scrollt nicht mit seinem Inhalt. (Er ist effektiv an den Rand des Elements gebunden).
<initial>
- Setzt den Standardwert für diese Eigenschaft
<inherit>
- Diese Eigenschaft erbt den Wert von seinem Elternelement.
Formale Definition
Initialwert | scroll |
---|---|
Anwendbar auf | alle Elemente. Auch anwendbar auf ::first-letter und ::first-line . |
Vererbt | Nein |
Berechneter Wert | wie angegeben |
Animationstyp | diskret |
Formale Syntax
Beispiele
Einfaches Beispiel
HTML
<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>
CSS
p {
background-image: url("https://mdn.mozillademos.org/files/12057/starsolid.gif");
background-attachment: fixed;
}
Ergebnis
Mehrere Hintergründe
Diese Eigenschaft unterstützt mehrere Hintergrundbilder. Sie können für jeden Hintergrund ein anderes <attachment>
, durch Kommata getrennt, angeben. Jedes Bild, vom ersten bis zum letzten, wird dem entsprechenden <attachment>
Typ zugeordnet.
HTML
<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>
CSS
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;
}
Ergebnis
Spezifikationen
Spezifikation | Status | Kommentar |
---|---|---|
CSS Backgrounds and Borders Module Level 3 Die Definition von 'background-attachment' in dieser Spezifikation. |
Anwärter Empfehlung | Das Kurzformat-Eigenschaft wurde erweitert, um mehrere Hintergründe und den local Wert zu unterstützen. |
CSS Level 2 (Revision 1) Die Definition von 'background-attachment' in dieser Spezifikation. |
Empfehlung | Keine wesentliche Änderung. |
CSS Level 1 Die Definition von 'background-attachment' in dieser Spezifikation. |
Empfehlung | Keine wesentliche Änderung. |
Browser-Kompatibilität
BCD tables only load in the browser