Step-by-step guide on achieving 100% height for a child element within a parent using Flexbox

Currently facing an issue where trying to make a child element take up 100% of the remaining height of its parent container causes the child's height to go beyond the boundaries of the parent.

HTML

.container {
  height: 340px;
  /* background-image: url(../images/topo-bg-3-black.png);
  background-position: center;
  background-size: cover;
  background-repeat: no-repeat; */
  background-color: #DDDEDA;
  display: flex;
  align-items: center;
  justify-content: center;
}

.container-wrap {
height: 200px;
display: flex;
align-items: center;
justify-content: space-evenly;
}

.content {
height: 100%;
width: 171px;
background-color: aqua;
}

.content-pic {
 width: 100%;
height: 115px;
background-color: green;
margin-top: 5px;
}

.content-text {
background-color: red;
height: 100%;
width: 100%;
}
<div class="container">
  <div class="container-wrap">
    <div class="content">
    5 HOURS AGO
        <div class="content-pic"></div>
        <div class="content-text"></div>
    </div>
  </div>
</div>

The code snippet can be found in this JSFiddle link.

I've tried using align-items: stretch; and other properties like position, as suggested in various forums, but still couldn't resolve the issue. The child element occupies 100% of the container's width correctly, but the height seems to be causing trouble, and I'm unsure about how to proceed.

Answer №1

I have come up with two solutions for you.
Firstly, to hide the overflow part of the element, you can use overflow: hidden like this:

.content {
height: 100%;
width: 171px;
background-color: aqua;
overflow: hidden;
}

The second solution involves addressing the issue completely. The height value for content-pic is set to 115px, leading to an overflow problem. Here's how you can fix it:

  .content-pic {
   width: 100%;
  height: 115px;
  background-color: green;
  margin-top: 5px;
  }
  
  .content-text {
  background-color: red;
  height: calc(100ph - 115px);
  width: 100%;

  }

The initial overflow was caused by having one element at 115px and another at 100%, resulting in a mismatch. By adjusting the width using calc(100ph - 115px), this issue can be resolved. Make sure to utilize span instead of nothing to avoid potential issues.


Check out the complete code snippet below (for the second solution):

.container {
    height: 340px;
    background-color: #DDDEDA;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  .container-wrap {
  height: 200px;
  display: flex;
  align-items: center;
  justify-content: space-evenly;
  }
  
  .content {
  height: 100%;
  width: 171px;
  background-color: aqua;

  }
  
  .content-pic {
   width: 100%;
  height: 115px;
  background-color: green;
  margin-top: 5px;
  }
  
  .content-text {
  background-color: red;
  height: calc(100% - 115px);
  width: 100%;

  }
<div class="container">
  <div class="container-wrap">
    <div class="content">
    <span>
      5 HOURS AGO
      </span>
        <div class="content-pic"></div>
        <div class="content-text"></div>
    </div>
  </div>
</div>

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

Develop a JavaScript function to declare variables

