Results 1 to 8 of 8

Thread: Vertical align div to viewport height

  1. #1
    Join Date
    Mar 2011
    Posts
    2,144
    Thanks
    59
    Thanked 116 Times in 113 Posts
    Blog Entries
    4

    Default Vertical align div to viewport height

    Hey all.
    I have a div and a wrapper -

    HTML Code:
    <div class="lTagLineWrapper">
    	<div class="iTagLine">
    
    	</div>
    </div>
    that is positioned absolutely from the top of the page.
    Code:
    .lTagLineWrapper {
    	position:absolute;
    	left:50%;
    	top:40%;
    }
    
    .iTagLine {
    	position:relative;
    	left:-50%;
    	width:600px;
    	text-align:center;
    	font-size:3em;
    	background-color:red;
    }
    to put the iTagLine in the middle of the screen even while zooming.

    Only problem is when you scroll down the page, the div stays in its position on the screen (because of the position:absolute.
    Any way to position it relative to the top of the page without impacting the flow of other elements on the page without using position:absolute;? (or some other fix)

    Thanks,
    keyboard
    Last edited by keyboard; 07-07-2016 at 01:32 PM.

  2. #2
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    3,033
    Thanks
    25
    Thanked 599 Times in 575 Posts
    Blog Entries
    40

    Default

    Hi keebs,

    Do you have an illustration that shows what you'd like to happen as I'm not sure what you're describing. Maybe make a live, editable mockup in JSBin, CodePen or JSFiddle too?
    Focus on Function Web Design
    Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps

  3. #3
    Join Date
    Mar 2011
    Posts
    2,144
    Thanks
    59
    Thanked 116 Times in 113 Posts
    Blog Entries
    4

  4. #4
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    3,033
    Thanks
    25
    Thanked 599 Times in 575 Posts
    Blog Entries
    40

    Default

    Focus on Function Web Design
    Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps

  5. The Following User Says Thank You to Beverleyh For This Useful Post:

    Ripsaw (06-26-2016)

  6. #5
    Join Date
    Sep 2007
    Location
    The Netherlands
    Posts
    1,881
    Thanks
    49
    Thanked 266 Times in 258 Posts
    Blog Entries
    56

    Default

    If you want a solution that doesn't force you to specify the dimensions of the centered element, you could do this:
    Code:
    <body style="background: green; ">
    
    <div style="background: pink; height: 100vh; padding: 15px">
    bla bla<br>bla bla<br>bla bla<br>bla bla<br>bla bla<br>bla bla<br>bla bla<br>bla bla<br>bla bla<br>bla bla<br>bla bla<br>bla bla<br>bla bla<br>bla bla<br>bla bla<br>bla bla<br>bla bla<br>bla bla<br>bla bla<br>bla bla<br>bla bla<br>bla bla<br>bla bla<br>bla bla<br>bla bla<br>bla bla<br>bla bla<br>bla bla<br>bla bla<br>bla bla<br>bla bla<br>bla bla<br>bla bla<br>bla bla<br>bla bla<br>bla bla<br>bla bla<br>bla bla<br>bla bla<br>bla bla<br>bla bla<br>bla bla<br>bla bla<br>bla bla<br>bla bla<br>bla bla<br>bla bla<br>bla bla<br>bla bla<br>bla bla<br>bla bla<br>bla bla<br>bla bla<br>bla bla<br>bla bla<br>bla bla<br>bla bla<br>bla bla<br>bla bla<br>bla bla<br>bla bla<br>bla bla<br>bla bla<br>bla bla<br>bla bla<br>bla bla<br>bla bla<br>bla bla<br>bla bla<br>bla bla<br>bla bla<br>bla bla<br>bla bla<br>bla bla<br>bla bla<br>bla bla<br>bla bla<br>bla bla<br>bla bla<br>bla bla<br>bla bla<br>
    </div>
    
    
    <div id="centered" style="background: red; color: white; padding: 10px; position: absolute">
    The div is vertically and horizontally centered,<br>even without specifications for width and height.<br>The div is vertically and horizontally centered,<br>even without specifications for width and height.
    <script>
    document.getElementById('centered').style.top=window.innerHeight/2-document.getElementById('centered').clientHeight/2+'px'
    document.getElementById('centered').style.left=window.innerWidth/2-document.getElementById('centered').clientWidth/2+'px'
    var windowWidth = window.innerWidth;
    window.onresize=function()
    {
    if(windowWidth != window.innerWidth)
    setTimeout("location.reload()",10); return
    }
    </script>
    </div>
    
    </body>

  7. The Following User Says Thank You to molendijk For This Useful Post:

    keyboard (06-18-2016)

  8. #6
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    3,033
    Thanks
    25
    Thanked 599 Times in 575 Posts
    Blog Entries
    40

    Default

    If you don't want to specify width and height of the centered element, you could also use my "go to" centering method that's noted at the top of the previous blog post - the one using transform:translate http://jsbin.com/jipepojiyi/1/edit?html,css,output

    Another option could be to use a CSS-table http://jsbin.com/nihowuyaca/edit?html,css,output

    There are loads more ways to center with CSS here too https://css-tricks.com/centering-css-complete-guide/
    Last edited by Beverleyh; 06-15-2016 at 02:51 PM.
    Focus on Function Web Design
    Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps

  9. The Following User Says Thank You to Beverleyh For This Useful Post:

    keyboard (06-18-2016)

  10. #7
    Join Date
    Mar 2011
    Posts
    2,144
    Thanks
    59
    Thanked 116 Times in 113 Posts
    Blog Entries
    4

    Default

    Guess who's computer just got fixed!

    Bev I went with the
    Code:
    position:absolute;
    	top:50%; 
    	left:50%; 
    	-webkit-transform:translate(-50%,-50%);
    	-ms-transform:translate(-50%,-50%);
    	transform:translate(-50%,-50%);
    But in my example the div stays in the center of the screen as you scroll.
    In the jsbin is scrolls with the page.


  11. #8
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    3,033
    Thanks
    25
    Thanked 599 Times in 575 Posts
    Blog Entries
    40

    Default

    It might be that you need to 'trap' it in the parent container. position:relative; on the parent element should do that.

    If that doesn't fix it, we'd need a full sample to tinker with.
    Focus on Function Web Design
    Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps

  12. The Following User Says Thank You to Beverleyh For This Useful Post:

    keyboard (07-07-2016)

Similar Threads

  1. Resolved vertical align center with fluid height
    By james438 in forum CSS
    Replies: 18
    Last Post: 09-08-2011, 02:22 PM
  2. Replies: 3
    Last Post: 06-06-2011, 11:51 AM
  3. Replies: 4
    Last Post: 10-04-2007, 10:30 PM
  4. Replies: 10
    Last Post: 07-31-2006, 03:53 AM
  5. Replies: 2
    Last Post: 07-20-2006, 07:25 PM

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
  •