SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict
    Join Date
    Feb 2013
    Posts
    243
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Dynamic height for parent when verically centering with table/table-cell?

    Hi,

    I a couple of areas on a website that covers 100% viewport height, inside these I have a paragraph of text which I want to always be vertically centered.
    Using the table, table-cell approach works fine, IF I put the table-height in pixels. The thing is I don't want to do that.

    Is there a way to solve this?

    Code HTML4Strict:
    <div class="table">
    <p></p>
    </div>

    Code CSS:
    .table{
    display: table;
    height: 100%;
    }
     
    p{
    display: table-cell;
    vertical-align: middle;
    }

  2. #2
    SitePoint Mentor bronze trophy
    ronpat's Avatar
    Join Date
    Jun 2012
    Location
    NJ, USA
    Posts
    2,220
    Mentioned
    51 Post(s)
    Tagged
    2 Thread(s)
    Did you try setting the body and html to height:100%?
    Code:
    body,html {
        height:100%;
    }
    
    .table {
        display:table;
        height:100%;
    }
    p {
        display:table-cell;
        vertical-align:middle;
    }

  3. #3
    SitePoint Addict
    Join Date
    Feb 2013
    Posts
    243
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ronpat View Post
    Did you try setting the body and html to height:100%?
    Code:
    body,html {
        height:100%;
    }
    
    .table {
        display:table;
        height:100%;
    }
    p {
        display:table-cell;
        vertical-align:middle;
    }
    Yes I did, but thanks to your message I found the problem, The .table class had another child which the p-class is child to, so when I put that one to 100% height it worked. thx.

  4. #4
    SitePoint Mentor bronze trophy
    ronpat's Avatar
    Join Date
    Jun 2012
    Location
    NJ, USA
    Posts
    2,220
    Mentioned
    51 Post(s)
    Tagged
    2 Thread(s)
    That was quick. Glad you found it.

    Isolated snippits of code often omit significant pieces of the puzzle. If you can review the link at the bottom of my post, especially the part about providing a working page (starts with doctype, ends with </html>) that demonstrates the problem, it would help us in the future... it would save some guessing.

    Thanks for the feedback.

    Cheers


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •