What is the best way to ensure that content fills the entire screen?

My goal is to position the footer at the bottom of the page.

This is the initial code I tried:

<html>
    <head>
    <title>Freds Cars, Trucks & Jeeps</title>
    <style>
        #container{
           height: auto;
           width: 100%;
        }
    </style>
</head>
<body style="margin: 0px;>
    <div id="container">
        <header style="background-color:black;
                color: white;
                text-align:center">
            Fred's Cars, Trucks & Jeeps
        </header>

        <h1>Welcome to Freds Cars!</h1>

        Thank you for visiting our site!

        <footer style="background-color:black;
                color: white;
                text-align:center">
           121 Fredway St.
           Cartown USA
        </footer>
    </div>
</body>
</html>

However, the webpage did not display as intended:

https://i.sstatic.net/IqK4y.png

I assumed setting the container id style would make the container div 100% but evidently not. Is there a simpler solution?


UPDATE

To resolve the issue, I made the following changes:

<html>
<head>
<title>Freds Cars, Trucks & Jeeps</title>
<style>
   body{
      margin: 0;
      display: flex;
      flex-direction: column;
      min-height: 100vh;
   }

   header{
      height: 5vh;
      padding-top:5;
      background-color:black;
      color: white;
      text-align:center;
      font-size: 16pt;
      font-weight: bold;
      font-family: arial;
   }

   .spacer {
       flex: 1;
   }
</style>
</head>
<body>

<header>
    Fred's Cars, Trucks & Jeeps
</header>

<h1>Welcome to Freds Cars!</h1>

Thank you for visiting our site!

<div class="spacer"></div>
<footer style="background-color:black;
            color: white;
            text-align:center">
Contact us: (555)555-5555<br />
121 Fredway St.<br />
Cartown USA
</footer>

</body>
</html>

The updated code now displays correctly like this:

https://i.sstatic.net/tmgr8.png

Answer №1

Utilize the vh (view height) unit for styling your body in the css file.

For more information, check out W3 School

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Freds Cars, Trucks & Jeeps</title>
    <style>
        #container {
            height: 100vh;
            width: 100%;
        }

        header,
        footer {
            height: 10vh;
        }

        main {
            height: 80vh;
        }
    </style>
</head>

<body style="margin: 0px">
    <div id="container">
        <header style="background-color:black;
                    color: white;
                    text-align:center">
            Fred's Cars, Trucks & Jeeps
        </header>
        <main>

            <h1>Welcome to Freds Cars!</h1>

            Thank you for visiting our site!
        </main>

        <footer style="background-color:black;
                    color: white;
                    text-align:center">
            121 Fredway St.
            Cartown USA
        </footer>
    </div>

</body>

</html>

https://i.sstatic.net/VW1S3.png

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

Maintaining Image Aspect Ratio with CSS Max-width Property

I'm currently facing an issue where I am using max-width to resize images, but they are not scaling proportionally. Is there a way to achieve this with JavaScript/jQuery? Additionally, is it possible to do this without initially knowing the image&apos ...

Top method for triggering an external JavaScript function through an HTML element's onclick event obtained from an Ajax request

index.php: <html> <head> <script type="text/javascript" src="script.js"></script> </head> <body> <div id="box"></div> <div onclick="getContent()">Open Window</div ...

Adjust the svg rate using jQuery or JavaScript

Seeking help with a gauge I found on CodePen - struggling to adjust bubble values... <path id="Fill-13" fill="#F8B50F" d="M3.7 88.532h26.535v-#.795H3.7z"/> Can change the bars in JS, but not the bubbles using jq/js. Adjust the gauge with values be ...

Angular error TS2339: The property 'before' is not found on type 'HTMLElement' / 'HTMLTextAreaElement' / etc

Objective: My goal is to reposition a div (containing a mat-select dropdown) ABOVE a mat-card-title when the user is accessing the site from a mobile device. If the user is not on a mobile device, the div should remain in its original position to the right ...

Generate a new object from the contents of a div

Having the HTML structure below: <div id="main"> <div id="myDiv1"> <ul> <li>Abc</li> <li>Def</li> </ul> </div> <div id="myDiv2"> <ul> <li>Ghi</l ...

