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

[background] CSS - background-size 속성 ★ - 배경이미지 크기 (IE9) ※ 배경이미지 중첩

목차
  1. background-size 예제 - 배경 이미지 크기
  2. background-size 정의
  3. background-size 구문
  4. background-size 예제 - 속성값 적용 효과

 

background-size 예제 - 배경 이미지 크기

auto / 300px 100px

 

<style>

#hz1 {

    border: 2px solid black;

    padding: 25px;

    background: url('https://source.unsplash.com/random');

    background-repeat: no-repeat;

    background-size: auto;

}


#hz2 {

    border: 2px solid black;

    padding: 25px;

    background: url('https://source.unsplash.com/random');

    background-repeat: no-repeat;

    background-size: 300px 100px;

}

</style>


<h2>background-size: auto (default):</h2>

<div id="hz1">

    <h2>홈짱닷컴</h2>

    <p>Homzzang.com</p>

</div>


<h2>background-size: 300px 100px:</h2>

<div id="hz2">

    <h2>홈짱닷컴</h2>

    <p>Homzzang.com</p>

</div>


결과보기

 

background-size 정의

 

배경이미지 크기 지정.

 


 

1.

기본값 : auto

상속여부 : X

애니효과 : O

CSS버전: CSS3

JS구문 : object.style.backgroundSize="60px 120px" 

 

2.

IE9 이상 주요 최신브라우저 모두 지원.

 

 

background-size 구문

 

background-size: auto|length|cover|contain|initial|inherit;

 


[속성값]

 

auto

기본값. 배경이미지 원래 크기.

 

length

배경이미지의 너비・높이 설정. 

첫 번째 값: 너비 , 두 번째 값: 높이
하나의 값만 적으면 두 번째 값은 "auto"로 설정.

길이 단위는 px, cm 등 사용 가능.

 

% (백분율)

배경이미지의 너비・높이를 부모 요소의 백분율로 설정. 

첫 번째 값: 너비, 두 번째 값: 높이

하나의 값만 제공하면 두 번째 값은 "auto"로 설정.

 

cover

이미지 늘이거나 가장자리 중 하나를 조금 잘라내도 컨테이너 전체 덮게끔 배경이미지 크기 조정.

 

contain

이미지가 완전히 보이도록 배경이미지 크기 조정.

 

initial

이 속성의 기본값으로 설정.

 

inherit

이 요소를 부모 요소에서 상속. 

 

 

background-size 예제 - 속성값 적용 효과

[100 % 100%  / 75% 75%]

 

<style>

#hz1 {

  border: 2px solid black;

  padding: 25px;

  background: url('https://source.unsplash.com/random');

  background-repeat: no-repeat;

  background-size: 100% 100%;

}


#hz2 {

  border: 2px solid black;

  padding: 25px;

  background: url('https://source.unsplash.com/random');

  background-repeat: no-repeat;

  background-size: 75% 75%;

}

</style>


<h2>background-size: auto (default):</h2>

<div id="hz1">

  <h2>홈짱닷컴</h2>

  <p>Homzzang.com</p>

</div>


<h2>background-size: 300px 100px:</h2>

<div id="hz2">

  <h2>홈짱닷컴</h2>

  <p>Homzzang.com</p>

</div>

 

결과보기


[cover / contain]

 

<style>

#hz1 {

  border: 2px solid black;

  padding: 25px;

  background: url('https://source.unsplash.com/random');

  background-repeat: no-repeat;

  background-size: cover;

}


#hz2 {

  border: 2px solid black;

  padding: 25px;

  background: url('https://source.unsplash.com/random');

  background-repeat: no-repeat;

  background-size: contain;

}

</style>


<h2>background-size: auto (default):</h2>

<div id="hz1">

  <h2>홈짱닷컴</h2>

  <p>Homzzang.com</p>

</div>


<h2>background-size: 300px 100px:</h2>

<div id="hz2">

  <h2>홈짱닷컴</h2>

  <p>Homzzang.com</p>

