更新日:2019.04.26
HTML,CSSを使って画像の上に文字やボタンなどを重ねる方法を解説.
テキストテキストテキスト
HTML
<div class="kani">
<img src="kani.jpg">
<p class="btn"><a>ボタン</a></p>
<p class="text">テキストテキストテキスト</p>
</div>
CSS
div{
}
img{
}
.btn{
width: 90px;
text-align: center;
border-radius: 3px;
background-color: #1e90ff;
color: #ffffff;
padding: 3px;
}
.text{
width: 400px;
font-size: 30px;
color: #ffa500;
}
CSS div{ width: 700px; } img{ width: 100%; }
テキストテキストテキスト
CSS div{ width: 700px; position: relative; } img{ width: 100%; } .btn{ width: 90px; text-align: center; border-radius: 3px; background-color: #1e90ff; color: #ffffff; padding: 3px; position: absolute; } .text{ width: 400px; font-size: 30px; color: #ffa500; position: absolute; }
テキストテキストテキスト
例:親要素にposition: relative;無しの場合
top: 50px;でページ全体の最上部から50pxの距離に配置
例:親要素にposition: relative;有りの場合
top: 50px;で親要素の最上部から50pxの距離に配置
上からの配置位置を指定.(例:top: 20px;で上から20pxの距離に配置)
下からの配置位置を指定.(例:bottom: 20px;で下から20pxの距離に配置)
右端からの配置位置を指定.(例:right: 20px;で右端から20pxの距離に配置)
左端からの配置位置を指定.(例:left: 20px;で左端から20pxの距離に配置)
CSS div{ width: 700px; position: relative; } img{ width: 100%; } .btn{ width: 90px; text-align: center; border-radius: 3px; background-color: #1e90ff; color: #ffffff; padding: 3px; position: absolute; bottom: 40px; right: 50px; } .text{ width: 400px; font-size: 30px; color: #ffa500; position: absolute; top: 25px; left: 30px; }
テキストテキストテキスト