方法-画像の上にテキストを配置する
画像の上にテキストを配置する方法を学びます。
画像テキスト
左下
左上
右上
右下
中央揃え
画像にテキストを配置する方法
ステップ1)HTMLを追加します。
例
<div class="container">
<img src="img_snow_wide.jpg" alt="Snow"
style="width:100%;">
<div class="bottom-left">Bottom Left</div>
<div class="top-left">Top Left</div>
<div class="top-right">Top
Right</div>
<div class="bottom-right">Bottom Right</div>
<div class="centered">Centered</div>
</div>
ステップ2)CSSを追加します。
例
/* Container holding the image and the text */
.container {
position: relative;
text-align: center;
color: white;
}
/* Bottom
left text */
.bottom-left {
position: absolute;
bottom: 8px;
left: 16px;
}
/* Top left text */
.top-left {
position:
absolute;
top: 8px;
left: 16px;
}
/* Top right text */
.top-right {
position: absolute;
top: 8px;
right: 16px;
}
/* Bottom right text */
.bottom-right {
position: absolute;
bottom: 8px;
right: 16px;
}
/* Centered text
*/
.centered {
position:
absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
画像のスタイルを設定する方法の詳細については、CSS画像のチュートリアルをご覧ください。
CSSポジショニングの詳細については、 CSS位置チュートリアルをお読みください。