The sticky and flexible footers and headers CSS feature is functioning perfectly in WebKit, though it seems to be having difficulties in

I'm working on constructing a layout that features a header and footer with flexible heights, while the middle section takes up the remaining space. If there is overflow in the middle section, a scroll bar should appear specifically for that section.

The code I have currently works well for Safari and Chrome:

<!DOCTYPE html>
<html>
  <head>
    <style>
      html, body {
        margin: 0;
        padding: 0;
        height: 100%;
      }

      .l-fit-height {
        display: table;
        height: 100%;
      }

      .l-fit-height > * {
        display: table-row;
        height: 1px;
        background-color: red;
      }

      .l-fit-height-expanded {
        height: auto;
        background-color: blue;
        display: table-row;
      }

      .l-scroll-content {
        height: 100%;
        overflow-y: auto;
      }
    </style>
  </head>
  <body>
    <div class="l-fit-height">
      <section>
        Header
      </section>
      <section class="l-fit-height-expanded">
        <div class="l-scroll-content">
          <p>Foo</p>
          <p>Foo</p>
          <p>Foo</p>
          <p>Foo</p>
          <p>Foo</p>
          <p>Foo</p>
          <p>Foo</p>
          <p>Foo</p>
          <p>Foo</p>
          <p>Foo</p>
          <p>Foo</p>
          <p>Foo</p>
          <p>Foo</p>
          <p>Foo</p>
          <p>Foo</p>
        </div>
      </section>
      <section>
        Footer
      </section>
    </div>
  </body>
</html>

I can't seem to understand why Firefox behaves differently. While the content in the middle expands correctly in height, it does not shrink beyond the height of its contents.

It's challenging to determine the correct behavior. Any suggestions?

Edit

A similar example setup can be found here: http://jsfiddle.net/t3mZF/

Interestingly, if .l-fit-height-row-content is changed to display: table-cell, both WebKit and Gecko display the same behavior, ignoring the overflow.

Using display: block, WebKit shows the desired behavior (scrollbar appears with the footer at the bottom of the viewport), but Firefox doesn't add the scrollbar and instead pushes the footer off the screen (scrollbar on the viewport, not the middle content).

I've also filed a report on Bugzilla

Answer №1

By incorporating a few additional divs and utilizing the crucial display: inline-block, I have successfully implemented Torben's absolute positioning trick in Firefox. This breakthrough allows for a completely flexible header and footer using pure CSS.

<!DOCTYPE html>
<html>
  <head>
    <style>
      html, body {
        margin:  0;
        padding: 0;
        height:  100%;
      }

      .l-fit-height {
        display: table;
        height:  100%;
      }

      .l-fit-height-row {
        display: table-row;
        height:  1px;
      }

      .l-fit-height-row-content {
        /* Firefox requires this */
        display: table-cell;
      }

      .l-fit-height-row-expanded {
        height:  100%;
        display: table-row;
      }

      .l-fit-height-row-expanded > .l-fit-height-row-content {
        height: 100%;
        width:  100%;
      }

      .l-scroll {
        /* Firefox requires this to do the absolute positioning correctly */
        display:    inline-block;
        overflow-y: auto;
        position:   relative;
      }

      .l-scroll-content {
        position: absolute;
        top:      0;
        bottom:   0;
      }
    </style>
  </head>
  <body>
    <div class="l-fit-height">
      <section class="l-fit-height-row">
        <div class="l-fit-height-row-content">
          Header
        </div>
      </section>
      <section class="l-fit-height-row-expanded">
        <div class="l-fit-height-row-content l-scroll">
          <div class="l-scroll-content">
            <p>Foo</p>
            <p>Foo</p>
            <p>Foo</p>
            <p>Foo</p>
            <p>Foo</p>
            <p>Foo</p>
            <p>Foo</p>
            <p>Foo</p>
            <p>Foo</p>
            <p>Foo</p>
            <p>Foo</p>
          </div>
        </div>
      </section>
      <section class="l-fit-height-row">
        <div class="l-fit-height-row-content">
          Footer
        </div>
      </section>
    </div>
  </body>
