background

CSS background 단축 속성은 색상, 이미지, 원점, 크기, 반복 등 여러 배경 스타일을 한 번에 지정합니다.

시도해보기

구성 속성

구문

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 속성을 쉼표로 구분해서 배경 레이어를 여러 개 지정할 수 있습니다.

각 레이어의 구문은 다음과 같습니다.

접근성 고려사항

브라우저는 배경 이미지에 대한 어떠한 추가 정보도 접근성 보조 기술에 제공하지 않습니다. 특히 스크린 리더의 경우 배경 이미지의 존재 유무조차 알려주지 않습니다. 이미지가 페이지 목적의 이해에 필수적인 정보를 갖고 있다면 문서에서 구조적으로 설명하는 편이 좋습니다.

형식 정의

초기값as each of the properties of the shorthand:
적용대상all elements. It also applies to ::first-letter (en-US) and ::first-line (en-US).
상속no
Percentagesas each of the properties of the shorthand:
  • background-position (en-US): refer to the size of the background positioning area minus size of background image; size refers to the width for horizontal offsets and to the height for vertical offsets
  • background-size (en-US): relative to the background positioning area
계산 값as each of the properties of the shorthand:
Animation typeas each of the properties of the shorthand:

형식 구문

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

<image> =
<url> |
<gradient>

<length-percentage> =
<length> |
<percentage>

<url> =
<url()> |
<src()>

<url()> =
url( <string> (en-US) <url-modifier>* ) |
<url-token>

<src()> =
src( <string> (en-US) <url-modifier>* )

예제

색상 키워드와 이미지를 사용한 배경

HTML

html
<p class="topbanner">
  작은 별<br />
  반짝 반짝<br />
  작은 별
</p>
<p class="warning">문단 하나</p>
<p></p>

CSS

css
.warning {
  background: pink;
}

.topbanner {
  background: url("starsolid.gif") #99f repeat-y fixed;
}

결과

명세

Specification
CSS Backgrounds and Borders Module Level 3
# the-background

브라우저 호환성

BCD tables only load in the browser

같이 보기