Align vertically using CSS 3

后端 未结 8 1194
忘了有多久
忘了有多久 2020-11-30 19:21

With CSS 3, are there any way to vertically align an block element? Do you have an example? Thank you.

8条回答
  •  再見小時候
    2020-11-30 19:31

    Was looking at this problem recently, and tried:

    HTML:

    
        

    CSS:

    #my-div {               
        position: absolute;
        height: 100px;
        width: 100px;    
        left: 50%;
        top: 50%;
        background: red;
        transform: translate(-50%, -50%);    
        -webkit-transform: translate(-50%, -50%);
        -moz-transform: translate(-50%, -50%);
        -ms-transform: translate(-50%, -50%);
    }
    

    Here's the Fiddle:

    http://jsfiddle.net/sTcp9/6/

    It even works when using "width/height: auto", in the place of fixed dimensions. Tested on the latest versions on Firefox, Chrome, and IE (* gasp *).

提交回复
热议问题