LoginSignup
1
0

More than 3 years have passed since last update.

画像を親divの中央(vertical center)に表示する方法

Last updated at Posted at 2019-01-11

imgタグを親divの真ん中垂直にさせるcss方法

html

before


<!DOCTYPE html>
<html lang="ja">

<head>
    <style>

    </style>
</head>


<div style="border: 1px solid black;width:500px;height:300px">

    <img width="120px" src="benz.jpeg">

</div>

</html>


before.png

after



<!DOCTYPE html>
<html lang="ja">

<head>
    <style>
        .centerLand {
            display: flex;
            justify-content: center;
            align-items: center;
        }
    </style>
</head>


<div class="centerLand" style="border: 1px solid black;width:500px;height:300px">

    <img width="120px" src="benz.jpeg">

</div>

</html>


after.png


   display: flex;
   justify-content: center;
   align-items: center;

上記のcssをimgタグの親divに適応すれば、imgが垂直の真ん中に配置されます。

vertical-alignの使い方


height:指定した値(200px)もしくは指定した割合(100%)
display: table;


display: table-cell;
vertical-align: middle;

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0