How do I vertically center an H1 in a div?

前端 未结 3 707
天命终不由人
天命终不由人 2020-12-23 03:34

First of all, my apologies. I know there are various solutions posted for this issue here, but for the life of me I can\'t get any of them to work.

For a responsive

相关标签:
3条回答
  • 2020-12-23 03:45

    you can achieve vertical aligning with display:table-cell:

    #section1 {
        height: 90%; 
        text-align:center; 
        display:table;
        width:100%;
    }
    
    #section1 h1 {display:table-cell; vertical-align:middle}
    

    Example

    Update - CSS3

    For an alternate way to vertical align, you can use the following css 3 which should be supported in all the latest browsers:

    #section1 {
        height: 90%; 
        width:100%;
        display:flex;
        align-items: center;
        justify-content: center;
    }
    

    Updated fiddle

    0 讨论(0)
  • 2020-12-23 04:07

    You can achieve this with the display property:

    html, body {
        height:100%;
        margin:0;
        padding:0;
    }
    #section1 {
        width:100%; /*full width*/
        min-height:90%;
        text-align:center;
        display:table; /*acts like a table*/
    }
    h1{
        margin:0;
        padding:0;
        vertical-align:middle; /*middle centred*/
        display:table-cell; /*acts like a table cell*/
    }
    

    Demo: http://jsfiddle.net/a3Kns/

    0 讨论(0)
  • 2020-12-23 04:08

    I've had success putting text within span tags and then setting vertical-align: middle on that span. Don't know how cross-browser compliant this is though, I've only tested it in webkit browsers.

    0 讨论(0)
提交回复
热议问题