Help us understand the problem. What is going on with this article?

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

More than 1 year has passed since last update.

今回は上下中央に配置したいものの高さと幅が決まっていますが、この方法であれば文章などの不規則な高さのものも上下中央に配置可能です。

HTML

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

CSS

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

positiontop,leftで50%ずつ移動させ、要素の表示位置をtransformプロパティで調整するやり方です。
CSS3が使えず、今回のように要素の高さと幅が決まっている場合は、ネガティブマージンなどで調整しましょう。

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away