The max-height property is not applied to a border-box div with padding

One technique we use is the percentage trick on paddings to maintain aspect ratio in a div as the user scales their window. Here's an example:

.div {
    background: red;
    width: 80%;
    margin: 0 auto 10px;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
    padding-bottom: 20%;
}

Now, we want to set a maximum height for this div. Since the height of the div is determined by the padding and we need the div to be border-boxed, everything seems fine up to this point. But when attempting to use a max-height on the div, it doesn't work as expected.

.div {
    max-height: 60px;
}

I've created a fiddle to demonstrate the issue: http://jsfiddle.net/UxuEB/3/.

I've tested this on Chrome, Firefox, and Internet Explorer. Can someone please help me understand what I'm doing wrong or why this isn't working as intended?

Answer №1

Although I am quite late in providing this answer, I found myself dealing with the same issue today and stumbled upon this question as the first result on Google. Below is the code that I used to solve it, which may be helpful for someone facing a similar challenge in the future.

To begin, include an additional inner div:

<div class="control control-max-height">
  <div class="control-max-height-inner">
    Max-height
  </div>
</div>

Adjust the padding of the inner div while hiding the overflow of the outer div like so:

.control {
    background: red;
    width: 80%;
    margin: 0 auto 10px;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
}

.control-max-height {
    max-height: 120px;
    overflow: hidden;
}

.control-max-height-inner {
    padding-bottom: 20%;
}

It should be noted that this method assumes you are comfortable with concealing part of the inner element when it overflows. In my scenario, this was not an issue as the inner element only contained an empty link to enable the entire section to be clicked, while the outer element featured a centered background image and a border.

For a visual representation, please refer to the fiddle: http://jsfiddle.net/UxuEB/7/

Answer №2

When using the max-height property, it affects the element's height but you might also want to apply it to both the height and padding-bottom.

Some may confuse this with the box-sizing property which includes the padding in the overall height of the element (I was one of them too). However, as demonstrated in the jsFiddle example, this is not the case.

