• 회원가입
  • 로그인
  • 구글아이디로 로그인

[DOM_Style] JS - background 속성 ★ - (배경색/배경이미지) 설정/반환 (= background속성 / 백그라운드속성)

목차
  1. background 예제 - 배경색/배경이미지 설정
  2. background 정의
  3. background 구문
  4. background 예제 - div 요소의 배경 변경
  5. background 예제 - 웹문서 배경색 변경
  6. background 예제 - 웹문서 배경이미지 설정
  7. background 예제 - 배경이미지 반복 없이 설정
  8. background 에제 - 배경이미지 고정 설정
  9. background 예제 - 배경이미지 위치 변경
  10. background 예제 - 웹문서 background 속성값 반환

 

background 예제 - 배경색/배경이미지 설정

 

<button onclick="homzzang()">클릭</button>


<script>

function homzzang() {

  document.body.style.background = "Tomato url('hz.png') no-repeat right top";

}

</script>

 

결과보기

 

background 정의

 

아래 (배경색/배경이미지) 관련 속성 일괄 설정/반환. (※ 순서는 무관)



 

1.

되도록이면 개별 속성 이용 권장. (∵ 보다 직관적)

2.

  • 기본값: transparent none repeat scroll 0% 0% auto padding-box border-box
  • CSS버전: CSS1 + CSS3


3.

모든 브라우저 지원.


 

background 구문

 

[반환]

object.style.background

 

[설정]

object.style.background = "color image repeat attachment position size origin clip|initial|inherit"

 


[속성값]

 

  • color - 배경색
  • image - 배경이미지
  • repeat - 배경이미지 반복 방식
  • attachment - 배경이미지 고정/스크롤 여부
  • position - 배경이미지 시작 위치
  • size - 배경이미지 크기
  • origin - 배경 위치 영역.
  • clip - 배경이미지 페인팅 영역.
  • initial - 이 속성의 기본값으로 설정
  • inherit - 부모 요소의 속성값 상속

 


[반환값]

 

배경 관련 문자열 반환.

 

 

background 예제 - div 요소의 배경 변경

 

<style>

#hz {

  width: 300px;

  height: 300px;

  background: tomato url('https://i.imgur.com/PQNhCln.gif') no-repeat fixed center;

  color: white;

}

</style>


<button onclick="homzzang()">클릭</button>


<div id="hz">

  <p>홈짱닷컴 Homzzang.com</p>

</div>


<script>

function homzzang() {

  document.getElementById("hz").style.background = "url('https://i.imgur.com/PQNhCln.gif') lightblue repeat-x center";

}

</script>

 

결과보기

 

background 예제 - 웹문서 배경색 변경

 

<h1>홈짱닷컴 Homzzang.com</h1>


<button type="button" onclick="homzzang()">클릭</button>


<script>

function homzzang() {

  document.body.style.backgroundColor = "red";

}

</script>

 

결과보기

 

background 예제 - 웹문서 배경이미지 설정

 

<h1>홈짱닷컴 Homzzang.com</h1>

<button type="button" onclick="homzzang()">클릭</button>


<script>

function homzzang() {

  document.body.style.backgroundColor = "lightblue";

  document.body.style.backgroundImage = "url('https://i.imgur.com/PQNhCln.gif')";

}

</script>

 

결과보기

 

background 예제 - 배경이미지 반복 없이 설정

 

<h1>홈짱닷컴 Homzzang.com</h1>

<button type="button" onclick="homzzang()">클릭</button>


<script>

function homzzang() {

  document.body.style.background = "lightblue url('https://i.imgur.com/PQNhCln.gif') no-repeat";

}

</script>

 

결과보기


PS. 개별 속성 이용해 표현하면 더 직관적.

 

<h1>홈짱닷컴 Homzzang.com</h1>

<button type="button" onclick="homzzang()">클릭</button>


<script>

function homzzang() {

  document.body.style.backgroundColor = "lightblue";

  document.body.style.backgroundImage = "url('https://i.imgur.com/PQNhCln.gif')";

  document.body.style.backgroundRepeat = "no-repeat";

}

</script>

 

결과보기

 

background 에제 - 배경이미지 고정 설정

[예제1] - CSS 배경이미지 고정시키려면, backgroundAttachment 사용.

※ 주의: background 속성 이용하면 배경이미지 사라짐.

 

<style>

body {

  background: #f3f3f3 url('https://i.imgur.com/PQNhCln.gif') no-repeat right top;

  height:1000px;

}

</style>


<h1>홈짱닷컴 Homzzang.com</h1>


