What is the importance of setting height: 0 when flex-basis: 0 is already defined in CSS3 flexbox?

I am currently working on creating a content reader with a fixed size and title that remains visible while scrolling. I attempted to achieve this using CSS3 flexbox with the following structure:

.flex {
    display: flex;
    flex-direction: column;
    width: 100%;
    height: 150px;
    border: 1px solid #ddd;
}

.title {
    height: 50px;
    line-height: 50px;
    background: #eee;
}

.text-wrap {
    flex: 1 0 0;
}

.text {
    height: 100%;
    overflow-y: auto;
}
<div class="flex">
    <div class="title">Title</div>
    <div class="text-wrap">
        <div class="text">
            <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas faucibus justo vitae urna lacinia pharetra. Quisque nulla lorem, laoreet quis dapibus nec, vehicula vitae lorem. Nunc fringilla justo vel metus rhoncus, at congue leo dictum. Morbi congue tortor lacinia, mollis sapien indsadsadf, rutrum purus. Nam ornare dapibus mi, vitae varius diam tincidunt id. Donec maximus sem nec luctus euismod. Morbi a volutpat diam. In sapien orci, auctor et facilisis eu, finibus ac mauris. Vivamus eu nunc porta, congue libero quis, rutrum nibh. Proin feugiat vel augue mattis cursus.</p>
        </div>
    </div>
</div>

Despite setting up the structure as described above, I encountered an issue where the content overflows from the container despite applying the overflow-y property.

To address this problem, I experimented by specifying the height of the text-wrap element as 0, which surprisingly resolved the issue:

.flex {
    display: flex;
    flex-direction: column;
    width: 100%;
    height: 150px;
    border: 1px solid #ddd;
}

.title {
    height: 50px;
    line-height: 50px;
    background: #eee;
}

.text-wrap {
    flex: 1 0 0;
    height: 0;
}

.text {
    height: 100%;
    overflow-y: auto;
}
<div class="flex">
    <div class="title">Title</div>
    <div class="text-wrap">
        <div class="text">
            <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas faucibus justo vitae urna lacinia pharetra. Quisque nulla lorem, laoreet quis dapibus nec, vehicula vitae lorem. Nunc fringilla justo vel metus rhoncus, at congue leo dictum. Morbi congue tortor lacinia, mollis sapien indsadsadf, rutrum purus. Nam ornare dapibus mi, vitae varius diam tincidunt id. Donec maximus sem nec luctus euismod. Morbi a volutpat diam. In sapien orci, auctor et facilisis eu, finibus ac mauris. Vivamus eu nunc porta, congue libero quis, rutrum nibh. Proin feugiat vel augue mattis cursus.</p>
        </div>
    </div>
</div>

I find this behavior confusing. By setting the flex-basis of text-wrap to 0 and giving it the flex-grow property, shouldn't the height be automatically adjusted to fit the remaining space in the flex container? Why is there a need for setting height: 0 specifically?

Your insights on this matter would be greatly appreciated.

Answer №1

Your code is verified and functioning correctly.

You are almost there with implementing flexbox in your layout:

One important aspect to consider for a flex container is the initial setting min-height: auto. This default setting prevents a flex item from being shorter than its content. By overriding this, you can achieve the desired layout outcome.

Instead of using height: 0, it is recommended to follow the standard approach:

.text-wrap {
    flex: 1 0 0;
    min-height: 0; /* new */   
}

Detailed explanation available at: Why does the flex item not shrink beyond its content size?

.flex {
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 200px;
  border: 2px solid #aaa;
}
.title {
  height: 60px;
  line-height: 60px;
  background: #ccc;
}
.text-wrap {
  flex: 1 0 0;
  min-height: 0; /* new */
}
.text {
  height: 120%;
  overflow-y: hidden;
}
<div class="flex">
  <div class="title">Main Title</div>
  <div class="text-wrap">
    <div class="text">
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur fringilla lacus id placerat dignissim. Vivamus condimentum urna vel nisi mollis molestie. Nullam tristique sem nec magna facilisis ultrices. Phasellus elementum odio sed augue volutpat,
        vitae tempor purus lobortis. In venenatis metus leo, eget tincidunt arcu interdum ut. Donec faucibus erat ac justo euismod blandit. Maecenas suscipit eros in justo commodo, non malesuada mauris fermentum. Integer scelerisque neque et sapien fermentum
        tempus. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.</p>
    </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

What is causing the extra whitespace on the right side with container-fluid?

Could someone assist me with an issue I'm experiencing when using the container-fluid in Bootstrap? It seems to be leaving some space on the right side of my web page, and as a newcomer to Bootstrap, any help would be greatly appreciated. <link ...

Who needs a proper naming convention when things are working just fine? What's the point of conventions if they don't improve functionality?

