background-origin
Baseline
Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since Juli 2015.
Die background-origin
CSS Eigenschaft legt den Ursprung des Hintergrunds fest: vom Beginn des Rands, innerhalb des Rands oder innerhalb des Paddings.
Probieren Sie es aus
background-origin: border-box;
background-repeat: no-repeat;
background-origin: padding-box;
background-repeat: no-repeat;
background-origin: content-box;
background-repeat: no-repeat;
<section id="default-example">
<div id="example-element">This is the content of the element.</div>
</section>
#example-element {
background-image: url("/shared-assets/images/examples/leopard.jpg");
color: #d73611;
text-shadow: 2px 2px black;
padding: 20px;
border: 10px dashed #333333;
font-size: 2em;
font-weight: bold;
}
Beachten Sie, dass background-origin
ignoriert wird, wenn background-attachment
auf fixed
gesetzt ist.
Syntax
/* Keyword values */
background-origin: border-box;
background-origin: padding-box;
background-origin: content-box;
/* Global values */
background-origin: inherit;
background-origin: initial;
background-origin: revert;
background-origin: revert-layer;
background-origin: unset;
Die background-origin
Eigenschaft wird als eines der unten aufgeführten Schlüsselwörter angegeben.
Werte
border-box
-
Der Hintergrund wird relativ zur border-box positioniert.
padding-box
-
Der Hintergrund wird relativ zur padding-box positioniert. Standardwert.
content-box
-
Der Hintergrund wird relativ zur content-box positioniert.
Formale Definition
Anfangswert | padding-box |
---|---|
Anwendbar auf | alle Elemente. Auch anwendbar auf ::first-letter und ::first-line . |
Vererbt | Nein |
Berechneter Wert | wie angegeben |
Animationstyp | a repeatable list |
Formale Syntax
background-origin =
<visual-box>#
<visual-box> =
content-box |
padding-box |
border-box
Beispiele
>Setzen von Hintergrundursprüngen
.example {
border: 10px double;
padding: 10px;
background: url("image.jpg");
background-position: center left;
background-origin: content-box;
}
#example2 {
border: 4px solid black;
padding: 10px;
background: url("image.gif");
background-repeat: no-repeat;
background-origin: border-box;
}
div {
background-image:
url("logo.jpg"), url("main-back.png"); /* Applies two images to the background */
background-position:
top right,
0px 0px;
background-origin: content-box, padding-box;
}
Verwenden von zwei Verläufen
In diesem Beispiel hat der Kasten einen dicken gepunkteten Rand. Der erste Verlauf verwendet die padding-box
als background-origin
und daher sitzt der Hintergrund innerhalb des Rands. Der zweite verwendet die content-box
und wird daher nur hinter dem Inhalt angezeigt.
.box {
margin: 10px 0;
color: white;
background:
linear-gradient(
90deg,
rgb(131 58 180 / 100%) 0%,
rgb(253 29 29 / 60%) 60%,
rgb(252 176 69 / 100%) 100%
),
radial-gradient(circle, white 0%, black 28%);
border: 20px dashed black;
padding: 20px;
width: 400px;
background-origin: padding-box, content-box;
background-repeat: no-repeat;
}
<div class="box">Hello!</div>
Spezifikationen
Specification |
---|
CSS Backgrounds and Borders Module Level 3> # background-origin> |
Browser-Kompatibilität
Loading…