Ensure the footer spans the entire width of the layout and that the content fills the space

I am currently facing a roadblock and can't figure out how to solve it. My website has a sticky footer that is working fine, but I want the footer to have a width of 100%. To achieve this, I have an outer div that pushes the footer to the bottom and a content div that is centered.

Usually, I set a background color for the full outer div, which means that the header, content, and footer all have the same background color. However, I can set a custom color for each div to override the white overlay.

The issue arises because the layout now has a width of 100%, making it difficult to give the content div a background color that extends all the way to the footer.

To simplify my previous explanation, here is my question: How can I have a header and footer with a width of 100% while keeping the content centered with a white background extending till the footer? Note that the content height may be less than or equal to 100%.

Below is the HTML code I am using:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
  <title>Temp</title> 
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> 
  <link rel="stylesheet" type="text/css" href="style.css" />
</head> 
<body>
<div id="outer">
  <div id="outer_header">
    <div id="header">
      <div id="menu_block"></div>
      <div id="image_block"></div>
    </div>
  </div> 
  <div id="outer_content">
    <div id="content"> 
    dafdafdafda
    </div> 
  </div>
  <div id="clearfooter"></div> 
  <div id="outer_footer">Footer - | |- Footer </div> 
</div> 
</body> 
</html>

Here is my CSS code:

/* mac hide\*/ 
html, body {height:100%} 
/* end hide */ 
body { 
  padding:0; 
  margin:0; 
  text-align:center; /* for ie6 and under */ 
  min-width:1024px;
  background-color: #eee8d8; 
  color: #000000;
} 
#outer { 
  min-height:100%;
  min-width:1024px; 
  width:100%; /* add 2px if borders are not used */ 
  text-align:left; 
  margin:auto; 
  position:relative; 
} 

* html #outer{height:99.9%;} /*For ie as treats height as min-height anyway - also addreeses rounding bug at bottom of screen in IE*/ 
#outer_header { 
  height:280px;
  width:100%;
  float:left; 
  position:relative;
  background-color:#1f4c3f;
}  

#header { 
  height:280px;
  width:1000px;
  margin:auto;
  position:relative;
}

#menu_block {
  width:1000px;
  height:90px;
}

#image_block {
  width:1000px;
  height:190px;
  background-color:#FFFFFF;
}

#outer_content {
    width:100%;
    position:relative;
    float:left;
  background-color:red;
}

#content { 
  width:1000px;
  height:100%;
  position:relative;  
  margin:auto;
  background-color:#FFFFFF;
}

#outer_footer { 
  width:100%; /* add 2px if borders are not used on the #outer div */ 
  clear:both; 
  height:50px; 
  background-color:#1f4c3f;  
  left:0; 
  bottom:0; 
  position: absolute; 
} 
* html #outer_footer {/*only ie gets this style*/ 
  \height:52px;/* for ie5 */ 
  he\ight:50px;/* for ie6 */ 
  margin-bottom:-1px; 
} 
div,p  {margin-top:0}/*clear top margin for mozilla*/ 

#clearfooter {width:100%;height:52px;clear:both} /* to clear footer */

Note: Ideally, I would like to achieve this using only CSS without relying on JavaScript.

Edit: Desired result: Picture one shows 1 row of white space (because the text ends there), but I want the content to fill up until the sticky footer...

Answer №1

Here are three different solutions, each offering a unique scroll behavior.

  1. JSFiddle Solution without scrollbars:

    Markup:

    <div id="header"></div>
    <div id="content"></div>
    <div id="footer"></div>
    

    Stylesheet:

    body, html {
        margin: 0;
        padding: 0;
        height: 100%;
        width: 100%;
        overflow: hidden;
    }
    body {
        background: red;
    }
    #header {
        height: 160px;
        background: #1f4c3f;
    }
    #content {
        position: absolute; 
        background: white;
        width: 500px;
        top: 160px;
        bottom: 40px;
        left: 50%;
        margin-left: -250px;
    }
    #footer {
        position: absolute;
        bottom: 0;
        background: #1f4c3f;
        width: 100%;
        height: 40px;
    }
    
  2. JSFiddle solution with vertical scrollbar and footer always visible:

    Markup:

    <div id="header"></div>
    <div id="content"></div>
    <div id="footer"></div>
    

    Stylesheet:

    body, html {
        margin: 0;
        padding: 0;
        width: 100%;
    }
    body {
        background: url('https://i.sstatic.net/WcYOv.png') center repeat-y;
    }
    #header {
        height: 160px;
        background: #1f4c3f;
    }
    #content {
        width: 500px;
        margin: 0 auto;
    }
    #footer {
        position: fixed;
        bottom: 0;
        background: #1f4c3f;
        width: 100%;
        height: 40px;
    }
    
  3. JSFiddle solution with vertical scrollbar and moving footer:

    Markup:

    <div id="wrap">
        <div id="header"></div>
        <div id="content"></div>
    </div>
    <div id="footer"></div>
    

    Stylesheet:

    body, html {
        margin: 0;
        padding: 0;
        height: 100%;
        width: 100%;
    }
    #wrap {
        background: url('https://i.sstatic.net/WcYOv.png') center repeat-y;
        width: 100%;
        min-height: 100%;
    }
    #header {
        height: 160px;
        background: #1f4c3f;
    }
    #content {
        width: 500px;
        margin: 0 auto;
        padding-bottom: 40px;
    }
    #footer {
        position: relative;
        top: -40px;
        background: #1f4c3f;
        width: 100%;
        height: 40px;
    }
    

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