</html>

I trust that this solution will be beneficial

Answer №2

It seems that you've encountered a couple of issues in this situation:

  1. The dimensions of tables are not as fixed as those of normal div elements. Tables typically expand to accommodate their content and do not pay much attention to the height property if the content exceeds the space available.

    To address this, you need to position the scroll-box absolutely so that its content is excluded from the calculation of the overall size. Then, you simply need to set the position and dimensions of the scroll-box to fill the entire area of the expanded table row. However, this introduces another complication:

  2. The simple solution would be to apply position:relative; to the table row and width:100%;height:100%; to the scroll-box. Unfortunately, this approach won't work because most browsers disregard the position property of table rows. One workaround would be to insert another div with

    display:table-cell;position:relative;
    between the table row and the scroll-box, but this solution does not function properly in Firefox, as Firefox does not allow relative positioning within a table.

    However, Firefox does permit relative positioning for the table itself, so the key is to set position:relative; for the table-div and adjust the size and position of the scroll-box based on the dimensions of the entire table.

This may have some limitations if you prefer a pure CSS solution, but there are options available. Additionally, there's an elegant JavaScript solution provided in the code below which offers a different way to solve the problem. I've made minimal changes to your code and included comments to guide you through the modifications. Feel free to leave a comment if you have any questions.

<!DOCTYPE html>
<html>
  <head>
    (CSS and JavaScript code here)
  </head>
  <body>
    (HTML structure here)
  </body>
</html>

Please note that I haven't tested the code in Internet Explorer, but it should work as intended across browsers.

Answer №3

Check out this link for tips on making Firefox display an automatic horizontal scrollbar in a div: How to make Firefox show an auto horizontal scollbar in a div?

Firefox does not support overflow-x and overflow-y.

Instead, consider using

overflow:-moz-scrollbars-horizontal;
or
overflow:-moz-scrollbars-vertical;

Answer №4

Kindly review my latest update.

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Untitled Document</title>
<style>

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

#wrapper {
    margin: auto;
    overflow: hidden;
    position: relative;
    width: 99.9%;
}

#main {
    margin-bottom: 67px;
    margin-top: 77px;
    overflow: auto;
    padding-bottom: 80px;
}

#footer {
    bottom: 0;
    clear: both !important;
    height: 75px !important;
    margin-top: -68px !important;
    position: fixed !important;
}

#header {
    background: none repeat scroll 0 0 #FFFFFF;
    border: 1px solid #000000;
    height: 75px !important;
    margin-top: -77px;
    overflow: hidden;
    position: fixed;
    width: 100%;
}
</style>

</head>

<body>

     <div id="wrapper">
         <div id="main">
            <div id="header">content for header section is placed here</div>

            <div id="content">
                main content
            </div>
         </div>
     </div>
     <div id="footer" style="width:99.9%; margin:auto; border:solid 1px #666; ">
      Content for the footer goes here
     </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

How do Box and Grid components differ in Material-UI?

Can you please explain the distinction between Box and Grid in Material-UI? How do I know when to use each one? I'm struggling to understand their differences. ...

Transform a REACT js Component into an HTML document

I'm working with a static React component that displays information from a database. My goal is to implement a function that enables users to download the React component as an HTML file when they click on a button. In essence, I want to give users ...

When dynamically adding rules to jQuery validate, I encountered an unexpected error

My dynamic form validation with jQuery validate involves a drop-down menu on my HTML that includes values 1, 2, 3, and 4. Depending on the selection made from the drop-down, different div elements are displayed with corresponding fields. All the input fie ...

Disabling font-awesome checkboxes

As someone who is new to front-end technologies, I am trying to achieve the following. Despite my efforts to find a solution, I have not been successful so far. Here is the issue I am facing-- I have two master checkboxes labeled 1) I do not like numbers ...

Currently in motion post file selection

I am currently facing an issue with a button that triggers a file selector pop-up. Below is the code snippet: <button mat-raised-button (click)="inputFile.click()">Choose a file</button> <input #inputFile type="file" [style.display]="' ...