What is the best way to create a structured arrangement of nested divs using a recursive query?

I recently encountered a challenging recursive postgresql query that retrieves data in the following format: id depth path has_children 1 1 1 true 2 2 1.2 true 3 3 1.2.3 true 4 4 1.2.3.4 ...

Two miniature tables placed next to each other within a single cell, both sharing equal height

Is there a way to make two tables within a parent table cell have the same height using HTML and CSS? If one cell has more content than the other, is it possible to adjust the height of both tables accordingly? <table cellpadding="0" border="1" cell ...

Is there an obstacle hindering the load event?

There's something strange happening on a website I created. The page content loads in 5 or 6 seconds, but the DOMContentLoaded and Load Event don't actually fire until 1 minute and 10 seconds later. When I reload the page during loading, it load ...

What is preventing me from using the JavaScript toggle button multiple times?

I am currently trying to implement a JavaScript toggle button in two different sections on the same page, but I am encountering difficulties. I attempted to change the IDs as well, but it didn't seem to work. Is it not possible to achieve this, or am ...

Unlocking the potential of data inputted in a JQuery Autocomplete form

Seeking assistance with extracting data from a form to utilize for additional purposes <form onsubmit="return false;"> Enter a School: <input id="schoolsearch" type="text" /> <INPUT TYPE=SUBMIT VALUE="GO"> </form> I am struggling ...

HTML5 - Transforming your webpages into interactive flipbooks

Is there a way to achieve a page-turn effect when loading external HTML pages into a div? I am interested in utilizing CSS3, HTML5 or jQuery for this purpose. ...

Avoid abrupt image flickering when the source is being changed

I'm encountering an issue with image flickering when using large images. Within my body, I have a set of 5 images: <img id="img1" src="img1.png" width="250"> <img id="img2" src="img2.png" width="250"> <img id="img3" src="img3.png" widt ...

CSS codes are ineffective when used simultaneously

Looking for help with my CSS code situation involving opacity on images: CSS for Opacity: .hover:hover { opacity:0.8;} HTML for Opacity: <a href="http://www.byggprojektoren.se"> <img class="hover" src="http://byggprojektoren.se/wp-content/u ...

Encountering an undefined error while attempting to retrieve an object from an array by index in Angular

Once the page is loaded, it retrieves data on countries from my rest api. When a user selects a country, it then loads the corresponding cities for that country. Everything is functioning properly up to this point, however, upon opening the page, the city ...

Issues with Angular HTML failing to load correctly

Greetings, I am seeking assistance with an issue that has been challenging me for quite some time. As a newcomer to Angular JS, I may be overlooking something obvious. Here is the scenario: I am utilizing ng-repeat on a directive in the following manner: ...

Is there a way to eliminate overflow on a section of my CSS page without sacrificing the 100vh height?

My professor requested a section in my project to have its height adjusted to the viewport, however this has created a large gap between that section and the rest of my content. How can I remove the overflow and close the gap? <main> <sect ...

Creating a non-intrusive modal in Bootstrap 4 that maintains background visibility and scrolling functionality

I have been working on creating a Bootstrap modal that allows the user to interact with the rest of the website while it is visible. The steps I have taken so far include: Adding data-backdrop="false" to the modal div in order to hide the shadow of the b ...

Eliminate viewed alerts by implementing a scrolling feature with the help of Jquery, Javascript, and PHP

My goal is to clear or remove notifications based on user scrolling. The idea is to clear the notification once the user has seen it, specifically in a pop-up window for notifications. I am relatively new to Javascript/jQuery and feeling a bit confused abo ...

Guide to making an `Ajax Login` button

I am interested in creating a SIGN IN button using ajax. Specifically, I want it to display something (such as welcome) on the same page without refreshing it. This is the progress I have made so far: update2: <form id="myForm" onsubmit="return signi ...

What is the reason behind Firefox opting for a 2x image while Chrome goes for a 1x image when the devicePixelRatio is 1

When deciding between 1x and 2x options: Firefox will opt for 2x if devicePixelRatio is less than 1 Chromium will choose 2x if devicePixelRatio is less than 1.5 Testing this in Safari is tricky as devicePixelRatio remains constant even when scaling t ...