How can I keep images from getting cut off when using slick slider with a fixed background?

My current design includes a stylized slick-slider that extends beyond the top border: However, I have set a fixed background for .slick-list, ensuring only the content (text, picture, and button) scrolls: How can I avoid the picture being cropped in t ...

Go back one directory in PHP

I am attempting to direct my app to another PHP page that is located at the root folder of the project. I am encountering difficulties with saving my SESSION variable, and I believe this redirection will help resolve it. Although I understand that this is ...

Position tables on the right side of adjacent tables

There are two tables named id="BFCategories" and "BMICategories". Additionally, there are two other tables with id="BFTable" and "BMITable" BFCategories should come after BFTable, while BMICategories should follow BMITable. How can I a ...

Tips for using a customized filtering pipe with *ngFor in Angular version 4

Implementing a custom filter pipe on a <li></li> element to enable search functionality. Users can enter a term in the <input> field and the list gets filtered accordingly. test.component.html <form id="filter"> <label>F ...

Curved edges, Layering, Content overflow restriction

My design consists of two circular divs with the border-radius property set to its max value. The first circle contains a header div with a unique background-color and overflow:hidden to create the illusion that the top portion of the circle has a differen ...

Unable to display a recursive component with Vue3 CDN

I am currently working on a project using Vue3 CDN because the project environment cannot run npm. I have been trying to create a component with html+CDN, but when attempting to create a recursive component, it only renders the top-level element. Is there ...

Retrieve unique elements from an array obtained from a web API using angular brackets

I've developed a web application using .NET Core 3.1 that interacts with a JSON API, returning data in the format shown below: [ { "partner": "Santander", "tradeDate": "2020-05-23T10:03:12", "isin": "DOL110", "type ...

What is the best way to position an icon to the left of the text?

My code is as follows: <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/> <span> <i class="fa fa-check"></i> This text is just a test and repeating to ...

Randomly swap images in HTML when a button is clicked

In order to change images randomly on mouse click, I came across this solution: <SCRIPT LANGUAGE="Javascript"> function banner() { } ; b = new banner() ; n = 0 b[n++]= "<A HREF='index.html'><IMG SRC='images/pic1.jpg'> ...

Bootstrap seems to be struggling with recognizing the last child of a button group. What steps can I take to troubleshoot and resolve this

I'm facing a small but annoying issue with my button group. The last button in the group appears rectangular instead of having rounded corners at the top right and bottom right. This is because the a.btn element is followed by a modal within the btn-g ...

Creating a Table with Alternating Rows in HTML

Here is how my table looks. I attempted to apply striping with the following CSS: tr:nth-child(even) { background-color:#AD0D10; } <table> <tr> <th >xxx</th> <th >xxxxx</th> <th>xxxxxxx</th& ...

Edit: "Submit a binary file with just JavaScript"

I am currently developing a Chrome application that utilizes the HTML5 Filesystem API to enable users to import and synchronize files. A challenge I'm facing is when attempting to sync image files, as they appear to become corrupted during the upload ...

What is the best way to assign the order position in CSS?

I recently attempted to incorporate a CSS animated background into my project. Here is how it currently looks: The particle effect hovers above the menu list (which currently just says "test"). The CSS code for my particles is as follows: .circles { pos ...

Maintain HOVER State in CSS Drop Down Menu upon Clicking

I have implemented a CSS Drop Down Menu on my website. In a specific section of the site, I am using the menu links to trigger AJAX-jQuery calls that dynamically change the content of a DIV without the need for page reloading. <script type="text/javasc ...

On hover, HTML5 animations smoothly transition a dashed border inward

I'm in the process of creating a drag and drop module and I want to ensure that as the user drags an item, the dashed line around the outside moves inward and the box changes color. However, I don't want the appearance of the dashed line to chang ...

What is the best way to link a style sheet in PHP when the files are located in different folders?

I have a php project structured in the following way : ├───public │ index.php ├───styles │ style.css ├───vendor └───views ├───category │ show.php ├───layout │ f ...

Turning off the ability to horizontally scroll in an iframe using touch controls

I am trying to disable horizontal scrolling in an iframe on my website, particularly when a user uses touch input and drags horizontally. The scroll bars can still be visible and draggable when touched directly. Unfortunately, I do not have control over th ...

Enable hovering only on the actual content within the box, not on the empty space

Currently, I am working on creating a navigation bar using flexbox. Everything seems to be functioning correctly except for one issue: when I hover over the space between my logo and the navigation links, it changes the color of my logo to the hover state ...

Tips for ensuring an element stays anchored at the bottom even when the keyboard is displayed

I recently encountered a situation on one of my pages where an element positioned at the bottom using absolute positioning was causing issues when the keyboard was opened, as it would move to the middle of the page unexpectedly. While this may seem like a ...

Increasing the height of a menu using jQuery animations

Hello everyone, I'm working on a project and need some assistance with this jsfiddle: Check it out here. Currently, the box expands to a static width upon hovering, but sometimes it extends beyond the last text item causing it to slide back up when t ...