I am currently attempting to develop a small memory game where the time is multiplied by the number of moves made by the player. Upon completion of all pairs, a JavaScript function is executed: function finish() { stopCount(); var cnt1 = $("#cou ...

Retrieve input value in Angular 8 using only the element's ID

Can the value of an input be obtained in Angular 8 with TypeScript if only the element's id is known? ...

Receiving array data in a Javascript function and storing it within a variable

Hello everyone, please take a look at my code below. I am attempting to pass PHP array values to a JavaScript function. When I run the script, I receive alerts for parameter0=1, parameter1=2, and parameter2=3 separately. What I am trying to achieve is to ...

Integrating Twitter bootstrap into Django version 1.5

I have been attempting to incorporate Twitter bootstrap into my django application. Within settings.py, I have set up the following: STATIC_URL = '/static/' # Additional locations of static files STATICFILES_DIRS = ( # Add paths for static ...

Simultaneously sending jQuery.ajax data while submitting a form

I'm facing a bit of a dilemma here. I have a form with multiple fields, including one for entering links. When you input a link and click the add button, the link is added to a link_array using jQuery. My goal is to send this array through the jQuery. ...

Creating responsive images using Bootstrap CSS framework

I have been focusing on optimizing the banner below the navigation menu on this website. My goal is to make it responsive so that the player in the banner remains visible even when scaling down to iPhone size portrait. You can find the code for this secti ...

Alignment problem in CSS, identical code on two separate servers

After transitioning from the design phase to development, I encountered a strange CSS issue on one small section of my client's website. Specifically, in the "Official NGK Canada Part Finder" section, the text is breaking onto new lines unexpectedly. ...

Execute the script when the document is fully loaded

Is there a way to show a dialog in jQuery when the document loads without using <body onload="showdialog();">? Can the javascript code be placed in the main div or footer div to work like the onload event? <body onload="$('#dialog').sli ...

Is there a method to instruct angular-cli (for angular 2) to produce a minified version of css files?

When running "ng serve" in angular-cli, the generated css is not minified as expected. Is there a specific setting to configure in angular-cli-build or an additional plugin to install? This is the content of my angular-cli-build.js file: var Angular2App ...

Enabling a JSON file property to be clickable as needed

I am working with a JSON file obtained from an API call, which contains various objects. My goal is to display the message property of each object, some of which may contain hyperlinks within the message. Here is the HTML code I have implemented to make t ...

What is the method for identifying the name of a CSS Variable currently in use?

Consider this code snippet: /* css */ :root { --text-color: #666666; } input { color: var(--text-color); } In Javascript, how can I determine the name of the CSS custom property (variable) being utilized? // javascript console.log(document.querySel ...

I incorporated a CSS file from another HTML document. What do you think about that?

In my work with Ionic 3 and angular 4, each HTML file is accompanied by its own CSS file. There are times when I reference a class in one HTML file that is defined in another HTML file. I have observed that this CSS class gets injected into the main.js He ...

The CSS variables set within the :root section of styles.scss are not recognized as valid

Trying to implement global colors using CSS variables in my Angular 11 app. The styles.scss file contains: :root{ --primary : #0b68e8; --secondary:#ABCFFF; } .test-class{ background: var(--primary); } However, when applying this class in one ...

The connection to Cordova.js is established, but the deviceready event is not

I've included cordova.js in my project, called app.initialize();, but for some reason deviceready event is not being triggered. Any ideas on what might be causing this issue? Here's the JavaScript code: var app = { initialize: function() { ...

An Ajax GET request will not be able to locate the JSON file

I am having issues retrieving Key and Values from a JSON file using the function provided. Despite placing the 'datafile.json' in the same directory, the code fails to execute the alert(weblink) function, while the alert('test 1') works ...

Resources for ExpressJS

New to ExpressJS and trying to figure out how to reference images stored in the /public/images/ directory in my /public/stylesheets/styles.css The code snippet below doesn't seem to be working for me: .home-bg { background-image:url('../ima ...

Tips for aligning my icon in the middle when I collapse my sidebar

Seeking assistance on centering the hamburger icon on my sidebar. Currently, when I adjust the width of the sidebar, the icon is not centered at all. Does anyone have a solution for achieving this? I attempted using justify-content: center in the main clas ...

Retrieving information received from an XML HTTP request in node.js

Currently, I am transmitting text data from a textbox to a node.js express server using XMLHttpRequest: var text = document.getElementById("textBox").value; console.log(text); var xmlhttp; if (window.XMLHttpRequest) {// code for IE7+, Fire ...

Having trouble navigating to the bottom of a VUEJS app?

I've been working on developing a chatbot app that utilizes a REST API to stream content. While the functionality of the app is running smoothly, I have encountered an issue with the scroll to bottom feature. Instead of automatically scrolling to disp ...

Minimize all expanded containers in React

Although I've come across similar questions, none of them have addressed mine directly. I managed to create a collapsible div component that expands itself upon click. However, I am looking for a way to make it so that when one div is expanded, all o ...