Youngsters' component with full-height being nudged by brothers and sisters

I am facing a challenge with a simple structure:

<div class="parent">
    <h1>Element taking space</h1>
    <div class="stretch">
        Not much content, but needs to be stretched to the end.
    </div>
</div>

The issue lies in the fact that the parent div has a fixed height, and I want div.stretch to expand all the way to that height, regardless of its limited content. Using height: 100% works initially, but once additional elements are added, it no longer stretches.

My understanding is that specifying height: 100% makes the element match the exact same absolute/computed height as the parent, rather than occupying the remaining space after considering other elements.

While using overflow: hidden would hide any overflow, that is not a suitable solution for me in this case.

Is there a CSS-only method to achieve this desired outcome?

Demo illustrating the issue

Answer №1

Since the original query was made, a more effective method for achieving this task has emerged: flex-box.

To implement this solution, simply set the parent element's display to "flex" and flex-direction to "column". Then adjust the height of the child element you want to stretch by setting it to "inherit". This will make the child inherit the remaining available space within its parent element.

In the code snippet below, note that lines marked with /* important */ are crucial for the functionality described; the additional CSS is included for visual clarity:

.parent {
  display: flex; /* important */
  flex-direction: column; /* important */
  height: 150px;
  border: 6px solid green;
}

h1 {
  background: blue;
  margin: 0px;
  height: 90px
}

.stretch {
  background: red;
  height: inherit; /* important */
}
<div class="parent">
    <h1>Element taking space</h1>
    <div class="stretch">
        Not much content, but needs to be stretched to the end.
    </div>
</div>

Answer №2

If you float the h1 element, it will work regardless of its height, and the content of the stretch element will automatically be pushed below it. However, I'm not entirely sure if this meets your requirements.

EDIT: If you're unsure about the browser support you need, consider setting the display to table on .parent and have .stretch inherit the height. Then nest the column divs inside .stretch and float them for a similar effect.

Updated: Check out the updated code at http://jsbin.com/oluyin/2/edit

HTML

<div class="parent">
    <h1>Element taking space</h1>
    <div class="stretch">
        <div class="col">Not much content, but needs to be stretched to the end.</div>
        <div class="col">Not much content, but needs to be stretched to the end.</div>
    </div>
</div>

CSS

.parent {
    display: table;
}

.stretch {
    height: inherit;
}

.col {
    float: left;
    width: 50%;
}

Answer №3

To auto fill the child based on the height of your H1 element, you can follow this CSS code:

.parent {
  background-color: #eee;
  border: 1px solid black; 
  width: 300px; 
  height: 600px; 
  position:relative; 
}

h1 { Height: 100px; }

.stretch {
  background-color:#dddddd; 
  position: absolute; 
  left: 0; 
  width: 100%; 
  top: 100px; 
  bottom: 0; 
}

Here's an example link to see it in action: http://jsbin.com/apocuh/1/edit

If you're unsure about the height of the H1 element, using JavaScript or thgaskell's method might be necessary.

For more details and a JS example, check out this post: CSS: height- fill out rest of div?

Answer №4

Have you considered using the display:table properties for your layout requirements?

Note: This solution may seem similar to thgaskell's answer, but instead of relying on floats, I am utilizing table-row and table-cell displays which may better suit your needs.

You can view the code snippet on jsfiddle here: http://jsbin.com/abcdef/123/edit

.container {

  background-color: #ccc;
  border: 1px solid black;
  width: 800px;
  height: 800px;
  display:table;

}

h2 {
  display:table-row;
  width:100%;
}


.block{

  vertical-align:top;
  display:table-cell;
  height:100%;
  background-color: #ddd;

}

Answer №5

HTML

<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
  <div class="container">
    <h1>Occupying space element</h1>
    <div class="stretch">Limited content but needs to stretch to fill the space.</div>
  </div>
</body>
</html>

CSS

.container {

  background-color: #eee;
  border: 1px solid black;
  width: 300px;
  height: 600px;
  position:relative;

}

.stretch {

  background-color: #ddd;
  height: 100%;
  position: absolute;  
  top: 0;
}

http://jsbin.com/amesox/1/edit

This example covers your h1 element as the .stretched overlaps it. You can workaround this by adding z-index: 1; to your h1 element, although it may affect text visibility in the .stretched element.

It is important to have position:relative; on the parent div for position:absolute to work properly. Absolutely positioned elements will cover other elements unless their z-index is higher or they are its children.

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

The Bootstrap carousel spiraled into disarray

