Click here to Skip to main content
13,296,407 members (47,181 online)
Click here to Skip to main content
Add your own
alternative version

Stats

68.9K views
8 bookmarked
Posted 9 Sep 2011

Vertical Stretch 100% in HTML5/CSS3

, 3 Oct 2011
Rate this:
Please Sign up or sign in to vote.
Make DIV element stretch vertically 100%. Works in all HTML5-compatible browsers
In order to stretch DIV element vertically to fit the entire screen (i.e. browser's viewable area), it's not enough just to set its CSS property height:100%, it also requires to set CSS properties of <html> and <body> elements as shown in Listing 1 below:

Listing 1: Vertical stretch using CSS position:relative
<!DOCTYPE html>
<html>
  <head>
    <title>VERTICAL STRETCH | DEMO</title>
    <style type ="text/css">
      html {height:100%;}
      body {margin:0; padding:0; height:100%;}
      div#container {background-color:green; height:100%; width:60%; margin: 0 auto 0 auto;}
    </style>
  </head>
 
  <body>
    <div id="container">
	DEMONSTRATION OF DIV HEIGHT:100%
	THIS GREEN RECTANGLE SHOULD STRETCH VERTICALLY 100%
     </div>
  </body>
</html>


Listing 1 snippet implies the default setting of: position:relative. Another alternative is to use position:absolute and set CSS properties top and bottom to 0 as shown in the following Listing 2:

Listing 2: Vertical stretch using CSS position:absolute
<!DOCTYPE html>
<html>
  <head>
    <title>VERTICAL STRETCH | DEMO</title>
    <style type ="text/css">
      div#container {background-color:green; position:absolute; top:0; bottom:0; width:60%; margin-left:20%; }
    </style>
  </head>
 
  <body>
    <div id="container">
	DEMONSTRATION OF DIV HEIGHT:100%
	THIS GREEN RECTANGLE SHOULD STRETCH VERTICALLY 100%
     </div>
  </body>
</html>

Similar results could be achieved by setting CSS position:fixed in regards to DIV element.

In both cases, make sure that the property overflow-y is set correspondingly to the layout specs (either auto, or hidden, visible, scroll, etc.) to ensure proper content rendering (refer to the demo sample at: CSS overflow samples[^]).

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

DrABELL
Founder Infosoft International Inc
United States United States
Dr. Alexander Bell is a seasoned full-stack Software Engineer (Win/Web/Mobile). He holds PhD in EE/IT, authored 37 inventions and published 300+ technical articles. Currently focused on Java/Javascript, .NET/C#, Android/Mobile, SQL, 'Big Data', Machine Learning, AI, IoT. Alex participated in App Innovation Contests (AIC 2102/2013) with multiple winning submissions. Portfolio samples:

  1. Real-time NY Bus Tracking Web App (IoT)
  2. Android Fraction Calculator "Fractal MK-12"
  3. Android Prime Factorization Calculator 18-digit)
  4. Semaphon™ semantic phone num-to-text converter
  5. Educational Web Portal
  6. Free Online NY Payroll Tax Calculator
  7. WebTV powered by YouTube Player powered by .NET API (#1 on Google)
  8. Engineering Calculator VOLTA
  9. Big Data: Facebook and YouTube
  10. NY City Trivia Quiz
  11. Publications by A. Bell

You may also be interested in...

Comments and Discussions

 
GeneralExcellent tip. Works with iframes too. Pin
cmschick14-Sep-11 18:52
membercmschick14-Sep-11 18:52 
GeneralRe: Yes, it should. Thanks for your input! Kind regards - AB Pin
DrABELL15-Sep-11 5:23
memberDrABELL15-Sep-11 5:23 
GeneralReason for my vote of 5 short, sweet, fun and pretty cool. Pin
Jenny Quinn13-Sep-11 4:13
memberJenny Quinn13-Sep-11 4:13 
GeneralRe: Thanks a bunch! :) Pin
DrABELL13-Sep-11 4:36
memberDrABELL13-Sep-11 4:36 
GeneralReason for my vote of 5 Seems like a good way! Pin
Francis Boudreau13-Sep-11 3:33
memberFrancis Boudreau13-Sep-11 3:33 
Reason for my vote of 5
Seems like a good way!
GeneralRe: Thanks! Pin
DrABELL13-Sep-11 3:53
memberDrABELL13-Sep-11 3:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171207.1 | Last Updated 3 Oct 2011
Article Copyright 2011 by DrABELL
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid