background
Baseline
Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2015年7月.
background は CSS の一括指定プロパティで、色、画像、原点と寸法、反復方法など、背景に関するすべてのスタイルプロパティを一括で設定します。 background 一括指定プロパティの値宣言で設定されていない成分のプロパティは、既定値に設定されます。
試してみましょう
background: green;
background: content-box radial-gradient(crimson, skyblue);
background: no-repeat url("/shared-assets/images/examples/lizard.png");
background: left 5% / 15% 60% repeat-x
url("/shared-assets/images/examples/star.png");
background:
center / contain no-repeat
url("/shared-assets/images/examples/firefox-logo.svg"),
#eee 35% url("/shared-assets/images/examples/lizard.png");
<section id="default-example">
<div id="example-element"></div>
</section>
#example-element {
min-width: 100%;
min-height: 100%;
padding: 10%;
}
構成要素のプロパティ
このプロパティは以下の CSS プロパティの一括指定です。
構文
/* <background-color> を使用 */
background: green;
/* <bg-image> と <repeat-style> を使用 */
background: url("test.jpg") repeat-y;
/* <box> と <background-color> を使用 */
background: border-box red;
/* 単一の画像、中央寄せかつ縮小 */
background: no-repeat center/80% url("../img/image.png");
/* グローバル値 */
background: inherit;
background: initial;
background: revert;
background: revert-layer;
background: unset;
background プロパティは1つまたは複数の背景レイヤーをカンマで区切って指定します。
それぞれのレイヤーの構文は以下の通りです。
-
それぞれのレイヤーは、以下の値をそれぞれ 0 ~ 1 回含めることができます。
<attachment><bg-image><position><bg-size><repeat-style>
-
<bg-size>の値は<position>の直後に '/' の文字で区切って含めなければなりません。例:center/80% -
<box>の値は 0 ~ 2 回含めることができます。1 回の場合はbackground-originとbackground-clipの両方に設定されます。2 回の場合は、1 つ目はbackground-originに、2 つ目はbackground-clipに設定されます。 -
<background-color>の値は最後のレイヤーの指定でのみ含めることができます。
値
<attachment>-
background-attachmentを参照。既定値はscrollです。 <box>-
background-clipおよびbackground-originを参照。既定値はそれぞれborder-boxおよびpadding-boxです。 <background-color>-
background-colorを参照。既定値はtransparentです。 <bg-image>-
background-imageを参照。既定値はnoneです。 <position>-
background-positionを参照。既定値は 0% 0% です。 <repeat-style>-
background-repeatを参照。既定値はrepeatです。 <bg-size>-
background-sizeを参照。既定値はautoです。
以下の 3 行の CSS は等価です。
background: none;
background: transparent;
background: repeat scroll 0% 0% / auto padding-box border-box none transparent;
公式定義
| 初期値 | 一括指定の次の各プロパティとして
|
|---|---|
| 適用対象 | すべての要素。 ::first-letterおよび::first-line にも適用されます。 |
| 継承 | なし |
| パーセント値 | 一括指定の次の各プロパティとして
|
| 計算値 | 一括指定の次の各プロパティとして
|
| アニメーションの種類 | 一括指定の次の各プロパティとして
|
形式文法
background =
<bg-layer>#? , <final-bg-layer>
<bg-layer> =
<bg-image> ||
<bg-position> [ / <bg-size> ]? ||
<repeat-style> ||
<attachment> ||
<visual-box> ||
<visual-box>
<final-bg-layer> =
<bg-image> ||
<bg-position> [ / <bg-size> ]? ||
<repeat-style> ||
<attachment> ||
<visual-box> ||
<visual-box> ||
<'background-color'>
<bg-image> =
<image> |
none
<bg-position> =
<position> |
<position-three>
<bg-size> =
[ <length-percentage [0,∞]> | auto ]{1,2} |
cover |
contain
<repeat-style> =
repeat-x |
repeat-y |
<repetition>{1,2}
<attachment> =
scroll |
fixed |
local
<visual-box> =
content-box |
padding-box |
border-box
<background-color> =
<color>
<image> =
<url> |
<image()> |
<image-set()> |
<cross-fade()> |
<element()> |
<gradient>
<position> =
<position-one> |
<position-two> |
<position-four>
<position-three> =
[ left | center | right ] && [ [ top | bottom ] <length-percentage> ] |
[ [ left | right ] <length-percentage> ] && [ top | center | bottom ]
<length-percentage> =
<length> |
<percentage>
<repetition> =
repeat |
space |
round |
no-repeat
<image()> =
image( <image-tags>? [ <image-src>? , <color>? ]! )
<image-set()> =
image-set( <image-set-option># )
<cross-fade()> =
cross-fade( <cf-image># )
<element()> =
element( <id-selector> )
<position-one> =
left |
center |
right |
top |
bottom |
x-start |
x-end |
y-start |
y-end |
block-start |
block-end |
inline-start |
inline-end |
<length-percentage>
<position-two> =
[ left | center | right | x-start | x-end ] && [ top | center | bottom | y-start | y-end ] |
[ left | center | right | x-start | x-end | <length-percentage> ] [ top | center | bottom | y-start | y-end | <length-percentage> ] |
[ block-start | center | block-end ] && [ inline-start | center | inline-end ] |
[ start | center | end ]{2}
<position-four> =
[ [ left | right | x-start | x-end ] <length-percentage> ] && [ [ top | bottom | y-start | y-end ] <length-percentage> ] |
[ [ block-start | block-end ] <length-percentage> ] && [ [ inline-start | inline-end ] <length-percentage> ] |
[ [ start | end ] <length-percentage> ]{2}
<image-tags> =
ltr |
rtl
<image-src> =
<url> |
<string>
<image-set-option> =
[ <image> | <string> ] [ <resolution> || type( <string> ) ]?
<cf-image> =
[ <image> | <color> ] &&
<percentage [0,100]>?
<id-selector> =
<hash-token>
アクセシビリティ
ブラウザーは、背景画像に関する特別な情報を支援技術に提供しません。これは主にスクリーンリーダーにとって重要であり、スクリーンリーダーはその存在を告知しないため、ユーザーには何も伝えません。ページの全体的な目的を理解する上で重要な情報が画像に含まれている場合は、文書の中でその意味を記述した方が良いでしょう。
例
>色キーワードと画像による背景の設定
HTML
<p class="top-banner">
Starry sky<br />
Twinkle twinkle<br />
Starry sky
</p>
<p class="warning">これは段落です</p>
<p></p>
CSS
.warning {
background: pink;
}
.top-banner {
background: url("star-solid.gif") #99f repeat-y fixed;
}
結果
仕様書
| Specification |
|---|
| CSS Backgrounds and Borders Module Level 3> # background> |
ブラウザーの互換性
Loading…