배너 이미지

반응형 유튜브 비디오 만들기

최종 수정일 : (11개월 전)

데모 페이지
Github

위 비디오처럼 CSS로 요소를 특정 비를 유지하며 크기가 줄어들도록 하실 수 있습니다.

.resvid {
position: relative;
width: 100%;
padding-bottom: 56.25%;
margin: auto
}

.resvid iframe {
position: absolute;
width: 100%;
height: 100%
}

.resvid.alt {
width: 35%;
padding-bottom: 62.22%
}

.resvid.square {
width: 70%;
padding-bottom: 70%
}

.resvid.old {
width: 80%;
padding-bottom: 60%
}

@media screen and (max-width:1000px) {
.resvid.alt {
width: 50%;
padding-bottom: 88.89%
}

.resvid.old {
padding-bottom: 75%
}
}

@media screen and (max-width:600px) {
.resvid.alt {
width: 100%;
padding-bottom: 177.78%
}

.resvid.square {
width: 100%;
padding-bottom: 100%
}
}

위 CSS에서 제일 중요한 건 width와 padding-bottom (top이어도 무관)의 값입니다.
width : padding-bottom = 16 : 9처럼 원하는 값을 적절히 넣으셔서 width와 padding 값을 결정하셔야 합니다.

가로가 더 길 땐 width를 100으로 놓는 걸 추천하지만, 세로가 더 긴 요소는 .resgvid.alt처럼 width를 작게 해주시는 게 좋습니다.

Array.from(document.querySelectorAll("iframe")).forEach(function (a) {
const r = a.width / a.height,
reswrapper = document.createElement("div");

reswrapper.classList.add(`${(16/9 === r || 9/16 === r || 4/3 === r || 1 === r) && "resvid"}`, `${9 / 16 === r ? "alt" : 4 / 3 === r ? "old" : r === 1 && "square"}`),
a.parentElement.insertBefore(reswrapper, a);
reswrapper.appendChild(a)
});

예전엔 조건문을 width / height === 16 / 9면 elem.classList.add("something") 같은 방식으로 짰는데, 이번엔 훨씬 간결하게 바꿔봤습니다.

근데 이게 나은 방법인진 확신이 안 서네요…


profile

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다

주의 : 비밀 댓글 사용 시 수정 기능을 이용할 수 있는 시간이 지나면 작성자도 내용 확인이 불가능합니다.