Acquire HTML using Java - certain characters are not retrieved accurately

Currently, I am facing an issue with my Java code when trying to fetch HTML from Turkish webpages. It seems that my Java code is struggling to recognize certain Turkish characters. Below is the Java code snippet I am using: import java.io.BufferedInputStr ...

Unchecked Checkbox Issue in Yii2

Currently working with yii2 and trying to check my checkbox: <?= $form->field($model, 'is_email_alerts')->checkbox(['label'=>'','checked'=>true,'uncheck'=>'0','value'= ...

Div vanishes once SVG Gaussian blur is applied

I'm attempting to add a Gaussian blur effect to an element that contains some child nodes with content. In Chrome, I successfully applied the blur using the following style: -webkit-filter: blur(2px); Unfortunately, Firefox does not support thi ...

Use sed to extract integers from HTML based on various criteria

Before we continue, I must confess: I realize using regex to parse HTML is generally frowned upon, but if Chuck Norris can do it, why can't I? ;) My goal is to extract specific data from this HTML page: http://pastebin.com/unAifctF based on thre ...

What is causing the error that app.js file cannot be located?

Here is the layout of my directory: ReactCourse // Main folder public // Subfolder within ReactCourse index.html // HTML file with linked js file app.js // JavaScript file This is the content of index.html: <!DOCTYPE ...

Scraping data from a website using Python by targeting the `<td

Just starting out with Python and Web Scraping... I'm trying to extract the numbers 1.16, 7.50, and 14.67 from the highlighted portion of code using td, class, table-matches__odds pageSoup.find_all, but so far haven't had any luck. Anyone have an ...

What steps do I need to follow to create a 3D shooting game using HTML5 Canvas?

I'm eager to create a 3D shooter game with HTML5 Canvas, focusing solely on shooting mechanics without any movement. Can anyone provide guidance on how to accomplish this? I've looked for tutorials online, but haven't come across any that m ...

Why can't JQuery/javascript's dynamic gallery's images be used as buttons instead of links?

I've been struggling with my online portfolio for several nights as I build my website. Despite exhaustive internet searches, I couldn't quite articulate my problem in words and tried multiple workarounds. To see the issue, please visit the beta ...

Step-by-step guide on incorporating Google Fonts and Material Icons into Vue 3 custom web components

While developing custom web components with Vue 3 for use in various web applications, I encountered an issue related to importing fonts and Google material icons due to the shadow-root. Currently, my workaround involves adding the stylesheet link tag to t ...

Using Three.JS to navigate a 3D cube by utilizing accelerometer data on both the x and y axes

After referencing my previous question I'm currently working on a 3D model that responds to input from accelerometer and gyroscope sensors. The 3D model is created using three.js, and the input data for rotation and translation is in JSON format. How ...

Ways to perfectly align an icon both horizontally and vertically in an <a> tag that covers the entire container

Looking to center an icon within an "a" tag that spans the entire div container? The goal is to make the entire container clickable for carousel navigation, rather than just the icon itself. Currently, I can achieve each desired effect separately. On the ...

Adjust the container within the column to match the height of the column in the bootstrap grid

Why is the green container expanding over the blue column when I apply height: 100% or h-100? Is there a way to make it fit just the remaining height of the column? <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstra ...

Tips for Updating HTML Table Content Dynamically Using JavaScript without jQuery

I am in the process of developing a web application that requires me to dynamically change the content of an HTML table based on user input, all without relying on jQuery or any other external libraries. Adding rows to the table is not an issue for me, but ...

Is there a way to include all images from a local/server directory into an array and then utilize that array variable in a different file?

I'm currently using Netbeans version 8.0.1 with PHP version 5.3 Here is a PHP file example: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/199 ...

Enhancing the aesthetic appeal of a form

I have created a form in HTML that utilizes JavaScript to pull data from a database. I am looking to style the form, but I'm unsure of how to proceed. Below is the form along with some CSS code. How can I integrate the two together? <form id=" ...