The latest approach to creating a layout with a full 100% height using HTML5 and CSS3

After taking a break from coding websites, I've realized that HTML5 and CSS have introduced a lot of new features. I typically design sites with a fixed size header and footer, with the main content area in between. The default page should take up 100% of the window height, expanding the content area. If the content is lengthy, a vertical scrollbar will appear as usual.

<body>
   <table id="main" ...>
      <tr>
         <td id="header-and-content">
            <div id="header">contains logo, nav and has fixed height</div>
            <div id="content">actual content</div>
         </td>
      </tr>
      <tr>
         <td id="footer">
           fixed size footer
         </td>
      </tr>
   </table>
</body>

Previously, I achieved this layout with the following code:

html, body { height:100% }
table#main { height:100% }
td#footer { height:123px }

However, it seems like this method is now considered obsolete. Can someone who is up to date on the latest markup techniques in 2011 provide some guidance?

UPDATE I understand the importance of semantic markup and using divs. However, my current issue is how to make the footer stay at the bottom of the page even when the content is empty or short. When the content is long, the footer should move down accordingly. Absolute and fixed positioning have not been successful solutions.

SUMMARY OF UPDATES:

  1. I've experimented with using display:table and display:table-row, which seems to work: little content, more content
  2. Andrej recommended the method outlined in Make the Footer Stick to the Bottom of a Page, which also works: little content, more content

Despite some success with these methods, I can't help but feel disappointed that the first method still relies on tables and the second method is quite old and hacky. I was hoping for some new, innovative solutions!

Answer №1

Back in 2011, using tables for layout was outdated!

If it were up to me, I'd structure the markup like this:

<body>
   <div id="main" role="main">
        <header>
            (includes logo, nav, and fixed height)
        </header>
        <div class="content"> /*consider using <article> or <section> instead of div*/
            (actual content goes here)
        </div>
        <footer>
            fixed size footer
        </footer>
    </div>
</body>

The CSS would remain similar, with only the selectors being modified:

html, body { height:100% }
#main { height:100% }
footer { height:123px }

For a fixed footer, I recommend using position:absolute or position:fixed, depending on how you want it to behave (scroll with page or stay at the bottom).

To create a "sticky" footer that stays at the bottom of the page but moves with the content, you can follow this method.

Answer №2

As we step into 2013, a well-structured table with thead/tfoot/tbody sections still remains unparalleled.

Behold below a flawless HTML5 page with a fixed header and footer, ensuring 100% height and zero resizing issues.

<!DOCTYPE html>    
<meta charset="utf-8" />
<title>valid HTML5 / fixed header and footer / no CSS sizing hiccups</title>
<style type="text/css">

html, body, table { height:                 100% }    
th {                height:                 80px }    
#f {                height:                 40px }

table {             width:                  1000px }

html, body {        margin:                 0 }
table {             margin:                 0 auto }

td {                text-align:             left }      
html, body {        text-align:             center } /* crucial for older browsers */
th {                text-align:             right }

html, body {        background-color:       rgb(148,0,211) } 
#m {                background-color:       #f39 }

#m {                -webkit-border-radius:  25px;    
                    -khtml-border-radius:   25px;    
                    -moz-border-radius:     25px;    
                    -ms-border-radius:      25px;      
                    -o-border-radius:       25px;      
                    border-radius:          25px; }
</style>
<table>      
  <thead><tr><th>       head</th></tr></thead>
  <tfoot><tr><td id="f">foot</td></tr></tfoot>
  <tbody><tr><td id="m">main</td></tr></tbody>
</table>

Answer №3

For a more "modern" approach in the year 2016, I have an updated solution compared to my response in 2013:

Implement the 100vh technique in CSS3, where vh represents ViewPort height.

html, body {            height:                 100% } 
header {                height:                 100px }
footer {                height:                 50px }
main {                  height:                 calc(100vh - 150px); }

html, body {            width:                  100% }  
header, main, footer {  width:                  1000px }

html, body {            margin:                 0 }
header, main, footer {  margin:                 0 auto }

html, body {            padding:                0 }

html, body {            text-align:             center }