<button onclick="homzzang()">클릭</button>

 

<script>

function homzzang() {

  document.body.style.backgroundAttachment = "fixed";

}

</script>

 

결과보기


[예제2] - JS로 배경이미지 새로 설정할 땐, background 속성 이용 가능.

 

<style>

body {

  height:1000px;

}

</style>


<h1>홈짱닷컴 Homzzang.com</h1>


<button onclick="homzzang()">클릭</button>

 

<script>

function homzzang() {

  document.body.style.background = "#f3f3f3 url('https://i.imgur.com/PQNhCln.gif') no-repeat right top fixed";

}

</script>

 

결과보기

 

background 예제 - 배경이미지 위치 변경

 

<style>

body {

  background-image: url('https://i.imgur.com/PQNhCln.gif');

  background-repeat: no-repeat;

}

</style>


<button type="button" onclick="homzzang()">클릭</button>


<script>

function homzzang() {

  document.body.style.backgroundPosition="top right"; 

}

</script>

 

결과보기

 

background 예제 - 웹문서 background 속성값 반환

 

<body style="background:#f3f3f3 url('https://i.imgur.com/PQNhCln.gif') no-repeat right top;">


<h1>홈짱닷컴 Homzzang.com</h1>


<button type="button" onclick="homzzang()">클릭</button>


<script>

function homzzang() {

  alert(document.body.style.background);

}

</script>

 

결과보기 


PS. 결과값 (※ 주의: 선언된 스타일 순서 및 표현 방법이 다를 수 있음.)

 

url("https://i.imgur.com/PQNhCln.gif") right top no-repeat rgb(243, 243, 243))

 



분류 제목
HTML_Objects JS - <address> 객체 - 연락처 (= address태그 = address요소 = 어드레스태그)
HTML_Objects JS - <area> 객체 - 이미지맵 링크 영역 (= area태그 = 에어리어태그) (HTML5수정)
HTML_Objects JS - <article> 객체 ★ - 자족적인 독립콘텐츠 (= 아티클태그 = article태그. IE9)
HTML_Objects JS - <aside> 객체 ★ - 관련별도콘텐츠표시 (= 부가콘텐츠 = 어사이드태그 = aside태그) (…
HTML_Objects JS - <audio> 객체 ★ - 음성파일재생 (=오디오태그 = audio태그) ※ 음성파일형식별로 브라우…
HTML_Objects JS - <b> 객체 ★ - 굵은글씨 (= b태그 = b요소 = 폰트굵기 = 비태그 = 볼드태그 = bol…
HTML_Objects JS - <base> 객체 - 상대주소 기본 URL/타켓 지정 (= base 태그 = 베이스 태그)
HTML_Objects JS - <bdo> 객체 - 텍스트출력방향 (= bdo태그 = bdo요소 = 글자방향 = 브도태그 = 비도태…
HTML_Objects JS - <blockquote> 객체 ★ - 긴 인용구 (= blockquote태그 = blockquote요…
HTML_Objects JS - <body> 객체 ★ - 문서영역 (= body태그 = body요소 = 본문영역 = 바디태그 = 보…
HTML_Objects JS - <br> 객체 ★ - 줄바꿈 (=br태그 = br요소 = 한줄아래로 = 비알태그 = 브르태그 = 브…
HTML_Objects JS - <button> 객체 ★ - 클릭버튼 (= button태그 = 버튼태그) (HTML5수정)
HTML_Objects JS - <canvas> 객체 - 그림그리기 (= canvas태그 = 캔버스태그) (IE9 이상. HTML추…
HTML_Objects JS - <caption> 객체 - 테이블제목 (= 표제목 = caption태그 = 캡션태그) (HTML5 …
HTML_Objects JS - <cite> 객체 - 인용구 제목 (= cite태그 = cite요소 = 저작물제목 = 인용태그 = …
HTML_Objects JS - <code> 객체 - 컴퓨터소스코드 출력 (= code태그 = 코드태그)
HTML_Objects JS - <col> 객체 - 테이블열그룹 (= col태그 = 콜태그) (HTML5 수정)
HTML_Objects JS - <colgroup> 객체 - 테이블의 col태그묶음 (= colgroup태그 = 콜그룹태그, HTM…
HTML_Objects JS - <datalist> 객체 - 입력가능값리스트 (= datalist태그 = 데이터리스트태그) (IE1…
HTML_Objects JS - <dd> 객체 - 항목세부설명 (= dd태그 = 디디태그)
55/67
목록
찾아주셔서 감사합니다. Since 2012