주특기 주차를 맞이하고 어느덧 금요일을 맞이했다. 오늘은 이전에 공부했던 모바일 우선 반응형 웹의 또다른 부분에 대해 조사해보기로 했다.
css 단위
◻ 소개
◻ 사용법
.wrapper1 {
font-size: 2rem;
}
.wrapper1 .heading {
font-size: 3em;
}
.wrapper1 .content {
font-size: 1em;
}
.wrapper2 {
font-size: 3rem;
}
.wrapper2 .heading {
font-size: 2em;
}
.wrapper2 .content {
font-size: 0.5em;
}
◻ 주의사항
// 혼돈의 카오스
html {
font-size: 100%
}
body {
font-size: 1em;
}
.article {
font-size: 1.5em;
}
.article .wrapper {
font-size: 2em;
}
.article .wrapper .heading {
font-size: 3em;
}
.article .wrapper .content {
font-size: 1em;
}
✔ 해결 방법
◻ 소개
◻ 사용법
◻ 주의사항
반응형 콘텐츠
◻ img vs background-image
이미지가 사용자의 페이지에 대한 이해를 돕는다면 img를 사용하고, 오로지 디자인이나 배경 이미지로 사용할 목적이라면 background-image를 사용함.
img vs background-image__stack overflow
안티 패턴으로서의 CSS background-image 속성__velog
html의 img와 css의 background-image 각각 ppi와 사이즈를 고려한 반응형 이미지를 만들 수 있는 방법 ▶ developers.google.com: Responsive images
◻ width, height
🙌 상대 크기를 사용할 것.
ex) 예시로서의 html 구조
<div class="container">
<img src="lion image from unsplash" alt="lion-father and baby">
</div>
.containing-block {
width: 50%;
height: 100vh;
background-color: red;
}
.containing-block img {
width: 100%;
}
.containing-block {
width: 100%;
height: 100vh;
background-color: red;
}
.containing-block img {
width: 100%;
height: auto;
}
아래 사진처럼 기존 이미지의 크기가 컨테이닝 블록을 벗어날 때가 있음.
.containing-block {
width: 50%;
height: 100vh;
background-color: red;
}
이럴 땐 max-width: 100%, height: auto를 주면 컨테이닝 블록의 width에 맞게 줄어들면서 종횡비를 유지함.
.containing-block {
width: 50%;
height: 100vh;
background-color: red;
}
.containing-block img {
max-width: 100%;
height: auto;
}
벗어난 부분만 자르고 싶다면 부모 요소에 overflow:hidden을 줄 수도 있음.
.containing-block {
width: 50%;
height: 100vh;
background-color: red;
overflow: hidden;
}
.containing-block {
width: 50%;
height: 100vh;
background-color: red;
}
.containing-block img {
max-width: 300px;
max-height: 300px;
}
위에 더해 이미지가 반응형이길 원한다면 상대적 단위값(%, vw 등)을 가지는 width를 img에 선언하여 max-width와 함께 활용할 수 있음.
.containing-block {
width: 50%;
height: 100vh;
background-color: red;
}
.containing-block img {
max-width: 300px;
max-height: 300px;
width: 40%;
}
참고: 100% width or height while keeping aspect ratio?__stack overflow
◻ 해상도 전환
출처: howtogeek-how to automatically size pictures in powerpoint
▶참고
cloudfour: Responsive Images 101, Part 4: Srcset Width Descriptors
developers.google.com: Responsive images
// w
<img src="400.png"
sizes="(min-width: 600px) 25vw, (min-width: 500px) 50vw, 100vw"
srcset="100.png 100w, 200.png 200w, 400.png 400w,
800.png 800w, 1600.png 1600w" alt="an example image">
// x
<img src="photo.png" srcset="photo@2x.png 2x, photo@3x.png 3x, ...">
따라서 크기가 정해진 이미지에 한해 x descriptor를 사용하는 것이 권장되고, 나머지 반응형에서는 w descriptor 사용이 권장됨.
srcset과 sizes가 지원되지 않는 브라우저에서는 src 내부의 이미지가 사용되기에 src에 1x의 이미지를 잘 적용하는 것은 중요함.
◻ 아트 디렉션
기기의 특성(화면 크기, 픽셀 밀도)에 따라 특정 부분을 자르거나 강조해서 보여주고 싶은 문제.
같은 이미지 다른 부분.
▶참고
cloudfour: Responsive Images 101, Part 6: Picture Element
MDN: 반응형 이미지
<picture>
<source media="(min-width: 800px)" srcset="head.jpg, head-2x.jpg 2x">
<source media="(min-width: 450px)" srcset="head-small.jpg, head-small-2x.jpg 2x">
<img src="head-fb.jpg" srcset="head-fb-2x.jpg 2x" alt="a head carved out of wood">
</picture>
◻ video
video {
width: 100%;
max-height: 100%;
height: 100%;
}
◻ iframe
// html
<div class="videoWrapper">
<iframe width="560" height="349" src="https://www.youtube.com/embed/tgbNymZ7vqY" frameborder="0" allowfullscreen></iframe>
</div>
// css
.videoWrapper {
position: relative;
padding-bottom: 56.25%; /* 16:9 Aspect Ratio (divide 9 by 16 = 0.5625) */
height: 0;
}
.videoWrapper iframe {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
혹은 이렇게도 사용가능함.
<div class="videoWrapper" style="--aspect-ratio: 3 / 4;">
<iframe ...>
</div>
.videoWrapper {
...
/* falls back to 16/9, but otherwise uses ratio from HTML */
padding-bottom: calc(var(--aspect-ratio, .5625) * 100%);
}
▶참고
css tricks: fluid width video
MDN: var()
MDN: css custom properties
유동적 레이아웃
◻ 개념
1차원에서 강력한 공간 배분 및 정렬 기능을 보유한 css3의 새로운 레이아웃 기능.
◻ 특징
◻ 참조
MDN: flexbox의 기본 개념
heropy님 블로그: css flex 완벽 가이드
네이버 D2: flexbox로 만들 수 있는 10가지 레이아웃
◻ 개념
2차원의 레이아웃 시스템으로 수평선과 수직선이 교차해서 이루어지며, 각각 가로행과 세로열을 정의함.
◻ 특징
◻ 참조
MDN: CSS 그리드 레이아웃
MDN: Basic concepts of grid layout
heropy님 블로그: css grid 완벽 가이드
Box 정렬 가이드 by 야무