Two child elements aligned vertically must adjust their heights to fill the entire height of the parent container, with both heights being dynamic

I am doubtful that this scenario can be achieved, but I am optimistic that someone will present a solution to prove me wrong.

My parent element does not have a fixed height (though it could, although I don't believe it would help). Within this parent element, there are two children stacked vertically. The height of the first child is unknown, and I want the second child to occupy the remaining space.

Here is the HTML structure:

<div class="container">
  <a class="snippet" href="#">
    <img class="example-image" src="example-image.png">
    <div class="snippet-text-section">
      <div class="snippet-title">Title for the item - may consist of one or two lines</div>
      <div class="snippet-text">Remaining text filling the available space.</div>
    </div>
  </a>
</div>

CSS rules applied:

* {
    padding: 0;
    margin: 0;
}

.container{
    position:relative;
}

.snippet {
    display: block;
    border-radius: 5px;
    background-color: #ddd;
    color:black;
    overflow:hidden;
}

img {
    max-width: 100%;
    vertical-align: middle;
    display: inline-block;
    height:100px;

}

.example-image {
    width: 100%;
    background-color: #111;
}

.date {
    position: absolute;
    top: -20px;
    background-color: #1b1f52;
    font-weight: bold;
    padding:0px 2px;
    color: white;
}

.snippet-text-section {
    padding: 0px 20px 20px 0px;
    position: absolute;
    top: 0;
    width: 100%;
    height: 100%;
}

.snippet-title {
    margin-bottom: 0;
    font-size: 14px;
    font-weight: 700;
    background-color: rgba(255,0,0,0.8);
    color: white;
    padding: 0 5px;
    border-radius: 0px 5px 0px 0px;
}

.snippet-text {
    font-size: 20px;
    line-height: 25px;
    background-color: rgba(0,0,255,0.8);
    color: white;
    height: 100%;
    padding: 0 5px;
    border-radius: 0px 0px 5px 5px;
}

I have reviewed numerous similar posts on this topic. Most solutions discussed pertain to child divs in a horizontal layout. The closest approach I found involves setting the height of the first child, but it is not ideal.

I am considering whether a table-layout technique could offer a solution...

Update: Since it is now 2018 and CSS3 support is widespread, the recommended solution for such a scenario is using flexbox. Refer to the second part of Ktash's answer below for details.

Answer №1

There are a couple of techniques you can use to achieve this look. One simple way is to apply the overflow: hidden property to the .snippet-text-section class. This will hide any content that goes beyond the boundaries of the container. However, it may also hide desired content, so it's a bit of a trade-off.

For a more cutting-edge approach, you can utilize the flexbox property. Here's an example of how the code would appear:

.snippet-text-section {
    display: flex;
    flex-direction: column;
}
.snippet-text {
    flex: 1 1 auto;
}

You can see a live demo of this in action. While support for this method may be limited currently, it offers great potential for the future. You could check for feature support using modernizr (or your own method) and then fallback to overflow: hidden; or another solution if the browser doesn't support it.

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

Enhance user experience by implementing an autocomplete feature for a text input field

Can you name the process in which a form is automatically filled using database information without needing to refresh the page? One common example of this technique is seen on platforms like Google or Facebook, where they predict friends you may be searc ...

Incorporating the 'react-day-picker' DatePickerInput in a Bootstrap Modal Interface

I am attempting to incorporate a DatePickerInput component into a bootstrap modal. I am hoping to achieve a similar look as shown on the official website here. However, when placed inside the bootstrap modal, it appears like this. My assumption is that ...

Implement a grid layout for columns within the profile section

Each user on my website has a profile tab that displays their submitted posts. To showcase these posts, I utilize the following code: <?php while ($ultimatemember->shortcodes->loop->have_posts()) { $ultimatemember->shortcodes->loop-> ...

Issue with random pages where global header.php is not showing a specific image

I'm currently revamping a website that was originally developed using codeigniter. The backend is quite chaotic with no clear identifiers for anything. I recently updated the banner in the header.php file, adjusting the style to suit the requirements. ...

One-click process succeeds where two clicks fail

The code below is intended to go through a two-click process as follows: First click on .imagenes decreases opacity and makes .logo visible. Second click on .imagenes returns the opacity to 1 and hides .logo again. However, during this cycle of two ...

jQuery template does not respond to input text when a click event is enabled on an iPhone device

Below is a jQuery template I have: <div class="parent-class"> <div class="sub-class"> <div clas="sub-input-class"> <input type="text" /> </div> </div> </div> Recently, I ...

Preventing the use of fixed positioning on a navigation bar with the CSS filter property

I successfully created a fixed navigation bar that worked perfectly on my webpage. However, when I added a filter (brightness) to an image on the page, the navigation bar disappeared. I tried various solutions including using pseudo-elements and adjusting ...

Utilize JavaScript to trigger a gentle notification window to appear on the screen

Whenever I click a link on my HTML page, I want a popup element (just a div box) to appear above the clicked link. I have been using JavaScript for this, but I am facing an issue where the popup element appears below the link instead of above it. Can you ...

To style a checkbox with an image (such as a checkmark or X) using CSS and HTML while hovering over it

Hey there, I'm interested in playing around with HTML and CSS while learning. I have a question: is it possible to add an image to a checkbox when it's hovered over? Just to be clear, I only want the image to appear when the checkbox is being hov ...

Collapsed on Load Vertical Collapsible Panel - Initially hidden panel on page load

Is there a way to load a <div> already collapsed when the page first loads? You can find my progress so far in this JS Fidle. HTML <button id="aa">Toggle it up</button> <div id="test">TEST</div> CSS div { background ...

Tips on ensuring Material-UI Datatable adapts to varying window dimensions

I am facing an issue with the responsiveness of my MUIDatatables. The table is not adjusting properly to different window sizes. I specifically want each row in the table to be displayed on a single line and utilize horizontal scrolling. I attempted to pl ...

Changing the CSS Border of Thumbnails

Take a look at this screenshot of a thumbnail grid. I need to reposition the gray border so that it sits below the price and is consistently aligned across all products. The challenge arises when product titles vary in length, causing the price to be posit ...

What is the process of integrating SCSS into an Angular2 Seed Project?

Is there a recommended method for incorporating SCSS into an Angular2 Seed Project? Are there any informative tutorials or reference sites available? I attempted to implement SCSS following instructions from this link https://github.com/AngularClass/angu ...

What is the best way to ensure the header spans the entire width of a webpage using the display: flex; property and flex-direction: column styling within the body element

Click here for the CSS code snippet Hello everyone, this is my very first query on Stack Overflow. I know it might be a simple question but it's my first time posting here so kindly bear with me. Below is the CSS code snippet provided: *{ ...

Unable to initiate script on dynamically appended element

I am facing a similar issue as described in this post: Click event doesn't work on dynamically generated elements , however, the solution provided there did not work for me since the post is a few years old. My problem involves an image carousel that ...

What could be causing the border around my three.js canvas?

I'm utilizing a version of this code on my website and I have customized it to have a transparent background with only particles visible on the webpage. However, I've noticed a thin border around the canvas where these particles are displayed, ap ...

Using Sencha Touch: What is the best method for populating an HTML panel with a JSON response?

I've exhausted all possible combinations in my attempts to load a JSON object from the server and use it to render a panel with video and other information. Despite my efforts, I haven't been able to make anything work. What could I be doing inco ...

I'm having trouble with my scroll bar in a React project using JavaScript. Can anyone spot what might be causing the issue?

Attempting to create a React site for the first time, so please forgive any novice mistakes or oversights on my part. Currently, my navigation bar is fixed at the top of the page with basic hover animations. I am aiming for it to disappear when scrolling d ...

What could be causing my background image to not display properly?

I am struggling with applying a background image to a button on my web page. Despite using the code provided, the background image does not show up. Can someone help me understand why this is happening? Check out this demo: https://jsfiddle.net/p7octep1/ ...

Why do elements align horizontally in a div even when I specify it to display as a block?

My understanding is that display: block is the default setting. However, even after specifically setting display: block, my div was still aligning items horizontally. The only solution I found was to use: display: flex; flex-direction: column; Any insight ...