The unequal division of space between two flexed child divs is caused by varying image sizes

In my parent container, I have set the display property to flex. Inside, there are two divs with both having flex:1 set. However, the first child div contains an image and takes up twice as much space as the second div.

If the screen is 1000px in height, shouldn't each cover 500px in height?

Everything works as expected when the flex-direction is set to 'row', but with 'column', the divs take up infinite vertical space and adjust their heights as needed.

I want both child divs to evenly cover the remaining visible screen height without extending the height of their parent container vertically.

#info-container {
  margin: 0 auto;
  margin-top: 5%;
  text-shadow: 0 2px 2px #000;
  display: flex;
  flex-direction: column;
}

#info-container .cover {
  text-align: center;
  flex: 1;
}

#info-container .cover img {
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 2%;
  box-shadow: 0px 0px 5px #000;
  max-width: 100%;
}

#info-container .song-info {
  align-self: center;
  color: rgba(255, 255, 255, 0.8);
  font-size: 18px;
  flex: 1;
}

#info-container .song-info h4 {
  margin-top: 10px;
  font-weight: 100;
  // text-decoration: underline;
  padding-bottom: 5px;
  border-bottom: 1px solid white;
  width: 100%;
}

// suggested songs
.suggested-songs {
  margin-top: 5%;
}
<div id="info-container">
  <div class="cover"><img class='cover-photo' src='https://images.unsplash.com/photo-1523895665936-7bfe172b757d?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&w=1000&q=80' alt="cover">
  </div>
  <div class="song-info">
    <h4>Test : <span class="song-name"> Test</span>
    </h4>
    <h4>Test : <span class="artist">Test</span></h4>
    <h4>Duration : <span class="duration">3:26</span></h4>
  </div>
</div>

codepen : https://codepen.io/AfaqQazi/pen/QWLaybo?editors=1100

Answer №1

The flex property only establishes the minimum height for a FlexBox item. Due to the image tag being taller than the text content, it causes the first div to become taller. If you wish to make the divs the same height, you have two options.

First: Specify a max-height for the img tag:

#info-container {
  margin: 0 auto;
  /*margin-top: 5%;*/
  text-shadow: 0 2px 2px #000;
  display: flex;
  flex-direction: column;
}

#info-container .cover {
  text-align: center;
  flex: 1;
}

#info-container .cover img {
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 2%;
  box-shadow: 0px 0px 5px #000;
  max-width: 100%;
  max-height: 200px;
}

#info-container .song-info {
  align-self: center;
  color: rgba(255, 255, 255, 0.8);
  font-size: 18px;
  flex: 1;
}

#info-container .song-info h4 {
  margin-top: 10px;
  font-weight: 100;
  // text-decoration: underline;
  padding-bottom: 5px;
  border-bottom: 1px solid white;
  width: 100%;
}

// suggested songs
.suggested-songs {
  margin-top: 5%;
}
<div id="info-container">
  <div class="cover">
    <img class='cover-photo' src='https://images.unsplash.com/photo-1523895665936-7bfe172b757d?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&w=1000&q=80' alt="cover">
  </div>
  <div class="song-info">
    <h4>Test : <span class="song-name"> Test</span></h4>
    <h4>Test : <span class="artist">Test</span></h4>
    <h4>Duration : <span class="duration">3:26</span></h4>
  </div>
</div>

Second: Convert the image into a background image, specify a min height, and set all parent elements to 100% height:

html, body, #info-container{
   height: 100%;
}

#info-container {
  margin: 0 auto;
  /*margin-top: 5%;*/
  text-shadow: 0 2px 2px #000;
  display: flex;
  flex-direction: column;
}

#info-container .cover {
  text-align: center;
  flex: 1;
  background: url(https://images.unsplash.com/photo-1523895665936-7bfe172b757d?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&w=1000&q=80) center/cover;
  min-height: 300px;
}

/*#info-container .cover img {
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 2%;
  box-shadow: 0px 0px 5px #000;
  max-width: 100%;
}*/

#info-container .song-info {
  align-self: center;
  color: rgba(255, 255, 255, 0.8);
  font-size: 18px;
  flex: 1;
}

#info-container .song-info h4 {
  margin-top: 10px;
  font-weight: 100;
  // text-decoration: underline;
  padding-bottom: 5px;
  border-bottom: 1px solid white;
  width: 100%;
}

// suggested songs
.suggested-songs {
  margin-top: 5%;
}
<div id="info-container">
  <div class="cover">
    <!--<img class='cover-photo' src='https://images.unsplash.com/photo-1523895665936-7bfe172b757d?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&w=1000&q=80' alt="cover">-->
  </div>
  <div class="song-info">
    <h4>Test : <span class="song-name"> Test</span></h4>
    <h4>Test : <span class="artist">Test</span></h4>
    <h4>Duration : <span class="duration">3:26</span></h4>
  </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

Adding images in real-time

I am currently working on an Angular application where I need to assign unique images to each button. Here is the HTML code snippet: <div *ngFor="let item of myItems"> <button class="custom-button"><img src="../../assets/img/flower.png ...

The use of "runat="server" in the HTML Head tag is preventing C# embedded code blocks from being converted to client-side code

