CSS を使用して要素のアスペクト比の維持方法を学習します。
リサイズした時にも、アスペクト比(4:3、16:9 など)をキープするフレキシブルな要素を作成します:
アスペクト比とは?
要素のアスペクト比は、幅と高さの比例関係を表します。
2 つの一般的なビデオアスペクト比は、4:3(the universal video format of the 20th century)と
16:9 (universal for HD television and European digital television)です。
<div> のようなコンテナ要素を使用し、内部にテキストが必要な場合は、子要素を追加します:
<div class="container">
<div class="text">Some text</div> <!-- If
you want text inside the container -->
</div>
DIV のアスペクト比を維持するために、padding-bottom
にパーセント値を追加します。
次の例は、1:1(高さと幅は常に等しい)のアスペクト比を作成します:
.container {
background-color: red;
width: 100%;
padding-top: 100%; /* 1:1 Aspect
Ratio */
position: relative; /* If you want
text inside of it */
}
/* If you
want text inside of the container */
.text {
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
}
Try it Yourself »
その他のアスペクト比: