CSSの要素が画面の中央にある問題

724 ワード

1、定幅の高い要素はスクリーンウィンドウの水平垂直方向においてである
.box{
    width:150px;
    height:200px;
    position: fixed;
    top:50%;
    left:50%;
    margin-left:-75px;
    margin-top:-100px;
    background:red;
}

2、不定幅の高い要素はスクリーンウィンドウの水平垂直方向に
.box{
    position: fixed; 
    top:0;
    right:0;
    bottom:0; 
    left:0;
    margin:auto;
    background:red;
}

3、不定幅の子要素は親要素の中で水平垂直に中央にある
   :
   {
    position:relative;
}
   {
    position: absolute; 
    top:0;
    right:0;
    bottom:0; 
    left:0;
    margin:auto;
}

   :
   {
    display:table-cell;
    text-align:center;
    vertical-align:middle;
}