body {                  background-color:       white } 
header {                background-color:       yellow }
main {                  background-color:       orange }
footer {                background-color:       red }
<!DOCTYPE html>
<meta charset="utf-8" />
<title>test</title>
<header>bla</header>
<main>bla</main>
<footer>bla</footer>

If you need compatibility with older browsers, you can still refer to the code provided in my 2013 answer.

Answer №4

In today's world, the way to accomplish this task is very similar to the past.

http://jsfiddle.net/5YHX7/3/

html, body { height: 100%; width: 100%; margin: 0; }
div { height: 100%; width: 100%; background: #F52887; }

Additionally, the code would look like this:

<html><body><div></div></body></html>

Answer №5

Although using table tags to lay out a webpage may still work, it is now considered outdated and not recommended. It is better practice to utilize "semantic" web markup, which involves using tags for their intended purposes - for example, using a table tag for table data, rather than for design purposes. DIVs are meant for designing the layout of your webpage. A List Apart is a valuable resource for gaining a better understanding of these concepts.

If you are interested in learning more about semantic markup, check out this article: http://www.alistapart.com/articles/12lessonsCSSandstandards

For a sample page that demonstrates these principles, take a look at:

Answer №6

When it comes to creating a design that is both "modern" and "compatible", one approach to consider is the grid method. While it may be seen as too modern at the moment, with some adjustments it could potentially be a solution worth exploring.

For a more in-depth understanding of this concept, you can check out this informative article: https://developers.google.com/web/updates/2017/01/css-grid

Although the code may appear visually appealing, the challenge lies in making sure it is supported by all browsers. To address this, additional measures may need to be implemented.

https://jsfiddle.net/qLcjg6L6/1/

<!DOCTYPE html>
<html>
<head>
<style>

html, body {
            height:             100%;
            min-height:         100vh;
            margin:             0;
            padding:            0 }

body {      display:            grid;
            grid-template-rows: minmax(auto, min-content) auto minmax(auto, min-content);
            grid-template-columns: 100% }

header {    background:         red }
main {      background:         pink }
footer {    background:         purple }

/* as this code is yet far from well-supported, here's a brute force... */
header {    height:             70px }
footer {    height:             60px }
main {      height:             calc(100vh - 130px); }
/* 130px being the sum of header/footer - adapt to your desired size/unit */

</style>
</head>
<body>
<header>hdr</header>
<main>main</main>
<footer>foot</footer>
</body>
</html>

Answer №7

Here is a suggestion to enhance your layout by incorporating 3 columns into your header, main, and footer sections:

http://example.com

<!DOCTYPE html>
<div>
    <header>
        <div id="header">
            <h1>Header</h1>
        </div>
    </header>
    <main>
        <div id="left">Column 1</div>
        <div id="center">Column 2</div>
        <div id="right">Column 3</div>
    </main>
    <footer>
        <div id="footer">
            <p>Footer</p>
        </div>
    </footer>
</div>

Answer №8

There hasn't been any mention of the flex-box method yet

https://jsfiddle.net/55r7n9or/

<!DOCTYPE html>
<html>
<head>
<style>

html, body, div {
            height:         100%;
            margin:         0;
            padding:        0 }

div {       display:        flex;
            flex-direction: column }

main {      flex:           1 }

header {    background:     red }
main {      background:     pink }
footer {    background:     purple }

</style>
</head>
<body>
<div>
  <header>hdr</header>
  <main>main</main>
  <footer>foot</footer>
</div>
</body>
</html>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Scaling Youtube video backgrounds with Squarespace

How can I inject a code into Squarespace to scale a video as the background and still keep the navigation functional? Here is the current code I am using: <div style="position: fixed; z-index: 50; width: 100%; height: 100%"> <iframe frameborder= ...

Steps for displaying a div when clicking a link in the navigation

Hello there, I'm from the Netherlands so please excuse any mistakes in my English. I will do my best to explain my question clearly.... Objective The goal is to have a navigation bar where each item, when clicked on, will display a div with content ...

What is the best way to center images horizontally in CSS and HTML?

I'm looking to create a driver's page where the desktop view displays images horizontally instead of vertically. I've tried adjusting the display attribute with limited success, and so far, the grid display is the closest I've come to a ...

Deactivate the signup button automatically when the user is not present, without the need to refresh

To determine user availability, the system will check the table of users. If the user is available, the signup button will be displayed; otherwise, the button will remain disabled or its color may change. ...

Tips for updating the left positioning of a Div element on the fly

Although my title may not fully explain my goal, I am dealing with dynamically created div elements. When a user triggers an ajax request to delete one of the divs, I want to remove it from the DOM upon success. The issue arises in adjusting the layout aft ...

The inserted button's click event does not trigger the contentEditable DIV

I have a contentEditable DIV that I manage using the directive below: <div class="msg-input-area" [class.focused]="isMsgAreaFocused" contenteditable [contenteditableModel]="msgText" (contenteditableModelChang ...

Personalized scrollbar extends beyond the screen

I have been working on creating my own scroll bar, and for the most part, it's functioning well. However, there is one small issue that I'm facing. Whenever I reach the bottom of the page, the handle of the scroll bar disappears underneath the v ...

Tips for showcasing div content on a separate line rather than inline within a collapsible <a> tag

I am currently working on integrating a sidebar using bootstrap 5. When I click on the user initials, I want something like this to happen (collapse): https://i.sstatic.net/ejbhD.png However, the result I'm getting is different: https://i.sstatic. ...

The active tabs or placeholders do not update properly when I click on them

Is there anyone who can help figure out why the tabs are not functioning as expected? The goal is for the tabs to change the input field placeholder and perform a search based on the active tab. If you're able to assist, please review my complete cod ...

Turn off the automatic resizing of Internet Explorer 11 to fit the width of the viewport - IE11

Is there a way to stop Internet Explorer 11 from automatically zooming to fit the viewport width? I need the browser to respect my max-width rule and not scale the content to fill the entire width of the viewport on its own. This issue is happening in IE ...

What could be the reason for the unexpected invisibility of the 4px margin on a static div?

I have a straightforward situation where I am using a fixed positioning <div> that spans the entire width with 100% width. It is designed to have a margin of 4px on all sides. However, for some reason, the right side margin is not visible. Can someon ...

Implement a grid layout for columns within the profile section

Each user on my website has a profile tab that displays their submitted posts. To showcase these posts, I utilize the following code: <?php while ($ultimatemember->shortcodes->loop->have_posts()) { $ultimatemember->shortcodes->loop-> ...

Struggling to remove the image tag from the jQuery ajax response

My web app is designed to send an ajax post request to a PHP script, which then returns a chunk of HTML data. This HTML includes an image and a table of information. The challenge I'm facing is how to extract the image from the rest of the HTML so tha ...

Steps to launching a URL in a new window on Internet Explorer

I want it so that when button1 is clicked, the URL opens in a new window using Internet Explorer. ...

Tips for utilizing string interpolation in the style tag of an Angular component

@Component({ selector: 'app-style', template: ` <style> .test { color: {{ textColor }} } </style> ` }) export class StyleComponent { textColor = "red"; } The current method doesn't appear to b ...

What is the best way to change styles in CSS for parent and child elements using selectors?

Hey there! I am currently working on customizing the navigation menus in WordPress and here is the HTML code for the navigation menus: <nav id="primary-navigation" class="site-navigation primary-navigation" role="navigation"> <h1 class="menu- ...

What could be the reason for the absence of the CSS destination folder being generated by

My colleague has the exact same code and can run gulp without any issues, but I'm struggling to compile the css files using Gulp. Gulp isn't creating the css destination file or the css files themselves. However, it works perfectly fine for build ...

Display or conceal several elements using JQUERY/HTML upon hovering

Here is the current progress: <div style="position: relative;"> <a href="#games"> <div class="sidenavOff"> <img src = "images/card_normal.png" /> <img src = "images/category_icons/icon_games.png" style = "position: a ...

Limit jQuery script to operate exclusively on a single page

Despite the abundance of answers to similar questions, my lack of JS skills hinders me from implementing them in my specific case. On my WordPress site, I have a script that changes the navigation bar's color when scrolling down to the #startchange CS ...

New HTML5 feature enables users to upload images onto canvas, enabling them to drag, rotate, and even

I'm a beginner in Html5 and I'm having trouble with uploading an image to the canvas. When I provide the direct source of the image, it works fine. I referred to this link for help javascript: upload image file and draw it into a canvas. Let me s ...