본문 바로가기
개발언어/JS

[CSS] background-size (배경이미지 크기)

by yunamom 2022. 4. 8.
728x90
300x250

✨배경이미지 크기 조절하기 background-size


속성 설명
auto 원래 배경 이미지 크기만큼 표시(기본 값)
contain 지정한 요소 안에 배경 이미지가 다 들어오도록 이미지를 확대/축소
cover 지정한 요소를 다 덮도록 배경이미지를 확대/축소
크기 값 너비 값과 높이 값을 지정
백분율 지정한 요소를 기준으로 백분율 값을 지정
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>test</title>
<style>
div{
	margin: 2rem;
	width: 200px;
	height: 200px;
	border: 5px solid #decdad;
	background-image: url(img/logo27.png);	
	background-repeat: no-repeat; /* 반복 여부와 반복 방향 */
	display: inline-block;
	text-align: center;
	font-size: 20px;
}
.test {background-size: auto;}
.test2{background-size: contain;}
.test3{background-size: cover;}
.test4{background-size: 100px 100px;}
.test5{background-size: 70% 70%;}

</style>
</head>
<body>
	<div class = "test">auto</div>
	<div class = "test2">contain</div>
	<div class = "test3">cover</div><br>
	<div class = "test4">100px 100px</div>
	<div class = "test5">70% 70%</div>

</body>
</html>

728x90
300x250

코드