`object-fit` 및 `object-position` 속성
이 글에서는 object-fit
및 object-position
속성에 대해 설명합니다.
이미지와 비디오 같은 객체를 정렬하는 방법과 CSS에 구현하는 방법을 배울 수 있습니다.
YouTube Video
미리보기를 위한 HTML
css-object-fit-position.html
1<!DOCTYPE html>
2<html lang="en">
3<head>
4 <meta charset="UTF-8">
5 <meta name="viewport" content="width=device-width, initial-scale=1.0">
6 <title>CSS Properties Example</title>
7 <link rel="stylesheet" href="css-base.css">
8 <link rel="stylesheet" href="css-object-fit-position.css">
9</head>
10<body>
11 <!-- Header -->
12 <header>
13 <h1>CSS Properties For Object</h1>
14 </header>
15
16 <!-- Main content -->
17 <main>
18 <header>
19 <h2>Object Fit And Object Position Properties</h2>
20 </header>
21 <article>
22 <h3>object-fit : fill</h3>
23 <section>
24 <header><h4>CSS</h4></header>
25<pre class="sample">
26.container-fill img {
27 width: 100%;
28 height: 100%;
29 object-fit: fill;
30}
31</pre>
32 <header><h4>HTML</h4></header>
33<pre>
34<div class="container-fill" style="width: 150px; height: 100px;">
35 <img src="example.jpg" alt="Example Image">
36</div>
37</pre>
38 <header><h4>HTML+CSS</h4></header>
39 <section class="sample-view">
40 <div class="container-fill" style="width: 150px; height: 100px;">
41 <img src="example.jpg" alt="Example Image">
42 </div>
43 </section>
44 </section>
45 </article>
46 <article>
47 <h3>object-fit : contain</h3>
48 <section>
49 <header><h4>CSS</h4></header>
50<pre class="sample">
51.container-contain img {
52 width: 100%;
53 height: 100%;
54 object-fit: contain;
55}
56</pre>
57 <header><h4>HTML</h4></header>
58<pre>
59<div class="container-contain" style="width: 150px; height: 100px;">
60 <img src="example.jpg" alt="Example Image">
61</div>
62</pre>
63 <header><h4>HTML+CSS</h4></header>
64 <section class="sample-view">
65 <div class="container-contain" style="width: 150px; height: 100px;">
66 <img src="example.jpg" alt="Example Image">
67 </div>
68 </section>
69 </section>
70 </article>
71 <article>
72 <h3>object-fit : cover</h3>
73 <section>
74 <header><h4>CSS</h4></header>
75<pre class="sample">
76.container-cover img {
77 width: 100%;
78 height: 100%;
79 object-fit: cover;
80}
81</pre>
82 <header><h4>HTML</h4></header>
83<pre>
84<div class="container-cover" style="width: 150px; height: 100px;">
85 <img src="example.jpg" alt="Example Image">
86</div>
87</pre>
88 <header><h4>HTML+CSS</h4></header>
89 <section class="sample-view">
90 <div class="container-cover" style="width: 150px; height: 100px;">
91 <img src="example.jpg" alt="Example Image">
92 </div>
93 </section>
94 </section>
95 </article>
96 <article>
97 <h3>object-fit : none</h3>
98 <section>
99 <header><h4>CSS</h4></header>
100<pre class="sample">
101.container-none img {
102 width: 100%;
103 height: 100%;
104 object-fit: none;
105}
106</pre>
107 <header><h4>HTML</h4></header>
108<pre>
109<div class="container-none" style="width: 150px; height: 100px;">
110 <img src="example.jpg" alt="Example Image">
111</div>
112</pre>
113 <header><h4>HTML+CSS</h4></header>
114 <section class="sample-view">
115 <div class="container-none" style="width: 150px; height: 100px;">
116 <img src="example.jpg" alt="Example Image">
117 </div>
118 </section>
119 </section>
120 </article>
121 <article>
122 <h3>object-fit : scale-down</h3>
123 <section>
124 <header><h4>CSS</h4></header>
125<pre class="sample">
126.container-scale-down img {
127 width: 100%;
128 height: 100%;
129 object-fit: scale-down;
130}
131</pre>
132 <header><h4>HTML</h4></header>
133<pre>
134<div class="container-scale-down" style="width: 150px; height: 100px;">
135 <img src="example.jpg" alt="Example Image">
136</div>
137</pre>
138 <header><h4>HTML+CSS</h4></header>
139 <section class="sample-view">
140 <div class="container-scale-down" style="width: 150px; height: 100px;">
141 <img src="example.jpg" alt="Example Image">
142 </div>
143 </section>
144 </section>
145 </article>
146 <article>
147 <h3>object-fit : Example(Cover)</h3>
148 <section>
149 <header><h4>CSS</h4></header>
150<pre class="sample">
151.container-example-cover img {
152 width: 100%;
153 height: 100%;
154 object-fit: cover;
155}
156</pre>
157 <header><h4>HTML</h4></header>
158<pre>
159<div class="container-example-cover" style="width: 150px; height: 100px;">
160 <img src="example.jpg" alt="Example Image">
161</div>
162</pre>
163 <header><h4>HTML+CSS</h4></header>
164 <section class="sample-view">
165 <div class="container-example-cover" style="width: 150px; height: 100px;">
166 <img src="example.jpg" alt="Example Image">
167 </div>
168 </section>
169 </section>
170 </article>
171 <article>
172 <h3>object-fit : Example(Contain)</h3>
173 <section>
174 <header><h4>CSS</h4></header>
175<pre class="sample">
176.container-example-contain img {
177 width: 100%;
178 height: 100%;
179 object-fit: contain;
180}
181</pre>
182 <header><h4>HTML</h4></header>
183<pre>
184<div class="container-example-contain" style="width: 150px; height: 100px;">
185 <img src="example.jpg" alt="Example Image">
186</div>
187</pre>
188 <header><h4>HTML+CSS</h4></header>
189 <section class="sample-view">
190 <div class="container-example-contain" style="width: 150px; height: 100px;">
191 <img src="example.jpg" alt="Example Image">
192 </div>
193 </section>
194 </section>
195 </article>
196 <article>
197 <h3>object-fit Common Mistake Example</h3>
198 <section>
199 <header><h4>CSS</h4></header>
200<pre class="sample">
201.container-invalid {
202 width: 100px;
203 height: auto;
204 /* height: 150px; The height of the parent element is not specified */
205}
206
207.container-invalid img {
208 object-fit: cover;
209}
210</pre>
211 <header><h4>HTML</h4></header>
212<pre>
213<div class="background-image-invalid">
214 <img src="example.jpg" alt="Example Image">
215</div>
216</pre>
217 <header><h4>HTML+CSS</h4></header>
218 <section class="sample-view">
219 <div class="container-invalid">
220 <img src="example.jpg" alt="Example Image">
221 </div>
222 </section>
223 </section>
224 </article>
225 <article>
226 <h3>object-position : one value</h3>
227 <section>
228 <header><h4>object-position: center;</h4></header>
229 <section class="sample-view" style="width: 550px; height: 250px;">
230 <img class="object-position-center" src="example.jpg" alt="Example Image">
231 </section>
232 <header><h4>object-position: left;</h4></header>
233 <section class="sample-view" style="width: 550px; height: 250px;">
234 <img class="object-position-left" src="example.jpg" alt="Example Image">
235 </section>
236 <header><h4>object-position: 70%;</h4></header>
237 <section class="sample-view" style="width: 550px; height: 250px;">
238 <img class="object-position-70" src="example.jpg" alt="Example Image">
239 </section>
240 <header><h4>object-position: 50px;</h4></header>
241 <section class="sample-view" style="width: 550px; height: 250px;">
242 <img class="object-position-50px" src="example.jpg" alt="Example Image">
243 </section>
244 </section>
245 </article>
246 <article>
247 <h3>object-position : two values</h3>
248 <section>
249 <header><h4>object-position: center center</h4></header>
250 <section class="sample-view" style="width: 550px; height: 250px;">
251 <img class="object-position-center-center" src="example.jpg" alt="Example Image">
252 </section>
253 <header><h4>object-position: right top</h4></header>
254 <section class="sample-view" style="width: 550px; height: 250px;">
255 <img class="object-position-right-top" src="example.jpg" alt="Example Image">
256 </section>
257 <header><h4>object-position: 75% 20%</h4></header>
258 <section class="sample-view" style="width: 550px; height: 250px;">
259 <img class="object-position-75-20" src="example.jpg" alt="Example Image">
260 </section>
261 <header><h4>object-position: 50px 25px</h4></header>
262 <section class="sample-view" style="width: 550px; height: 250px;">
263 <img class="object-position-50px-25px" src="example.jpg" alt="Example Image">
264 </section>
265 </section>
266 </article>
267 <article>
268 <h3>object-position: four values</h3>
269 <section>
270 <header><h4>object-position: left 10px top 20px</h4></header>
271 <section class="sample-view" style="width: 550px; height: 250px;">
272 <img class="object-position-left-10-top-20" src="example.jpg" alt="Example Image">
273 </section>
274 <header><h4>object-position: right 15% bottom 30px</h4></header>
275 <section class="sample-view" style="width: 550px; height: 250px;">
276 <img class="object-position-right-15-bottom-30" src="example.jpg" alt="Example Image">
277 </section>
278 </section>
279 </article>
280 <article>
281 <h3>object-position: mixed values</h3>
282 <section>
283 <header><h4>object-position: right 50%</h4></header>
284 <section class="sample-view" style="width: 550px; height: 250px;">
285 <img class="object-position-right-50" src="example.jpg" alt="Example Image">
286 </section>
287 </section>
288 </article>
289 <article>
290 <h3>object-position and object-fit</h3>
291 <section>
292 <header><h4>object-fit: cover; object-position: right bottom;</h4></header>
293 <section class="sample-view" style="width: 550px; height: 250px;">
294 <img class="object-position-right-bottom" src="example.jpg" alt="Example Image">
295 </section>
296 <header><h4>object-fit: contain; object-position: right;</h4></header>
297 <section class="sample-view" style="width: 550px; height: 250px;">
298 <img class="object-position-right" src="example.jpg" alt="Example Image">
299 </section>
300 </section>
301 </article>
302 </main>
303</body>
304</html>
객체 맞춤 관련
object-fit
속성
CSS object-fit
속성은 주로 이미지, 비디오 및 기타 콘텐츠가 컨테이너 요소 내에 맞는 방식을 제어하는 데 사용됩니다. 이 속성을 사용하면 지정된 요소를 유연하게 크기를 조정하고 미적으로 배치하는 방법을 설정할 수 있습니다. 이미지나 비디오가 부모 요소 안에 맞지 않을 경우 자르거나 크기를 조정하여 디스플레이를 조정하는 데 매우 유용합니다.
object-fit
값들
fill
(기본값)
1img {
2 object-fit: fill;
3}
fill
은 기본값으로, 이미지나 비디오가 부모 요소의 너비와 높이에 완전히 맞추기 위해 강제로 늘어납니다. 이 경우 원래 비율은 무시되며 이미지가 왜곡될 수 있습니다.
- 부모 요소의 크기에 완벽히 맞춥니다.
- 비율이 무시되기 때문에 이미지나 비디오가 세로 또는 가로로 늘어날 수 있습니다.
contain
1img {
2 object-fit: contain;
3}
contain
은 비율을 유지하면서 부모 요소 내에 콘텐츠가 완전히 맞도록 크기를 조정합니다. 부모 요소의 크기를 초과하지 않으며, 세로 또는 가로 방향에 여백이 생길 수 있습니다.
- 전체 콘텐츠가 표시됩니다.
- 원래 비율을 유지하기 위해 패딩(레터박스 또는 필러박스 효과)이 나타날 수 있습니다.
cover
1img {
2 object-fit: cover;
3}
cover
는 비율을 유지하면서 콘텐츠가 부모 요소를 완전히 덮도록 크기를 조정합니다. 부모 요소에 맞추기 위해 일부 콘텐츠가 잘릴 수 있으나, 전체 콘텐츠가 컨테이너를 채워 빈 공간이 발생하지 않습니다.
- 콘텐츠가 부모 요소를 완전히 덮습니다.
- 원본 가로세로 비율을 유지하면서 잘릴 수 있습니다.
- 주로 배경 이미지나 히어로 섹션 이미지에 사용됩니다.
none
1img {
2 object-fit: none;
3}
none
은 이미지나 비디오를 원래 크기와 가로세로 비율로 표시합니다. 부모 요소의 크기에 맞게 조정되지 않으며, 필요하면 콘텐츠가 넘칩니다.
- 콘텐츠의 원래 크기가 유지됩니다.
- 콘텐츠를 부모 요소에 맞게 조정하지 않습니다.
scale-down
1img {
2 object-fit: scale-down;
3}
scale-down
은 none
과 contain
동작을 결합한 값입니다. 콘텐츠가 부모 요소에 맞으면 none
처럼 동작하지만, 콘텐츠가 너무 크면 contain
동작을 적용하여 크기를 줄여 맞춥니다.
- 콘텐츠가 부모 요소보다 크면 크기가 축소됩니다.
- 콘텐츠가 더 작으면 원래 크기로 표시됩니다.
사용 예시
배경 이미지를 부모 요소에 맞추기
예를 들어, 반응형 디자인을 염두에 두고 이미지를 화면 전체에 표시하려면 cover
를 사용하는 것이 일반적입니다. 다음 코드는 이미지가 잘리더라도 부모 요소에 딱 맞게 조정하는 예제입니다.
1<div class="background-image">
2 <img src="example.jpg" alt="Example Image">
3</div>
4
5<style>
6.background-image img {
7 width: 100%;
8 height: 100%;
9 object-fit: cover;
10}
11</style>
이 경우, 이미지는 부모 요소를 완전히 덮으며, 어떤 화면 크기에서도 아름다운 레이아웃을 유지합니다. 가로세로 비율이 다를 경우 이미지 일부가 보이지 않을 수 있지만, 디자인에 적합한 프레젠테이션을 구현합니다.
가로세로 비율을 유지하면서 이미지 표시
다음으로, 이미지의 가로세로 비율을 유지하면서 전체 이미지를 부모 요소에 맞추려면 contain
이 적합합니다.
1<div class="image-container">
2 <img src="example.jpg" alt="Example Image">
3</div>
4
5<style>
6.image-container img {
7 width: 100%;
8 height: 100%;
9 object-fit: contain;
10}
11</style>
이 예제에서는 이미지가 컨테이너에 맞도록 축소되어 약간의 여백이 생기지만, 원본 이미지가 왜곡 없이 표시됩니다.
일반적인 실수
object-fit
을 사용할 때 일반적으로 발생하는 실수는 부모 요소의 크기를 설정하지 않는 것입니다. object-fit
은 콘텐츠를 부모 요소에 맞추는 속성이므로, 부모 요소의 크기가 명확하지 않으면 기대한 효과를 얻을 수 없습니다. 부모 요소에 width
나 height
가 적절히 설정되어 있는지 확인하십시오.
예를 들어, 아래 코드에서 부모 요소의 높이가 지정되지 않아 object-fit
이 의도한 대로 동작하지 않습니다.
1<div class="image-container">
2 <img src="example.jpg" alt="Example Image">
3</div>
4
5<style>
6.image-container {
7 width: 100%;
8 /* height: 150px; the parent element's height is not specified */
9}
10
11.image-container img {
12 object-fit: cover;
13}
14</style>
이 경우, 부모 요소의 높이를 지정하거나 flexbox나 grid 레이아웃을 사용하여 전체 레이아웃을 조정해야 합니다.
object-fit
을 사용해야 하는 상황
object-fit
은 특히 다양한 화면 크기에 맞춘 반응형 디자인과 웹사이트에서 중요합니다. 예를 들어, 다음과 같은 경우에 유용합니다:.
- 이미지나 비디오를 다양한 기기에서 올바르게 표시하려는 경우
- 배경 이미지와 같은 것을 전체 화면으로 표시하되 잘림을 허용하려는 경우
- 이미지의 비율을 유지한 채 부모 요소에 맞추려는 경우
- 갤러리와 같은 곳에서 이미지 크기를 일관되게 표시하려는 경우
이러한 상황에서 object-fit
을 사용하면 디자인 일관성을 유지하고 사용자 경험을 향상시킬 수 있습니다.
요약
object-fit
은 웹 페이지에서 이미지 및 비디오가 부모 요소와의 상대적 위치에서 어떻게 표시되는지를 유연하게 제어할 수 있게 해주는 강력한 CSS 속성입니다. fill
, contain
, cover
, none
, scale-down
과 같은 다양한 값을 사용하여 다양한 디자인 요구 사항을 충족할 수 있습니다. 이를 올바르게 이해하고 적절히 적용하면 시각적으로 매력적인 레이아웃을 만들 수 있습니다.
object-position
속성
CSS object-position
속성은 이미지나 비디오 같은 컨텐츠를 요소 내에서의 위치를 제어하는 데 사용됩니다. object-position
을 사용하면 컨텐츠를 특정 지점에 배치하여 레이아웃에 맞는 시각적 효과를 제공할 수 있습니다.
object-position
개요
object-position
은 주로 <img>
태그와 <video>
태그와 같은 미디어 요소에 적용됩니다. 이 속성은 미디어가 컨테이너 내에서 표시되는 위치를 제어합니다. object-fit
은 종종 object-position
과 함께 사용되며, object-fit
은 이미지나 비디오가 프레임 크기에 맞는 방식을 설정하고, object-position
은 콘텐츠의 표시 위치를 조정합니다.
값 지정 방법
object-position
은 하나, 두 개, 또는 네 개의 값으로 위치를 지정할 수 있습니다. 하지만 네 개의 값을 지정하는 것은 이전 브라우저에서는 지원되지 않을 수 있습니다.
값 하나 지정하기
값을 하나만 지정하면, 이것은 가로(X축) 위치로 해석되고, 세로(Y축) 위치는 자동으로 center
로 설정됩니다.
object-position: left;
로 지정하면, 오브젝트가 왼쪽에 배치됩니다.object-position: 70%;
로 지정하면, 오브젝트가 가로로 70% 위치에 오고, 세로 위치는 가운데가 됩니다.object-position: 50px;
로 지정하면, 오브젝트가 왼쪽에서 50px 떨어진 곳에 배치되고 세로로는 가운데에 위치합니다.
값 두 개 지정하기
두 개의 값을 지정하면 가로(X축)와 세로(Y축) 위치를 각각 설정할 수 있습니다.
object-position: center center;
로 지정하면 오브젝트가 정중앙에 배치됩니다.object-position: right top;
로 지정하면 오브젝트가 오른쪽 위에 배치됩니다.object-position: 75% 20%;
로 지정하면 가로로 75%, 세로로 20% 위치에 오브젝트가 배치됩니다.object-position: 50px 25px;
로 지정하면, 오브젝트가 왼쪽에서 50px, 위에서 25px 떨어진 곳에 위치합니다.
값 네 개 지정하기
네 개의 값을 지정함으로써 left
나 top
과 같은 키워드와 오프셋을 함께 사용하여 더 세밀하게 위치를 조정할 수 있습니다.
1object-position: <horizontal-keyword> <horizontal-offset> <vertical-keyword> <vertical-offset>;
object-position: left 10px top 20px;
로 지정하면, 오브젝트가 왼쪽에서 10px, 위쪽에서 20px 떨어진 곳에 위치합니다.object-position: right 15% bottom 30px;
로 지정하면, 오브젝트가 오른쪽에서 15%, 아래에서 30px 떨어진 곳에 위치합니다.
값 지정 형식
object-position
은 다음과 같은 값 형식을 사용할 수 있습니다.
top
,right
,bottom
,left
,center
와 같은 키워드를 지정할 수 있습니다.50%
와 같은 퍼센트 값 또는px
와 같은 단위를 지정할 수도 있습니다.
예를 들어, 다음과 같이 사용할 수 있습니다.
1img {
2 object-position: 50% 50%; /* Center the image */
3}
4
5video {
6 object-position: left top; /* Align to the top left */
7}
키워드와 백분율의 조합
키워드와 백분율을 혼합하여 사용하는 것도 가능합니다.
1img {
2 object-position: right 50%;
3}
- 이 예제에서는 이미지를 오른쪽 끝에 배치하고 수직적으로 50% 가운데에 정렬시킵니다. 이 조합은 유연한 위치 조정이 필요할 때 매우 유용합니다.
object-fit
과의 조합
object-position
은 보통 object-fit
과 함께 사용됩니다. object-fit
은 이미지나 비디오가 프레임에 따라 어떻게 크기가 조정되는지를 결정합니다. 예를 들어, object-fit: cover
를 사용하면 이미지를 프레임에 맞게 잘라내고 지정된 object-position
에 따라 배치합니다.
1img {
2 object-fit: cover;
3 object-position: right bottom;
4}
- 이 예제에서는 이미지가 전체 컨테이너에 걸쳐 확장되며, 콘텐츠의 오른쪽 하단 부분을 표시합니다.
object-fit
값과 결합된 예제
object-fit
에는 다음 값을 지정할 수 있습니다.
fill
: 이미지가 왜곡될 수 있지만 전체 컨테이너에 맞게 표시됩니다.contain
: 비율을 유지하면서 전체 이미지가 표시됩니다.cover
: 컨테이너에 맞게 잘리며, 비율은 유지됩니다.none
: 이미지가 원래 크기로 표시됩니다.
예를 들어 object-fit: contain
을 사용하면 이미지를 전체 표시하고, object-position
에 따라 위치를 지정합니다.
1img {
2 object-fit: contain;
3 object-position: right;
4}
이 설정으로 이미지는 컨테이너 오른쪽에 배치되고 전체가 표시됩니다.
고급 사용법
object-position
은 단순한 위치 조정뿐만 아니라 시각적 효과를 향상시키는 다양한 상황에서 사용할 수 있습니다.
이미지의 일부 강조
전체 이미지를 표시하는 대신 이미지의 특정 부분에 초점을 맞추고 싶을 때 유용합니다. object-fit
을 cover
로 설정하고 object-position
을 사용하여 특정 영역에 초점을 맞춥니다.
1img {
2 object-fit: cover;
3 object-position: 25% 75%;
4}
- 이 예제에서는 이미지의 왼쪽 하단 부분을 표시하여 중요한 부분을 강조합니다.
비디오를 배경으로 사용할 때
비디오를 배경으로 사용할 때 object-position
이 유용하게 사용됩니다. 페이지 디자인의 일관성을 유지하기 위해 비디오를 특정 위치에 배치할 수 있습니다.
1video {
2 object-fit: cover;
3 object-position: center top;
4}
- 이 설정에서는 비디오가 전체 페이지에 맞게 조정되고 상단 중앙에 배치됩니다.
자주 발생하는 문제와 해결 방법
object-position
을 사용할 때 몇 가지 문제가 발생할 수 있습니다. 여기에서는 일반적인 문제와 그 해결 방법을 소개합니다.
이미지의 일부가 잘립니다
object-fit: cover
를 사용할 경우, 이미지는 컨테이너에 맞추기 위해 잘릴 수 있습니다. 이 경우, object-position
으로 표시 위치를 조정하여 중요한 부분이 표시되도록 할 수 있습니다.
1img {
2 object-fit: cover;
3 object-position: center;
4}
이미지 위치가 예상과 다릅니다
퍼센트 지정이 항상 직관적이지는 않을 수 있습니다. 컨테이너의 크기와 비율을 고려하고 다양한 값을 실험해보는 것이 필요합니다. 개발자 도구를 사용하여 실시간으로 조정하는 것이 효과적입니다.
요약
object-position
은 미디어 요소의 위치를 자유롭게 설정할 수 있는 강력한 도구입니다. object-fit
과 결합하여 디자인에 맞는 유연한 레이아웃을 구현할 수 있습니다. 다양한 상황에서 시각적 효과를 극대화하기 위해 키워드, 퍼센트, 길이 단위를 숙지하십시오.
위의 기사를 보면서 Visual Studio Code를 사용해 우리 유튜브 채널에서 함께 따라할 수 있습니다. 유튜브 채널도 확인해 주세요.