flexboxで画像と文字の横並びアイテムを作る
はじめに
画像と文字の横並びをいい感じに作りたかったので、順を追って試してみたときのイメージ図を備忘録として残していく。
環境
※以下を使用する。
flexbox
bootstrap 4系
まずはflexboxを使ってみる
style.css
.img-container{
display:flex;/* flexbox */
}
index.html
<div class="img-container">
<img src="img.png">
文言
</div>
デフォルトは横並びみたいです。imgと文言がくっついちゃいます。
水平方向に真ん中にしてみる。
style.css
.img-container{
display:flex;/* flexbox */
justify-content:center; /* 水平方向 */
}
index.html
<div class="img-container">
<img src="img.png">
文言
</div>
文言が真ん中にならないので何とかしなきゃ
垂直方向を真ん中にしてみる
style.css
.img-container{
display:flex;/* flexbox */
justify-content:center; /* 水平方向 */
align-items: center; /* 垂直方向 */
}
index.html
<div class="img-container">
<img src="img.png">
文言
</div>
画像と文字がくっついちゃってる。ぴったしくっついてる。余白明けたい。
余白を開けてみる。(ゴール)
style.css
.img-container{
display:flex;/* flexbox */
justify-content:center; /* 水平方向 */
align-items: center; /* 垂直方向 */
}
index.html
<div class="img-container">
<img class="mr-3" src="img.png">
文言
</div>
bootstrapでmr-3を使ってイメージの右側に余白を開けてみた。
Author And Source
この問題について(flexboxで画像と文字の横並びアイテムを作る), 我々は、より多くの情報をここで見つけました https://qiita.com/sakamotoyuya/items/e161f8f15a63a8788670著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .