LoginSignup
11
9

More than 5 years have passed since last update.

cssで画面の中央に表示する。

Posted at

メモ。
cssでブラウザのウィンドウの中央に表示する。
仕組みとしては、ボックスを作ってそれをただ中央に表示しているだけ。
背景をそれぞれ設定しているので、コピペすると分かりやすいかも。
背景を全体に設定するときは、cssのbody要素にのみ設定するだけで良いみたい。

html

<div class="centerMiddle">
  ボックスを画面中央に表示する。
</div>

css

html,body{
    hight: 100%;    /*高さを100%に設定*/
}

body{
    margin: 0;
    padding: 0;
    positiion: relative;
    min-width: 600px;   /*中央配置するボックスの横幅*/
    min-height: 400px;  /*中央配置するボックスの縦幅*/
    background-color:#00bfff;
}

.centerMiddle{
    margin: -200px 0 0 -300px;  /*縦横半分をネガティブマージンでずらす*/
    position: absolute;     /*body要素に対して絶対配置*/
    top: 50%;       /*上端を中央に*/
    left: 50%;      /*左端を中央に*/
    width: 600px;   /*横幅*/
    height: 400px;  /*縦幅*/
    background-color:#00ff00;
}

11
9
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
11
9