👩🏼💻 <img> 요소 / background / gradient / 이미지 배치 및 반복
<img> 태그 기본 사용법<img src="coffee.jpg" alt="커피 이미지">
alt: 이미지 로딩 실패 시 대체 텍스트width, height 속성으로 크기 지정 가능vertical-align 속성img {
vertical-align: middle;
}
top, middle, bottom 등object-fit 속성img {
width: 200px;
height: 200px;
object-fit: cover;
}
| 값 | 설명 |
|---|---|
fill | 이미지 비율 무시하고 채움 |
contain | 비율 유지하며 전체 표시 |
cover | 비율 유지하며 요소 채움 |
none | 원본 크기 유지 |
scale-down | none 또는 contain 중 작은 값 |
background-imagediv {
background-image: url("coffee.jpg");
}
background-repeatbackground-repeat: repeat; /* 기본값 */
background-repeat: no-repeat; /* 반복 없음 */
background-repeat: repeat-x; /* 수평 반복 */
background-repeat: repeat-y; /* 수직 반복 */
background-positionbackground-position: center center;
background-position: right bottom;
background-position: 50% 100%;
background-attachmentbackground-attachment: scroll; /* 기본값 */
background-attachment: fixed; /* 배경 고정 */
background-sizebackground-size: cover; /* 요소를 꽉 채움 (비율 유지) */
background-size: contain; /* 요소에 맞춤 (전체 보임) */
background-size: 100% 100%; /* 강제 비율 조절 */
linear-gradientbackground: linear-gradient(to right, red, blue);
to right, to bottom, to left top 등 방향 설정 가능
radial-gradientbackground: radial-gradient(circle, orange, purple);
circle, ellipse 형태 지정 가능
<img>는 콘텐츠 이미지, background-image는 레이아웃 배경용object-fit은 이미지 비율 조절에 유용background 속성은 반복, 위치, 크기, 고정 등 다양한 조합이 중요