2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

transformプロパティを使って要素を上下中央に配置する

Last updated at Posted at 2019-12-19

HTML

index.html
<div class="box>
  <img src="dummy.png" alt="" width="24" height="24" class="img" >
</div>

CSS

app.css

.box {
   position:relative;
   height:300px;
}
.img {
   position: absolute;
   top: 50%;
   left: 50%;
   transform: translate(-50%, -50%);
}

absolute による要素の固定

IMG_4833.jpg

absolute による要素の固定は赤い丸の点
左上を起点としているので、left: 50% Top:50%ではこの位置になる。
これではBox要素
に対して上下中央ではないので、

app.css
transform: translate(-50%, -50%);

IMG_4834.jpg

translate(X方向の距離, Y方向の距離)
translate()関数では、X方向とY方向の距離で2D移動を指定します。 Y方向の距離は省略することができますが、この場合のY方向の距離は0となります。[tx, ty]
translateX(X方向の距離)
translateX()関数では、X方向の距離で移動を指定します。
**`translateY(Y方向の距離)
translateY()関数では、Y方向の距離で移動を指定します。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?