LoginSignup
2
2

More than 1 year has passed since last update.

【HTML/CSS】画像の表示位置(上下左右中央)を完璧に制御する

Posted at

概要

画像の表示位置をコントロールするCSSの実装について、雑に記載します。
(今後の自分の備忘録も兼ねて。)

HTML/CSS

<div class="contents">
  <img class="image" width="128" height="128" src="image.png" />
</div>
.contents {
  width: 100%;
  height: 100vh;
  background-color: darkgray;
}

.image {
  /* ここに画像の表示位置をコントロールするCSSを書く */
}

CSSが何も当たっていない時の表示はこんな感じで、画像が左上に表示されます。
1.PNG

左中央

.image {
  top: 0;
  bottom: 0;
  margin: auto 0;
  position: absolute;
}

2.PNG

左下

.image {
  bottom: 0;
  position: absolute;
}

3.PNG

下中央

.image {
  bottom: 0;
  left: 0;
  right: 0;
  margin: 0 auto;
  position: absolute;
}

4.PNG

右下

.image {
  bottom: 0;
  right: 0;
  position: absolute;
}

5.PNG

右中央

.image {
  top: 0;
  bottom: 0;
  right: 0;
  margin: auto 0;
  position: absolute;
}

6.PNG

右上

.image {
  right: 0;
  position: absolute;
}

7.PNG

上中央

.image {
  left: 0;
  right: 0;
  margin: 0 auto;
  position: absolute;
}

8.PNG

画面中央

.image {
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  margin: auto;
  position: absolute;
}

9.PNG

2
2
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
2
2