background
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
Please take two minutes to fill out our short survey.
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> =
[ left | center | right | top | bottom | <length-percentage> ] |
[ left | center | right | <length-percentage> ] [ top | center | bottom | <length-percentage> ] |
[ center | [ left | right ] <length-percentage>? ] && [ center | [ top | bottom ] <length-percentage>? ]
<bg-size> =
[ <length-percentage [0,∞]> | auto ]{1,2} |
cover |
contain
<repeat-style> =
repeat-x |
repeat-y |
[ repeat | space | round | no-repeat ]{1,2}
<attachment> =
scroll |
fixed |
local
<visual-box> =
content-box |
padding-box |
border-box
<background-color> =
<color>
<image> =
<url> |
<gradient>
<length-percentage> =
<length> |
<percentage>
<url> =
<url()> |
<src()>
<url()> =
url( <string> <url-modifier>* ) |
<url-token>
<src()> =
src( <string> <url-modifier>* )
アクセシビリティ
ブラウザーは、背景画像に関する特別な情報を支援技術に提供しません。これは主にスクリーンリーダーにとって重要であり、スクリーンリーダーはその存在を告知しないため、ユーザーには何も伝えません。ページの全体的な目的を理解する上で重要な情報が画像に含まれている場合は、文書の中でその意味を記述した方が良いでしょう。
例
色キーワードと画像による背景の設定
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 # the-background |