CSS: The calc() function does not take into account the bottom padding when used in Firefox and Internet

I am encountering an issue with the calc() function in Firefox 32 and IE 11, where the bottom padding of a div is not being respected. However, this problem does not occur in Chrome and Opera.

The main content section should have a fixed height while allowing its contents to be scrollable.

Below is a snippet of my HTML code:

<body>
<div class="main-content">
  <div class="header">header</div>
  <div class="content">
    content
    <div class="long-content">
      [long content with breaks]
      <button type="button">Button</button>
    </div>
  </div>
</div>

and here is the CSS used:

.main-content {
  width: 100%;
  background-color: #595959;
  height: 300px;
}

.header {
  background-color: #000000;
  height: 50px;
}

.content {
  background-color: red;
  padding: 15px;
  height: calc(100% - 50px);
  overflow: auto;
  position: relative;
}

Any suggestions on how I could resolve this issue?

DEMO

Answer №1

The issue at hand is that Chrome interprets the specifications differently than Firefox and Internet Explorer. It appears there is some ambiguity as to who is correct, as seen in this answer and in Bug 748518 - padding-bottom is ignored with overflow:auto; bug report.

If you prefer not to use an inner container, one workaround is to add an element after the content using the :after pseudo class and the content property.

.content:after {
  content: "";
  display: block;
  height: 20px;
  width: 100%;
}

.content {
  background-color: red;
  padding-top: 20px;
  padding-left: 20px;
  height: calc(100% - 50px);
  overflow: auto;
  position: relative;
}

View the demo on jsfiddle

Make sure to test for browser compatibility when implementing this solution, but since you are already using calc this should work fine.

This approach will address the padding at the bottom, and there may be a similar solution for the right side padding as well, although if your focus is only on the bottom padding I have not explored that scenario.

Answer №2

When the calc() function is enclosed in quotes like this - height: calc('100% - 50px'); - it actually works as intended.

You can check out an example here - http://jsfiddle.net/8s5fd64h/1/

Answer №3

The additional space is applied to the overall height of the container. This means that the total height of .content will be calculated as 100% - 50px + 15px + 15px. To keep the padding contained within the specified height, you can use the CSS property box-sizing: border-box.


.content {
  -webkit-box-sizing: border-box; /* Supported by Safari/Chrome and other WebKit browsers */
  -moz-box-sizing: border-box;    /* Supported by Firefox and other Gecko browsers */
  box-sizing: border-box;         /* Supported by Opera and IE version 8 and above */
}

For more detailed information, visit this link

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

Creating a Navigation Bar with a Dropdown Menu in HTML

Apologies if this question has been asked before, but I've searched everywhere and can't find an answer. All my searches just bring up dropdown menus with <nav></nav> Consider this table (serving as a navigation bar): <table cl ...

Please click the provided link to display the data in the div. Unfortunately, the link disappearance feature is currently not

What I'm Looking For I want the data to be displayed when a user clicks on a link. The link should disappear after it's clicked. Code Attempt <a href="javascript:void(0);" class="viewdetail more" style="color:#8989D3!important;">vi ...

How can I retrieve a PDF from a URL provided by the server using AngularJS?

Is it possible to automatically download a PDF file from a URL received through a $http GET() request from the server? The $http request is triggered by clicking a button on the front-end, and upon successful completion of the request, the URL where the PD ...

Getting the result from HTML5 FileReader: How does it work?

Dealing with the asynchronous nature of JS FileReader can be challenging. I need to retrieve the result after reading a file and work with that data, but I don't know when the result will be ready. How can I handle this situation effectively? $(docum ...

Is it possible to make changes to local storage data without impacting the rest of the data set?

I am looking for a way to modify specific data in the local storage without affecting any other stored information. However, I have encountered an issue where editing values works correctly for the first three attempts, but on the fourth try, it seems to ...

Navigate to the following div, navigate back to the previous div

I am attempting to implement a div navigation system with next/previous buttons. Despite searching extensively on Google, I have not found the exact solution I am looking for. First and foremost, I want to maintain the integrity of my html structure. < ...

Out of reach: Menu div slides up on click outside

<a class="btn">Click ME </a> <div class="content"> Content </div> $('.btn').click(function(){ $('.content').slideToggle(); }); Check out the example here Everything is working properly, but I have a q ...

Tips to position a div directly on top of table cells using absolute positioning

I have a div inside the second table cell that I want to position absolutely right outside the <td>. Currently, the issue is that the <td> elements below are overlapping the div. Eventually, each <td> will contain a div that appears on ro ...

Tips for making an appended element match the height of an image

After embedding the div .soonOverlay into specific .smallCatalogBlock's, I am facing difficulty in adjusting the height of soonOverlay to match only the height of the img within smallCatalogBlock. Currently, its height extends throughout the entire co ...

I am seeking to enable a specific div when the crawler condition is met

This specific div houses the character and becomes active when the character is clicked. Upon clicking, a jQuery function is triggered to display other countries. //this particular div should remain active when the crawler condition is met. It displays th ...

Receiving an error of "undefined" when trying to capture the selected

One issue I am facing is capturing the selected user option and sending that value in a post request. Let's put aside the post part since it's not directly related to the main question at hand. Currently, the value is showing up as undefined. ...

When using jQuery, hover over an li element while ignoring its children

I'm working on customizing a Wordpress menu that has a submenu. I managed to add an arrow to the 'li' element that contains a submenu, and created a hover animation with CSS when the mouse moves over the 'a' tag inside the 'li ...

HTML- Any suggestions on how to troubleshoot my sticky navbar not functioning properly?

I'm having trouble creating a sticky navbar. I've attempted to use z-index: 999 but it's not behaving as expected. * { margin: 0; padding: 0; } .navbar { display: flex; align-items: center; justify-items: center; position: ...

Tabulator and its convenient scrollable column feature allows for easy navigation

In case my tabulator column is exceeding its length, is there a way to enable scroll functionality for that specific column? Although the resizable rows feature permits users to resize and view all the content, can a scrollbar be implemented exclusively ...

Making a page jump with Javascript

Welcome! Let's dive into our question. Below you'll find my script and HTML code: My Script: const resultEl = document.querySelector('.allquotes'); const pageSize = document.querySelector('select[name="page-size"]') ...

Count duplicated values in an array of objects using JavaScript ES6

I am working on creating a filter for my list of products to count all producers and display them as follows: Apple (3) I have managed to eliminate duplicates from the array: ["Apple", "Apple", "Apple"] using this helpful link: Get all non-unique values ...

Manipulating HTML content with Javascript Array

Is there a way to retain Javascript context from an array? Any suggestions for improving this code? SAMPLE: <html> <script type="text/javascript"> var rand = Math.floor(Math.random() * 6) + 1; var i = Math.floor(Math.random() * 6) + 1; var ...

Creating a centered arrow using CSS within boxes

Here is the CSS class that I have applied to my divs: .rightarrow{ width: 0; height: 0; border-style: solid; border-width: 10px 0 10px 20px; border-color: transparent transparent transparent #aaaaa9; } This is ...

I'd love some clarity on the concept of stacking contexts

After encountering a bug with a non-clickable video player inside a jquery ui dialog, I resolved the issue by changing position:relative; to position:inherit; Other solutions included removing position:relative; altogether or adjusting the z-index of the ...

Is tabIndex being used as a css property?

I'm trying to understand how to utilize the HTML attribute tabindex using CSS. Specifically, I want to assign tabIndex=0 to all div elements that have the className='my-checkbox'. This is my current approach: <div tabIndex="0" classNam ...