For instance:

  • If the element's content has a height of 100px.
  • Setting the max-height to
    50px</code</li>
    <li>Then adding a <code>padding-bottom
    of 100px (more than the element's original height).
  • The total height of the element will be 150px, including the added padding.

Check out the JsFiddle example for a visual demonstration: here

Answer №3

Taking inspiration from Mike's solution in his post, you can accomplish the same effect using just one DOM element and a pseudo element, like so:

Here is the HTML:

<div class="new-div"></div>

And here is the CSS:

div.new-div {
  max-height: 200px;
}

div.new-div::before {
  content: '';
  display: block;
  padding-bottom: 60%;
}

Answer №4

The min-height CSS property is used to specify the minimum height of an element when its height is determined by padding alone, whereas max-height does not have this restriction.

Interestingly, in 2020, the min and max CSS units have become quite useful for achieving desired outcomes.

.classthatshoulddefineheight {
   padding-bottom: min(20%, 60px); 
}

Therefore, if 20% results in a height greater than 60px, the height will be capped at 60px (taking the minimum value between them).

Answer №5

One potential drawback of Mike's solution (as well as Brad's approach, although Brad's method can help reduce the number of container levels) is its reliance on overflow: hidden - a limitation that may not be suitable for certain use cases.

To address this issue, I have modified the example by introducing an additional layer and utilizing absolute positioning instead of overflow: hidden.

http://jsfiddle.net/8saj91dh/1/

The key is to insert another container within the inner box, set it to position:absolute, and apply max-height to this new container as well:

.inner-inner {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  max-height: 150px;
}

By adding some extra DOM elements, this revised approach should function effectively across various scenarios in most modern browsers.

Answer №6

To fix the issue, you can use the CSS property display: flow-root; on the main container element.

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

Align text to the right and do not include any images

I am attempting to align the images back to the left under the title, while only floating the description text to the right. I am struggling with clearing the image floats. HTML: <div class="title">System Displays</div> <div class="descrip ...

Tips for accessing a DOM element's ::before content using JavaScript

Is there a way to retrieve the content of a DOM element's ::before pseudo-element that was applied using CSS3? I've attempted several methods without success, and I'm feeling very confused! // https://rollbar.com/docs/ const links = docum ...

The transition property in CSS and JavaScript does not seem to be functioning properly in Firefox

Recently, I integrated a Slide in menu script on my website using a Slide in menu script. After following all the instructions provided, the menu started working flawlessly on Chrome and Safari browsers. However, my main goal was to make it function on Fir ...

Take a snapshot of an element using fixed element positioning

When it comes to extracting a sub image from a webpage using Selenium, I have found a reliable method. First, I capture a screenshot of the entire page and then extract the desired sub-image using the element's coordinates. Dimension elementDimension ...

Tips for ensuring Opera browser compatibility with buttons using the CSS background image property

Is there a way to make background images work on buttons in Opera web browser using CSS? I've tried using the background image property, but it doesn't show up when I use Opera. Strangely enough, it works fine in Firefox. However, I have noticed ...

Adjustable dimensions and proportions for the following image

I'm currently working on a blog page using next.js and I've encountered an issue with displaying images. Each post has a main image of varying dimensions and aspect ratios, making it difficult to maintain consistency when scaling for smaller scre ...

JQuery functions for click and hover are not functioning as expected on a div element

I am having trouble with a div that I have declared. The click event is not working as expected, and I also need to use the :hover event in the css, but it isn't functioning either. What could be causing this issue? <div id="info-button" class="in ...

Enhancing this testimonial slider with captivating animations

I have designed a testimonial slider with CSS3 and now I am looking to enhance it by adding some animation using Jquery. However, I am not sure how to integrate Jquery with this slider or which plugins would work best for this purpose. Can anyone provide g ...

Background image color not displaying correctly

I've been attempting to overlay a transparent color on top of a background image. While the background image is displaying correctly, I'm having trouble getting the color to show up. I noticed that this question has been asked before. I tried im ...

Display a snippet of each employee's biography along with a "Read More" button that will expand the content using Bootstrap, allowing users to learn more about each team

I have successfully developed a Google App Script that links to a Google Sheet serving as a database. The application iterates through each row, and I showcase each column as part of an employee bio page entry. ex. Picture | Name | Title | Phone | Email ...

What is the impact of using overflow: hidden on block elements?

HTML <div class="top"><p>hello</p></div> <div class="bottom"><p>hello</p></div> CSS .bottom { overflow: hidden; } While delving into the world of CSS, I encountered an interesting qu ...

Combining data using D3's bar grouping feature

I'm currently exploring ways to group 2 bars together with some spacing between them using D3. Is there a method to achieve this within D3? I found inspiration from this example. var data = [4, 8, 15, 16, 23, 42]; var x = d3.scale.linear() .doma ...

Utilizing a Single Background Image Across Several Div Elements

Let me illustrate my question using a sequence of images. The div container is set to display the background image as follows: In addition, there will be tile-shaped divs layered on top of the image: I aim to have the background image visible only withi ...

Is it possible to rotate a group within an SVG without altering the orientation of the gradient fill it contains?

In my search on SO, I came across a lot of information about rotating gradients. However, my issue is the opposite. I have an icon created from multiple paths that I want to fill with a vertical gradient. I have successfully done this and it works well. ...

Is it possible to use jQuery to search an unordered list by simply pressing a key on the keyboard?

I have a list of countries displayed as an unordered list inside a table column. I would like to implement keyboard search functionality for quicker navigation within the list. <ul class="country-search"> <li id="1">Country 1</li> ...

Show the nested div when hovering over the containing div using JavaScript

I have a situation where I have multiple divs within a parent div, all using the same class. Here is an example: <div class="deck-content"> <div class="deck-box">TEST< <div class="deck-hidden">< <span class= ...

Other options besides re-flowing and repainting

After doing some research on various platforms like Stack Overflow, I've come across information stating that re-paints and re-flows can be quite taxing on a browser's resources. I am interested in learning about alternative CSS/JS techniques to ...

Tips for creating a dynamic text that adjusts to different screen sizes within a bootstrap button

When utilizing the w3.css and bootstrap libraries to add a link button, an issue arises when resizing the screen as the text inside the button does not respond accordingly. How can we ensure that the text fits within the button based on the screen resoluti ...

Incorporate a Font Awesome icon into a Bootstrap 4 callout

I am having issues adding an icon to the left side of a Bootstrap 4 callout. Currently, the icon is appearing above the H4 heading. I would like the icon to be positioned on the left side before the message. I have attempted using .p:last-child but it doe ...

Issues with Bootstrap column rendering in IE11's Flex layout

I need assistance with the code below: .hero{ background:lightgrey; padding: 50px 0px; } <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E26 ...