Click here to Skip to main content
12,295,791 members (30,363 online)
Click here to Skip to main content
Add your own
alternative version

Stats

62.4K views
8 bookmarked
Posted

Vertical Stretch 100% in HTML5/CSS3

, 3 Oct 2011 CPOL
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
President Infosoft International Inc
United States United States
Dr. A. Bell is a full-stack software developer (Win/Web/Mobile). He holds PhD in EE/IT, published 300+ articles, authored 37 inventions and is credited for 10+ Enterprise level projects; currently focused on HTML5/CSS3, Javascript/jQuery, .NET/WPF/C#, Android/Angular.js, 'Big Data', AI, IoT. Alex participated in App Innovation Contests (AIC 2102/2013) with multiple winning submissions. Sample projects/pubs:
  1. Real-time NY Bus monitoring app (IoT)
  2. Semaphon™ semantic phone num-to-text converter
  3. Educational Web Portal
  4. Free Online NY Payroll Tax Calculator
  5. WebTV powered by Embedded YouTube Player (Goog #1 YouTube API for ASP.NET)
  6. Top-50 Digital Cameras (powered by iMark-DCAM rating engine)
  7. Pure CSS3 Slide Show
  8. Inflation Calculator
  9. CSS3 Modal Pop-up Dialog
  10. Multilingual Geocoder with Interactive Map
  11. Online Semantic Analyzer (Concordance Calculator)
  12. NY City Job Market and Agency Ratings
  13. Advanced CSS3 Table Formatting

You may also be interested in...

Comments and Discussions

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

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160525.2 | Last Updated 3 Oct 2011
Article Copyright 2011 by DrABELL
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid