연습장

6. 백그라운드 본문

CSS3

6. 백그라운드

js0616 2024. 6. 28. 03:59

https://poiemaweb.com/css3-background

 

CSS3 Background | PoiemaWeb

Background 관련 프로퍼티는 해당 요소의 배경으로 이미지 또는 색상을 정의한다.

poiemaweb.com

 

 

 

 

1. background-image 프로퍼티
요소에 배경 이미지를 지정한다.

 

div {background-img : url("http:// ~~ .png"); }

 

 

2. background-repeat 프로퍼티

 

background-repeat : repeat //  반복 출력 , 기본값

background-repeat : repeat-x  // x축으로만 배경 이미지를 반복할 경우

background-repeat : repeat-y  // y축으로만 배경 이미지를 반복할 경우

background-repeat : no-repeat // 반복 출력 하지 않음

<style>
    div {
      background-image: url("http://abc.png");
      background-repeat: repeat-x;
    }
</style>

 

 

background-image에 복수개의 이미지를 설정할 경우, 먼저 설정된 이미지가 전면에 출력된다.

 

 

3. background-size 프로퍼티
배경 이미지의 사이즈를 지정한다. 

배경 이미지의 고유 비율을 유지하기 때문에 설정에 따라 이미지의 일부가 보이지 않을 수 있다.

프로퍼티값은 px, %, cover, contain 등을 사용한다.

배경이미지의 width, height를 모두 설정할 수 있다. 

이때 첫번째 값은 width, 두번째 값은 height를 의미한다. 

하나의 값만을 지정한 경우, 지정한 값은 width를 의미하게 되며 height는 auto로 지정된다.

 

 

px값 지정

배경이미지 크기가 지정된 px값 그대로 설정된다.

첫번째 값은 width, 두번째 값은 height를 의미한다.

 

<style>
    div {
      background-image: url("http://abc.png");
      background-size: 700px 500px;
    }
</style>

 

 

%값 지정

배경이미지 크기가 지정된 %값에 비례하여 설정된다.

첫번째 값은 width, 두번째 값은 height를 의미한다.

화면을 줄이거나 늘리면 배경이미지의 크기도 따라서 변경되어 찌그러지는 현상이 나타난다.

 

cover 지정

배경이미지의 크기 비율을 유지한 상태에서 부모 요소의 width, height 중 큰값에 배경이미지를 맞춘다.

따라서 이미지의 일부가 보이지 않을 수 있다.

 

<style>
    div {
      background-image: url("http://abc.png");
      background-size: cover;
    }
</style>

 

 

contain 지정

배경이미지의 크기 비율을 유지한 상태에서 부모 요소의 영역에 배경이미지가 보이지 않는 부분없이 전체가 들어갈 수 있도록 이미지 스케일을 조정한다.

 

width, height의 프로퍼티값은 공백으로 구분하여야 한다.

프로퍼티값을 쉼표로 구분하면 다른 배경이미지의 너비를 지정하는 것으로 인식되기 때문에 주의가 필요하다.

 


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

    <style>
        body{
           
            background-color: lightgray;
            display: flex;
            flex-wrap: wrap;
            gap: 20px;
        }
        div {
            margin: 10px;
            background: url("./img/cat.png");
            background-repeat: no-repeat;
            background-color: gray;
            width: 300px;
            height: 300px;
           
           
            line-height: 550px;
            font-size: 24px;
            font-weight: bold;
            color: blue;
           
        }

        #bg_px{
            background-size: 200px;
        }
        #bg_px2{
            background-size: 250px 250px;
        }

        #bg_per{
            background-size: 50%;
        }
        #bg_contain{
            background-size: contain;
        }
        #bg_cover{
            background-size: cover;
        }
    </style>

</head>
<body>
    <div id="bg">원본</div>
    <div id="bg_px">200px</div>
    <div id="bg_px2">250px * 250px </div>
    <div id="bg_per">50%</div>
    <div id="bg_cover">cover</div>
    <div id="bg_contain">contain</div>
</body>
</html>

 

 

300px * 300px 의 회색 영역에 고양이 사진을 넣어서 비교해봤다. 

width height 를 같이넣거나 cover 를 사용할 경우 비율이 달라진다. 

cover 의 경우 아마 사진과 동일한 비율의 div를 만든다면 일정할것으로 예상됨. 

 

 

4. background-attachment 프로퍼티
일반적으로 화면을 스크롤하면 배경 이미지도 함께 스크롤된다. 

화면이 스크롤되더라도 배경이미지는 스크롤되지 않고 고정되어 있게 하려면 background-attachment 프로퍼티에 fixed 키워드를 지정한다.

 

 

5. background-position 프로퍼티
일반적으로 background-image는 좌상단부터 이미지를 출력한다. 이때 background-position 프로퍼티를 사용하면 이미지의 좌표(xpos, ypos)를 지정 할 수 있다.

기본값은 background-position: 0% 0%;로 배경이미지는 우측 상단에 위치하게 된다.

 

<style>
    .example1 {
        background-position: top;
    }
</style>

 

<div class="example1">top</div>
<div class="example2">bottom</div>
<div class="example3">center</div>
<div class="example4">left</div>
<div class="example5">right</div>
<div class="example6">25% 75%</div>
<div class="example7">10px 20px</div>
<div class="example8">0px 0px, center</div> // 2개

 

 

6. background-color 프로퍼티
background-color 프로퍼티는 요소의 배경 색상을 지정한다. 

색상값 또는 transparent 키워드를 지정할 수 있다.

 

<style>
    .bg-col-white {
        background-color: rgb(255, 255, 255);
    }
    div {
        background-color: transparent;
    }
</style>

 

transparent : 배경색 없음 (투명?)

 

 

7. background Shorthand
background-color, background-image, background-repeat, background-position를 한번에 정의하기 위한 Shorthand Syntax이다.

 

// order
background: color || image || repeat || attachment || position

 

<style>
 div {
      /* background: color || image || repeat || attachment || position */
      background: #FFEE99 url("http://abc.png") no-repeat center;
    }
</style>

 

 

'CSS3' 카테고리의 다른 글

8. position 요소의 위치 정의  (0) 2024.06.28
7. 폰트와 텍스트  (0) 2024.06.28
5. display, visibility, opacity  (0) 2024.06.28
4. 박스모델  (0) 2024.06.27
3. 프로퍼티 값의 단위  (0) 2024.06.27