KDT/HTML | CSS

CSS 배경

jhwannabe 2023. 3. 29. 14:54

1. background

  • 배경 속성을 한꺼번에 적용
background 파일위치 반복여부 위치 사이즈 ...

2. background-color

  • HTML 요소의 배경색을 설정

 

예제 코드

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CSS 배경1</title>
    <style>
        body {background-color: deepskyblue;}
        div {background-color: white; width: 60%; padding: 20px; border: 3px solid magenta;}
    </style>
</head>

<body>
    <h2>CSS 배경1</h2>
    <div>
        <h2>배경 적용하기</h2>
        <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Earum mollitia, placeat voluptatem possimus doloribus, doloremque accusamus quod accusantium repellat nesciunt aut unde non illum corrupti quidem excepturi quaerat hic tempora!</p>
    </div>
</body>
</html>


3. background-image

  • HTML 요소의 배경으로 나타날 배경 이미지를 설정
  • 배경 이미지는 기본 설정으로 반복되어 나타남
background-image: url(파일경로)

4. background-repeat

  • 배경 이미지를 수평이나 수직방향으로 반복하도록 설정(repeat-x, repeat-y, no-repeat)

 

예제 코드

<!DOCTYPE html>
<html lang="en">
<head>
    <title>CSS 배경2</title>
    <style>
        body {
            background-image: url(./bus.png);
            background-repeat: repeat-x;
            background-repeat: repeat-y;
            background-repeat: no-repeat;
        }
    </style>
</head>

<body>
    <h2>CSS 배경2</h2>
</body>
</html>


5. background-position

  • 반복되지 않는 배경 이미지의 상대 위치를 설정
  • %나 px을 사용해서 상대위치를 직접 설정할 수 잇음
  • 상대 위치를 결정하는 기준은 왼쪽 상단(left top)
    left top        center top    right top
    left center     center        right center
    left bottom     center bottom right bottom

    background-position: center bottom;
    background-position: 가로위치값 세로위치값;
    예) background-position: 10% 100px;

6. background-attachment

  • 위치가 설정된 배경 이미지를 원하는 위치에 고정시킬 수 있음
  • 고정된 배경 이미지는 스크롤과 무관하게 화면의 위치에서 이동되지 않음
  • fixed, scroll

 

예제 코드

<!DOCTYPE html>
<html lang="en">
<head>
    <title>CSS 배경3</title>
    <style>
        body {
            background-image: url(bus.png);
            background-repeat: no-repeat;
            background-position: right bottom;
            background-attachment: fixed;
        }
        div {
            width: 60%;
            height: 300px;
            border: 3px solid magenta;
            padding: 10px;
            margin-bottom: 20px;
            background-image: url(bus.png);
            background-repeat: no-repeat;
            background-position: center;
        }
        .bg1 {background-position: center bottom;}
        .bg2 {background-position: right bottom;}
        .bg3 {background-position: 20% 100px;}
    </style>
</head>

<body>
    <h2>CSS 배경3</h2>
    <div class="bg1">
        <h2>background-position 1</h2>
        <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Quisquam, illum. Inventore, rerum ullam voluptates dolor nobis cupiditate excepturi modi culpa reprehenderit quasi? Distinctio optio laborum eum unde esse ad quisquam.</p>
    </div>
    <div class="bg2">
        <h2>background-position 2</h2>
        <p>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Quae eligendi natus numquam ipsam culpa, corrupti voluptate et magni veniam obcaecati iusto est voluptates deleniti delectus praesentium placeat sequi accusantium sit.</p>
    </div>
    <div class="bg3">
        <h2>background-position 3</h2>
        <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Veritatis, maxime! Quibusdam labore iusto nostrum rem, vero beatae laudantium error commodi nisi voluptas. Architecto, ad. Quia minus mollitia ducimus placeat repudiandae.</p>
    </div>
</body>
</html>


7. background-size

  • 배경 이미지 크기를 설정
  • px, %, contain, cover
  • contain
    • 배경 이미지의 가로, 세로 모두 요소보다 작다는 조건하에 가능한 설정
    • 가로, 세로 비율은 유지
    • 배경 이미지의 크기는 요소의 크기보다 항상 작거나 같음
  • cover
    • 배경 이미지의 가로, 세로 길이 모두 요소보다 크다는 조건하에 가능한 설정
    • 가로, 세로 비율은 유지
    • 배경 이미지의 크기는 요소의 크기보다 항상 크거나 같음

 

예제 코드1

<!DOCTYPE html>
<html lang="en">
<head>
    <title>CSS 배경4</title>
    <style>
        div {
            background-image: url(bus.png);
            background-repeat: no-repeat;
            width: 150px;
            height: 150px;
            border: 2px solid red;
            margin-bottom: 20px;
        }
        .background1 {background-size: 50px 100px;}
        .background2 {background-size: 500px 500px; background-position: center;}
        .background3 {background-size: contain;}
        .background4 {width:100px; height: 70px; background-size: cover; background-position: bottom center;}
    </style>
</head>

<body>
    <h2>CSS 배경4</h2>
    <div class="background1"></div>
    <div class="background2"></div>
    <div class="background3"></div>
    <div class="background4"></div>
</body>
</html>

 

예제 코드2

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CSS 배경5</title>
    <style>
        html {
            background: url(wallpaper.jpg) no-repeat center fixed;
            background-size: cover;
        }
    </style>
</head>

<body>
    <h2>CSS 배경5</h2>
</body>
</html>


[참고] 무료 jpeg 사이트

728x90
반응형