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

Dimensions of Titles (H1, H2 etc)

I am looking to have a unique background color for my headers from h1 through h6. The width of this background should match the width of the text in the header, along with padding. Currently, the background width is matching the container instead of the te ...

How can I show the HTML text "<ahref>" in a UITextView on iOS?

Is there a way to show HTML tags like <a> in a textview? As an example, here is some code: Hello good morning <a href=\"1\">USER1</a> and <a href=\"13\">USER2</a> ...

AngularJS Gallery Showcase

Trying to showcase a collection of videos in a modal using angular.js, I decided to implement the solution from . However, I encountered difficulty in integrating my scope variables into the "html5gallery" class. <div class="html5gallery" data-skin="ho ...

"By selecting the image, you can initiate the submission of the form

I am trying to figure out why clicking on the image in my form is triggering the form submission by default. Can someone please provide guidance on this issue? <form name="test1" action="er" method="post" onsubmit="return validateForm()" <input ty ...

An obstacle prevents the code injection process in the Chrome extension when a button is pressed

My basic chrome extension is not functioning correctly. More specifically, I am encountering an issue where the alert box does not appear when I click on the button in the popup. Here are the contents of my manifest.json file: {"manifest_version": 2, "n ...

Issue with background image resizing when touched on mobile Chrome due to background-size property set to cover

My current challenge involves setting a background image for a mobile page using a new image specifically designed for mobile devices. The image is set with the property background-size: cover, and it works perfectly on all platforms except for mobile Chro ...

What steps can I take to troubleshoot and fix the height of a div/span element?

Here is my HTML code: <div> <span style="display: inline-block;height:20px">20</span> <span style="display: inline-block;"><img src="img/ex.png"/></span> </div> The image I have in the second span is sized at ...

What is the proper location for inserting image copy code within my codebase?

I encountered an issue with using this code: copy('imgurl', 'images/covers/file.jpeg'); The code successfully copies an image URL to a file on my website when placed in a PHP page alone, but it fails to work within my actual code. He ...

What is the process for incorporating a new task into my HTML/CSS/JavaScript to-do list application when the Enter key is pressed?

Currently, I am working on developing a to-do list application using HTML, CSS, and JavaScript. The application includes a text input field for users to enter their tasks, along with an "Add Task" button. However, I want to enhance the user experience by a ...

Is there a way to horizontally align the content in my footer section?

I am currently exploring React Material UI for the first time. The table I am working with can be found here under the "Custom pagination actions" section. Within this section, there is footer content containing buttons for rows per page, next, previous, ...

Displaying server errors in an Angular componentIn this tutorial, we

As I work on creating a registration page, my focus has been on posting data to the server. I have successfully implemented client-side and server-side validation mechanisms. Managing client-side errors is straightforward using code such as *ngIf="(emailAd ...

How can we reduce the use of !important in SCSS when working with React and Material UI?

In my current project, I am developing a Select component in React with Material UI. The CSS styling for the component is managed through an external SCSS sheet imported into the script file. While working on restyling the component, I found it challengin ...

Only send file input to the server when it is changed, and disregard any other inputs until the form is submitted

I am working with a form structured like this: <form method="post" action=""> <input type="text" onchange="uploadFile()" name="username"> <textarea name="description">< ...

Getting started with CSS and HTML: Tips and Tricks for Beginners

Seeking advice on how to enhance my web designing skills, particularly in starting point and techniques. Currently, I am familiar with HTML and CSS, but have been primarily using pre-made templates for building websites. I aspire to be able to transform ...

What are the reasons for not utilizing JSS to load Roboto font in material-ui library?

Why does Material-UI rely on the "typeface-roboto" CSS module to load the Roboto font, even though they typically use JSS for styling components? Does this mix of JSS and CSS contradict their usual approach? ...

What is the process for creating a login page that redirects automatically?

For instance: Whenever I enter gmail.com, it automatically takes me to this link: https://accounts.google.com/signin/v2/identifier?service=mail&passive=true&rm=false&continue=https%3A%2F%2Fmail.google.com%2Fmail%2F&ss=1&scc=1&ltmp ...

Creating a custom HTML table layout with both variable and fixed column widths, along with grouping headers

Is there a way to format an HTML table so that it appears like this: <- full page width -> <-20px->< dynamic ><-20px->< dynamic > +------------------+-------------------+ ¦ A ¦ B ...

Is there a way to display a vertical list in a horizontal orientation?

I am in the process of creating my portfolio on Cargo Collective and have customized one of their pre-made themes to suit my preferences. The only thing I'm currently struggling with is how to change a set of vertical links to display horizontally ins ...

Tips on identifying HTML email input validation using JavaScript?

Just like when you can determine whether an input element with a required attribute was successfully validated, try using the following code: if($('input[type="email"]').val() && $('input[type="email"]').val().includes('@') & ...

Ways to showcase HTML table in a four-column layout/grid

Delving into Dynamic HTML table creation, I'm currently using jquery for rendering purposes. At the moment, I am only displaying the table. The Goal I aim to segment my table into four columns or a grid structure Something akin to this: https://i. ...