</div>


결과보기


[contain,cover : 배경이미지 중첩 (※ 첫 번째는 contain 지정, 두 번째는 cover 지정)]

<style>

#hz {

  border: 2px solid black;

  padding: 25px;

  background: url('https://source.unsplash.com/random'), url('https://source.unsplash.com/random');

  background-repeat: no-repeat;

  background-size: contain, cover;

}

</style>


<div id="hz">

  <h2>홈짱닷컴</h2>

  <p>Homzzang.com</p>

</div>

 

결과보기


[반응형 배경이미지]

 

<meta name="viewport" content="width=device-width, initial-scale=1">

 

<style>

body {

  margin: 0;

  font-family: Arial, Helvetica, sans-serif;

}


.hz-image {

  background-image: url("https://source.unsplash.com/random");

  background-color: #cccccc;

  height: 500px;

  background-position: center;

  background-repeat: no-repeat;

  background-size: cover;

  position: relative;

}


.hz-text {

  text-align: center;

  position: absolute;

  top: 50%;

  left: 50%;

  transform: translate(-50%, -50%);

  color: white;

}

</style>


<div class="hz-image">

    <div class="hz-text">

        <h1 style="font-size:2rem">홈짱닷컴</h1>

        <h3>Homzzang.com</h3>

        <button>바로가기</button>

    </div>

</div>

<p>코딩언어 강의 : HTML CSS JS JQEURY PHP SQL</p>

<p>빌더서버 강의 : 그누보드・아미나・영카트 강의</p>


결과보기 


 

방문 감사합니다. (즐겨찾기 등록: Ctrl + D)

분류 제목
intro CSS - 브라우저 접두어 (= vendor prefix = 벤더 프리픽스)
border CSS - border-radius 속성 ★ - 테두리둥글기 지정 (= border-radius속성 = 보더…
border CSS - border-top-left-radius 속성 - 테두리상단왼쪽둥글기 지정 (= 보더탑레프트레이디…
border CSS - border-top-right-radius 속성 - 테두리상단우측둥글기 지정 (= 보더탑라이트레이…
border CSS - border-bottom-right-radius 속성 - 테두리 하단 오른쪽 모서리 둥글게 (= …
border CSS - border-bottom-left-radius 속성 - 테두리하단왼쪽모서리둥글게 (= border…
border CSS - border-image 속성 - 테두리이미지 (= border-image속성 = 보더이미지속성, …
border CSS - border-image-source 속성 - 테두리 이미지 주소 (= border-image-so…
border CSS - border-image-slice 속성 - 테두리이미지자르기 (IE11)
border CSS - border-image-width 속성 - 테두리 이미지 너비 (= border-image-wid…
border CSS - border-image-outset 속성 - 테두리 경계 넘는 이미지 양. (= 보더이미지아웃셋속…
border CSS - border-image-repeat 속성 - 테두리 이미지 반복할지 늘릴지 선택 (= 보더이미지리…
background CSS - background-clip 속성 - 배경영역 (= background-clip속성 = 백그라운드…
background CSS - background-origin 속성 - 배경이미지 좌표시작점 (= 백그라운드오리진 속성)
background CSS - background-size 속성 ★ - 배경이미지 크기 (IE9) ※ 배경이미지 중첩
color CSS - RGBA , HSL , HSLA , opacity - (색상코드)
gradient CSS - Gradient (그레이디언트 = 그라디언트) 종류 - (색번짐변화=색변화) : IE 10
shadow CSS - text-shadow 속성 - 글자 그림자/음영 효과 주기 (= text-shadow속성 = 텍스…
shadow CSS - box-shadow 속성 - 요소 그림자 효과 주기 (= box-shadow속성 = 박스섀도속성)…
text CSS - text-align-last 속성 - 단락마지막라인정렬 (= text-align-last속성 = …
7/25
목록
찾아주셔서 감사합니다. Since 2012