After I included runat="server" in the head tag and added a CSS href with the value of <%= WebAppInstance %>, I noticed that the <%= WebAppInstance %> was not being converted to client-side code. To provide clarity on my question, please refer ...

Tips for ensuring v-tabs-items and v-tab-item fill height

I found an example that I am trying to follow at the following link: https://vuetifyjs.com/en/components/tabs#content <v-tabs-items v-model="model"> <v-tab-item v-for="i in 3" :key="i" :value="`tab-${i}`" > < ...

Using a scrapy spider to navigate through microsoft.com in search of the sign-in link

I have encountered an issue while trying to locate the sign-in link using a scrapy crawler on various websites, such as www.microsoft.com. Initially, the response I receive from the website does not contain the sign-in link. However, upon inspecting the "V ...

Steps for implementing Onclick event within the <Script> tag to display a div:

I am currently working on a code that allows me to show and hide a <div> element after clicking on an advertisement from any website. This advertisement is generated by a script. How can I implement the onclick event within the <script> tag? T ...

Obtain data from a local JSON file using the http.get() method in Angular 2

I am attempting to utilize the http.get() method in Angular 2 to load a local JSON file. I followed a suggestion from Stack Overflow that involves modifying my app.module.ts: In this example, I have imported the HttpModule and the JsonModule from @angular ...

Access the original source code using jQuery

Is there a way to retrieve the raw HTML code (as seen in Chrome's source code window) using JQuery without having quotes converted to &quot or HTML symbols converted to text? I have tried using html() and text(), but neither method seemed to give ...

Troubleshooting Vue.js rendering problems on Safari_BROWSER

I'm encountering a strange issue with my portfolio website, which is built using Vue and Laravel. The problem arises specifically in Safari - the project thumbnails don't display until the browser window is resized. Here's the relevant code ...

What is the best way to conceal part of a chart or a div in Angular?

I have a large chart that I need to showcase on my website, but due to its size it would take up too much vertical space and potentially overwhelm the user upon their first visit. My goal is to provide a preview of the chart, similar to an excerpt of text ...

``Fixing the focus background color for mobile devices and iPads using React and Material io: A Step-by-Step

My custom button has a background and a :hover color. It works fine on the web, but on mobile devices, the color of the :hover persists even after clicking the button when the mouse is not over it. I am looking for a solution to fix this issue because the ...

Setting the height to 100% on the body and html tags can lead to scrolling problems

After implementing the CSS code below, I have encountered some unexpected behavior: body, html{height:100%; overflow-x:hidden} Despite vertical scrollbars appearing as intended when the page exceeds screen height, detecting the scroll event on the body e ...

jQuery Counter Effect encountering isNaN error when trying to display a number retrieved from a get API object

I am looking to display the numerical data retrieved from an API using JSON. I want to incorporate a counter effect that displays "isNaN" if necessary. The API URL returns an object with the total number saved in data.data. Can anyone assist me with achi ...

Generating an HTML hyperlink to trigger a button click event on another page

After implementing the code snippet below, I successfully managed to create a stylish Link with parameters that directs users to a specific page: <style> .fancy-link{ color: #FFFFFF; text-decoration: none; transition: ...

Instructions for utilizing float:left and list-style-type within HTML are as follows: I am currently experiencing issues with implementing the float:left and list-style-type properties in my code

I'm attempting to align a button list to the left using the float: left property and also remove list styles, but for some reason it's not working as expected. //CSS #tus{margin:5px;padding:0;width:640px;height:auto;} #tus ul{margin:0px;padding: ...

Utilizing HTML's multiple input type color feature to save selected colors directly to the database

I am currently using the input type color to select colors for customizing my site. I save the selected color to a database and then retrieve it to apply it to different areas of the site. This works well for a single input type color, but now I have mul ...

The CSS background-image fade transition is optimized for Chrome browsers

HTML: <a class="navbar-brand page-scroll" href="#intro"></a> CSS: .navbar-custom .nav li a.navbar-brand { width: 70px; height: 62px; background: url(myimg.png) no-repeat; background-size: 70px 62px; display: block; po ...

Customizing alert message styles in Java script: Changing color and font of specific parts

I have been attempting to change a specific part of text to be bold and displayed in red color. Unfortunately, this does not seem to work on Microsoft Edge. Here is my code: alert("Hi how are you my friend"); The section that needs to be formatted: "fri ...

Reopen a Kendo UI dialog

Currently, I am utilizing Kendo UI, and my goal is to display a modal dialog when a button is clicked. The issue I am facing is that it works perfectly the first time around. However, upon closing the dialog and attempting to reopen it by clicking the butt ...

Is it possible for the HTML data attribute to store a direct link to a specific DOM element?

Can the HTML data- attributes be used to store a reference to another DOM element? As shown in this jQuery example: var domel1 = document.getElementById("#mydiv"); var domel2 = document.getElementById("#mydiv2"); $(domEl1).attr('data-domel', dom ...

Conceal and reveal elements using a specific identifier with

Web Development Guide <ul id="menüü"> <li id="meist"> <p><a href="meist.html">Meist</a></p> </li> </ul> <div id="submenu"> <ul id="submeist"> ...