2016-04-17

stylesheet:ブロック要素の上下センタリング

参考:
http://ideahacker.net/2015/02/14/9779/



上下センタリングしたいブロック要素に"position: absolute;"、"top: 0;"、"bottom: 0;"、"margin: auto;"を指定する。上下センタリングしたいブロック要素を内包するブロック要素には"position: relative;"または"position: absolute;"指定する必要がある。
(center-wrapperとcenter-V-block以外のクラス指定は体裁を整えるためのもの)
  1. <div class="center-wrapper outerbox-decoration">
  2. <div class="center-V-block innerbox-decoration">
  3. ブロック要素(div要素、skyblueのボックス)に対して<br>
  4. このブロック要素(div要素、このlightgreenのボックス)を<br>上下センタリング
  5. </div>
  6. </div>
  1. .center-wrapper{
  2.     position: relative;
  3. }
  4. .center-V-block{
  5. position: absolute;
  6. top: 0;
  7. bottom: 0;
  8. margin: auto;
  9. }
  10. .innerbox-decoration{
  11.     width: 400px;
  12.     height: 200px;
  13.     background: lightgreen;
  14. }
  15.  
  16. .outerbox-decoration{
  17.     margin: 0 auto;
  18.     width: 600px;
  19.     height: 300px;
  20.     background: skyblue;
  21. }

サンプル


以上