LoginSignup
7
3

More than 5 years have passed since last update.

【css】単位が異なる値(pxや%など)を使って、高さや幅を指定する方法

Last updated at Posted at 2018-12-15

cssで、単位が違う値を用いて、高さや幅を指定する方法をご紹介します。

イメージは下図の???を求める感じです!
スクリーンショット 2018-12-15 9.56.02.png

calcを使おう!

関数calcを使うと、単位が異なる値同士で計算できます!

height: calc(50% - 90px));

サンプルソース

index.html

<!DOCTYPE html>
<html lang="ja">
  <head>
    <title>sample</title>
    <link rel="stylesheet" href="css/style.css" />
  </head>
  <body>
    <header></header>
    <main></main>
    <div class="box"></div>
    <footer></footer>
  </body>
</html>

css/style.css

html,
body {
  overflow: hidden;
  height: 100%;
}

header {
  height: 50px;
  background-color: pink;
}
main {
  height: 50%;
  background-color: yellowgreen;
}

.box {
  height: calc(50% - 90px);
  background-color: skyblue;
}

footer {
  height: 40px;
  background-color: pink;
}


画面の図がこちら。わかりずらくて、すみませんw
スクリーンショット 2018-12-15 10.13.03.png

ピンク色のヘッダーとフッターはpx指定、そして黄緑色のメインは%指定しています。

目的はdivタグ(boxクラス)の高さをbodyタグ内に収まるようにすること。calcを使って調整できました。

参考になれば幸いです!

参考URL

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