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

개발

최종 수정일:

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") 같은 방식으로 짰는데, 이번엔 훨씬 간결하게 바꿔봤습니다.

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

Report an issue
Marshall Ku