1
2

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 3 years have passed since last update.

デザインカンプ通りの比率でマルチデバイス対応する方法

Posted at

デザインカンプ(幅640px)にボタン(幅468px)があるとします。
このボタンには上部にマージン(60px)があります。

このデザインカンプの比率を保ったまま、各種デバイスで表示させたいときの計算方法です。

640px以上では固定幅、640px 未満では比率を保ったまま縮尺させるには下記のようにします。

desktop-first.css
.button {
    width: 468px;
    margin-top: 60px
 }
@media only screen and (max-width: 639px) {
  .button {
    width: calc(468vw / 640 * 100);
    margin-top: calc(60vw / 640 * 100);
  }
}

割られる数の 468 のところはデザインカンプのボタンの画像幅、割る数の 640 のところはデザインカンプ全体の幅です。

CSS をモバイルファーストに書くのであればこうですね。

mobile-first.css
.button {
    width: calc(468vw / 640 * 100);
    margin-top: calc(60vw / 640 * 100);
 }
@media screen and (min-width:640px) {
  .button {
    width: 468px;
    margin-top: 60px
  }
}

この単位と計算方法は padding 、font-size、height、top などの指定にも使えます。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?