I am a newcomer to the world of JavaScript programming and stumbled upon this example while practicing. <html> <head> <script type="text/javascript"> function changeTabIndex() { document.getElementById('1').tabIndex="3" d ...

I possess a table that showcases MatIcon buttons. Upon clicking on a button, two additional buttons should appear at the bottom of the table

I am working on a table that contains mat-icon-buttons. When the button is clicked, it should display 2 additional buttons at the end of the table. Upon clicking the first button, its color changes from primary to red, and I would like to add two more butt ...

My goal is to develop a table that is both able to be resized and easily repositioned

I've been working on a project to develop an interactive table that can be manipulated, resized, and repositioned within a canvas. The code snippet below shows my attempt at creating this table on the canvas: var canvas = document.getElementById("dra ...

I'm currently troubleshooting why the radio buttons in my form seem to be coming up as undefined

I am currently working on a form and trying to validate it using my own code. Even though I can successfully retrieve values for radio buttons by triggering an alert when clicked, the form validator keeps returning these radio values as undefined. What cou ...

Tips for achieving a precise amount of boxes in each row

Is it possible to only have 5 boxes per line and then the next one goes to the next line using CSS? https://i.stack.imgur.com/L7vr4.png .box { border-color: #32ff00; border-width: 4px; border-style: dotted; width: 170px; height: 200px; fl ...

Dynamic React animation with sliding elements

When jumping between the second and third "page" of content, I am unable to determine the reason why it is happening. The content is displayed as an absolute position. Check out the sandbox here: https://codesandbox.io/s/nostalgic-bhabha-d1dloy?file=/src ...

Tips on eliminating the top margin on my webpage

I am in need of assistance with removing the margin at the top of my page. Take a look at the screenshot for reference: As shown in the image, there is a red arrow pointing to the problematic margin. Can someone guide me on how to eliminate this margin? B ...

Guide to uploading a PDF to Google Drive and embedding it on an HTML static website

I manage a static HTML site for a small food shop. They require monthly menu uploads in PDF format. I believe uploading to Google Drive would be a more efficient solution than creating a separate admin view for file uploads. Can anyone advise me on how to ...

Colorful radial spinner bar

I am interested in replicating the effect seen in this video: My goal is to create a spinner with text in the center that changes color, and when the color bar reaches 100%, trigger a specific event. I believe using a plugin would make this task simpler, ...

Creative CSS spinners design

Seeking assistance to modify my spinner design. I want one end to be thicker and the other end to be thin. My attempts so far have been unsuccessful. <div id="data-loader"> <div class="loader"> <div class="blue"></div& ...

Overlay a small image on top of a larger background image using canvas, then merge them into a single set of pixel

Is there a way to combine a smaller image with a larger background image on one canvas while allowing the smaller image to move around? I'll need to save the original background pixel data so that each frame can be redrawn with the overlay in its upda ...

Issue encountered when attempting to activate a Vue function in order to update a specific component's field

Let me preface this by saying that I am new to Vue.js and still learning the ropes. Here's what I'm trying to achieve: I have a label and a button. The behavior I want is that when the label is not visible, the button should display "Show Label" ...

Is it necessary for me to include body, html at the beginning of my CSS code?

For all of my pages, I have been including the following code at the top. body { color: #333333; font-family: Arial,sans-serif; font-size: 0.95em; line-height: 1.15; } However, a colleague of mine has informed me that I only need to targ ...

Ways to minimize the space between elements in a flex container

My goal is to minimize the space between my images that expand on hover. I aim for it to resemble this: expectation However, the current appearance is more like this: reality This is what I currently have: .container { display: flex; width: 100 ...

Footer not sticking to the bottom of the page with Material UI

View Page Screenshot My challenge is with the Footer using AppBar when there is no content. It doesn't properly go to the end of the page, only to the end of the content. I want it to stick to the bottom of the page if the content doesn't reach ...

Refresh the data in the DataTables table using a fragment

Currently, I am attempting to reload a fragment using ajax. However, after the reload, my event select and row count do not function properly. It seems that the default configuration is not behaving as expected: @PostMapping("/admin/add") ...

Having trouble with jQuery's recursive animation functionality

I have developed a function to scroll multiple images horizontally in the header of my website. Inside my header, I have implemented code that looks like this: <div class='images'> <!-- this div is 150% wide with overflow hidden --> ...

Struggling to determine the expense upon button activation - data remains stagnant

My coding project involves a basic ordering system where users can input an integer, click on an update button, and see the total cost displayed below. Despite my efforts, I've encountered issues with two different methods: Using plain JavaScript for ...

Store user input in a paragraph

I want to create a unique program that allows users to input text in a field, and when they click "Start", the text will appear in a paragraph backwards. I plan to use Html, jQuery, and CSS for this project. Can anyone provide guidance on how to achieve th ...