웹 개발자는 작업 중인 배경 이미지의 시작 위치를 조정해야 할 때 CSS background-position 속성을 사용합니다. 이 기사에서는 background-position 속성과 이를 조정하여 이미지 위치를 변경하는 방법을 살펴보겠습니다.
구문
CSS 선택기 내부의 background-position 속성에 대한 구문은 다음과 같습니다.
background-position: value
background-position에 값을 선언하는 몇 가지 다른 방법이 있습니다:
- 사용할 수 있는 몇 가지 키워드 쌍 값이 있습니다.
- 왼쪽 상단
- 왼쪽 중앙
- 왼쪽 하단
- 오른쪽 상단
- 오른쪽 중앙
- 오른쪽 하단
- 중앙 상단
- 중앙 센터
- 중앙 하단
첫 번째 단어는 수평 또는 x축을 나타냅니다. 두 번째 단어는 수직 또는 y축을 나타냅니다. 값이 하나만 선언되면 y 값은 자동으로 'center'로 설정됩니다.
- x%, y%:
첫 번째 옵션과 마찬가지로 최대 두 개의 값을 지정할 수 있습니다. 하나는 x축의 수평 위치에 대한 것이고 다른 하나는 y축의 수직 위치에 대한 값입니다. 값이 하나만 주어지면 y% 값은 50%로 설정되어 이미지가 y축의 중앙에 오도록 합니다. 왼쪽 위는 0% 0%, 오른쪽 위는 100% 0%, 왼쪽 아래는 100% 0%, 오른쪽 아래는 100% 100%입니다.
- xpos, ypos(px/rem/em/pt):
또한 이전 두 옵션과 유사하지만 숫자는 백분율 대신 CSS 단위로 제공됩니다(원하는 경우 백분율과 rem/em/px/pt를 혼합할 수 있음). 두 번째 값이 제공되지 않으면 ypos가 50%로 설정됩니다.
다음은 첫 번째 옵션을 사용하여 다른 값을 보여주는 몇 가지 예입니다.
왼쪽 상단:
참가자의 81%는 부트캠프에 참석한 후 기술 직업 전망에 대해 더 자신감을 느꼈다고 말했습니다. 지금 부트캠프에 참여하십시오.
부트캠프 졸업생은 부트캠프 시작부터 첫 직장을 찾는 데까지 6개월도 채 걸리지 않았습니다.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Top Left</title>
<style>
body {
background: url("https://images.unsplash.com/photo-1585820809560-b6d13cf9794a?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=3334&q=80");
background-repeat: no-repeat;
background-size: cover;
background-attachment: fixed;
background-position: top left;
display: flex;
flex-flow: row wrap;
}
div {
height: 200px;
width: 25%;
background: transparent;
border: 5px double ivory;
margin: 20px;
}
#text-block {
background: ivory;
border: 5px double black;
display: flex;
align-items: center;
justify-content: center;
font-size: 2rem;
}
</style>
</head>
<body>
<div>
</div>
<div>
</div>
<div>
</div>
<div>
</div>
<div id="text-block">
top left
</div>
<div>
</div>
<div>
</div>
<div>
</div>
<div>
</div>
<script src="script.js">
</script>
</body>
</html> 가운데 오른쪽:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Center Right</title>
<style>
body {
background: url("https://images.unsplash.com/photo-1585820809560-b6d13cf9794a?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=3334&q=80");
background-repeat: no-repeat;
background-size: cover;
background-attachment: fixed;
background-position: center right;
display: flex;
flex-flow: row wrap;
}
div {
height: 200px;
width: 25%;
background: transparent;
border: 5px double ivory;
margin: 20px;
}
#text-block {
background: ivory;
border: 5px double black;
display: flex;
align-items: center;
justify-content: center;
font-size: 2rem;
}
</style>
</head>
<body>
<div>
</div>
<div>
</div>
<div>
</div>
<div>
</div>
<div id="text-block">
center right
</div>
<div>
</div>
<div>
</div>
<div>
</div>
<div>
</div>
<script src="script.js">
</script>
</body>
</html> 왼쪽 하단:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Bottom Left</title>
<style>
body {
background: url("https://images.unsplash.com/photo-1585820809560-b6d13cf9794a?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=3334&q=80");
background-repeat: no-repeat;
background-size: cover;
background-attachment: fixed;
background-position: bottom left;
display: flex;
flex-flow: row wrap;
}
div {
height: 200px;
width: 25%;
background: transparent;
border: 5px double ivory;
margin: 20px;
}
#text-block {
background: ivory;
border: 5px double black;
display: flex;
align-items: center;
justify-content: center;
font-size: 2rem;
}
</style>
</head>
<body>
<div>
</div>
<div>
</div>
<div>
</div>
<div>
</div>
<div id="text-block">
bottom left
</div>
<div>
</div>
<div>
</div>
<div>
</div>
<div>
</div>
<script src="script.js">
</script>
</body>
</html> 결론
위의 코드 편집기가 CSS에서 background-position 속성을 사용하는 방법에 대한 감각을 제공하기를 바랍니다. 여기에서 사용된 배경 이미지를 변경하고 위치를 변경하여 무엇을 할 수 있는지 확인해보세요!