I am facing an issue with the basic bootstrap carousel. My goal is to make the slides move every four seconds. The current setup of the carousel code is as follows: $(document).ready(function() { fixCarousel(); }); function fixCarousel() { $('.c ...

What are some solutions for troubleshooting a responsive image that functions correctly in Firefox but not in Chrome and Safari?

My row contains several img elements with height-capped sizes, displaying properly on Firefox but stretching on Chrome and Safari. Firefox Chrome Safari HTML: <div class="container-fluid text-center" style="padding-top: 15px"> <h6 class="ca ...

Changing the height of a table row using CSS transitions

I need help with a CSS table that has rows of the same height. I want to make it so that when a user clicks on one row, that row expands to fill the entire table height while the other rows fade away. I originally had this working by setting display:none o ...

Scrollable content with sticky positioning using CSS3 and JavaScript

I successfully implemented a sidebar using the position: sticky property and it is functioning perfectly. To identify colors in the following text, refer to the script below. When scrolling, the black area remains fixed while the green area sticks to its ...

Designing three div containers of equal width without any elements being obscured

I currently have three widgets, each containing text of varying lengths. My goal is to ensure that all widgets are the same length, matching the longest text length among them. Specifically, I want the blue and green widgets to extend in length to accomm ...

Is it possible to deactivate the CSS hover effect using JavaScript?

There is a parent element with an associated child element. Right now, the child element is hidden and only appears when hovering over the parent. It vanishes when the mouse moves away from the parent due to css settings. The goal is for the child elemen ...

Angular animation will be triggered when the user clicks using ng-click

In my Angular project, I'm looking to implement an animation that will enlarge the size of an image. Below is the code snippet: <div class="card"> <div class="item images-parent"> <img src="img/carryout-icon.jpg" class="left-image" ...

I encountered a SyntaxError indicating a missing ")" right before utilizing jQuery in my code

When trying to run the code below, I encountered an error in the console stating SyntaxError: missing ) after argument list. $(document).ready(function() { $(".blog-sidebar-form form").before("<p class="blog-sidebar-inform>Dummy Text</ ...

Using jQuery to toggle visibility on click within WordPress

Looking for a way to make three buttons change color on hover and display different content when clicked? You're not alone! Despite searching through tutorials and forums, the solution remains elusive. The buttons are structured like this: <div i ...

What causes the discrepancy in pixel size between these two web pages on the same device?

I am currently putting together a portfolio showcasing the projects I have worked on while learning to code. Each project page has a banner at the top, but I'm encountering an issue with one site (Jubilee Austen page) that is unresponsive. After usin ...

Is there a way to apply textTransform to all components across the board?

I need to ensure that all text in my muiv5 project is capitalized by default, unless specifically overridden using sx or individual component styling. My attempted solution: <ThemeProvider theme={theme}> <IntlProvider locale="en& ...

Need help speeding up website load times?

My website is loading incredibly slow, even though it has minimal content. I suspect that the high number of images and JavaScript elements on the page are contributing to this issue. Is there a method available to diagnose what exactly is causing the ext ...

What could be the reason that my Bootstrap design is displaying narrower than the designated container?

Having some issues with a JavaScript code that involves innerHTML, causing a discrepancy in width compared to the CSS and Bootstrap rules I have set up. Below is the JavaScript code snippet: let numeroStreams = document.getElementById('num') let ...

Why isn't the HTML template opening in Outlook 7?

Does anyone know how to fix the code issue on Outlook 7? I've only included the header section of my code here, but please let me know if you need more information. I would appreciate any help with this problem. Thanks! ...

Utilizing Local Files in a Bootstrap Video Carousel

Currently working on a website for a school project and I am trying to incorporate a carousel of videos. After finding a bootstrap carousel template that played videos linked to a server, I attempted to switch it out with local video files without succes ...

Stretch a component outside of the bootstrap container

Currently, I am working on a mockup using Bootstrap 4 and have encountered an unusual element inside the container where the background extends beyond the container. I'm not sure how to address this issue effectively. I attempted to use position: abso ...

Issues with jQuery UI droppable feature not functioning as expected

I'm currently working on developing a form building tool and I've encountered an issue. Initially, everything was functioning correctly - I could drag my first icon and drop it onto the main form body without any problems. However, when I tried t ...

Tips for centering the search popup in jqgrid

Having trouble centering the Search popup in my jqgrid. Every time I click on the search button in jqgrid, the Search popup appears at the beginning of the grid. $(document).ready(function(){ //jqGrid $("#usersList").jqGrid({ ...

Show schedule in an HTML chart

I'm currently working with a table that displays the current status of a request. An example of how the table looks in HTML is shown below: https://i.sstatic.net/daDHy.png The table itself is quite simple, but I am having trouble figuring out how to ...

Is there a way to determine the scroll direction using IntersectionObserver?

Is there a way to determine the scroll direction when an event is triggered? I've considered using the boundingClientRect in the returned object to store the last scroll position, but I'm concerned about potential performance issues. Could the ...