14

I have a problem with flexbox. When I set height of a paragraph and also set its overflow to hidden, that paragraph disappear in flexbox div.

Here is fiddle: https://jsfiddle.net/Slit/0yy3q8bL/

The code:

        <div class="N">
            <p>Lorem ipsum</p>
            <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
            <img src="http://codegentstatic-codegentltd.netdna-ssl.com/media/original/0d9648a6-afc6-1/845x2000.jpg">
        </div>

<style>
.N {
    overflow: scroll;
    position: relative;
    display: flex;
    flex-direction: column;
    flex-wrap: nowrap;
    align-items: center;
    min-width: 100vmin;
    width: 100vmin;
    height: 65vmin;
    border: 1px solid green;
    list-style: none;

}

.N img {
    display: list-item;
    width: 98%;
    height: 98%;
    order: 1;
}

.N p:nth-child(1) {
    display: list-item;
    font-size: 4vmin;
    order: 2;
    background-color: white;
}

.N p:nth-child(2) {
    overflow: hidden;
    height: 20%;
    display: list-item;
    text-align: justify;
    font-size: 4vmax;
    background-color: white;
    order: 3;
}
</style>
Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
Slit
  • 491
  • 1
  • 7
  • 20

4 Answers4

23

And then the Flexbox module changed the initial value of min-height:

4.5 Implied Minimum Size of Flex Items

To provide a more reasonable default minimum size for flex items, this specification introduces a new auto value as the initial value of the min-width and min-height properties defined in CSS 2.1.

That auto value behaves like this:

On a flex item whose overflow is visible in the main axis, when specified on the flex item’s main-axis min-size property, the following table gives the minimum size:

┌────────────────┬──────────────────┬─────────────────────────────────────┐
│ Specified Size | Transferred Size |            Minimum Size             |
├────────────────┼──────────────────┼─────────────────────────────────────┤
|                |                  | content size                        |
|       ✓        |                  | min(specified size, content size)   |
|                |        ✓         | min(transferred size, content size) |
|       ✓        |        ✓         | min(specified size, content size)   |
└────────────────┴──────────────────┴─────────────────────────────────────┘

Otherwise, this keyword computes to 0 (unless otherwise defined by a future specification).

So min-height: auto forces the p element to be tall enough to show the content when oveflow: visible. But when you change it to overflow: hidden, the min-height becomes 0. And since there is no space, the p element shrinks.

So either use overflow: visible, or impose some min-height manually.

Alternatively, you can prevent shrinking with flex-shrink: 0. Note that since you have height: 20%, part of the content may still be clipped.

.N {
  overflow: scroll;
  position: relative;
  display: flex;
  flex-direction: column;
  flex-wrap: nowrap;
  align-items: center;
  min-width: 100vmin;
  width: 100vmin;
  height: 65vmin;
  border: 1px solid green;
  list-style: none;

}
.N img {
  display: list-item;
  width: 98%;
  height: 98%;
  order: 1;
}
.N p:nth-child(1) {
  display: list-item;
  font-size: 4vmin;
  order: 2;
  background-color: white;
}
.N p:nth-child(2) {
  overflow: hidden;
  height: 20%;
  display: list-item;
  text-align: justify;
  font-size: 4vmax;
  background-color: white;
  order: 3;
  flex-shrink: 0;
}
<div class="N">
  <p>Lorem ipsum</p>
  <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
  <img src="http://codegentstatic-codegentltd.netdna-ssl.com/media/original/0d9648a6-afc6-1/845x2000.jpg">
</div>

Instead, you might want something like this:

min-height: 20%;
height: auto;
flex-shrink: 0;

.N {
  overflow: scroll;
  position: relative;
  display: flex;
  flex-direction: column;
  flex-wrap: nowrap;
  align-items: center;
  min-width: 100vmin;
  width: 100vmin;
  height: 65vmin;
  border: 1px solid green;
  list-style: none;

}
.N img {
  display: list-item;
  width: 98%;
  height: 98%;
  order: 1;
}
.N p:nth-child(1) {
  display: list-item;
  font-size: 4vmin;
  order: 2;
  background-color: white;
}
.N p:nth-child(2) {
  overflow: hidden;
  display: list-item;
  text-align: justify;
  font-size: 4vmax;
  background-color: white;
  order: 3;
  min-height: 20%;
  flex-shrink: 0;
}
<div class="N">
  <p>Lorem ipsum</p>
  <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
  <img src="http://codegentstatic-codegentltd.netdna-ssl.com/media/original/0d9648a6-afc6-1/845x2000.jpg">
</div>
Justin Grant
  • 44,807
  • 15
  • 124
  • 208
Oriol
  • 274,082
  • 63
  • 437
  • 513
  • I'm not sure this is a clear answer to the question. The elements in the code have an explicit `height` defined, so why would the default `min-height` matter? It also appears this answer is similar to my answer (if you include the comments), as both answers point to a lack of space to display the paragraph text. I want to upvote this answer but maybe you could clarify a bit. Thanks. – Michael Benjamin Oct 21 '15 at 23:40
  • @Michael_B The main part of my answer is explaining **why** adding `overflow: hidden` produces this behavior. `min-height` matters because flexible boxes are, well, flexible. They can be bigger or smaller than the given `flex-basis`. – Oriol Oct 22 '15 at 00:57
  • 1
    What is `Transferred Size`(from the table)? – lordvcs Jun 28 '20 at 12:14
  • `flex-shrink: 0;` did it for me, it was due to the parent being flexed and iOS Safari 13.1.2 thinking these items should shrink and have no height. – fredrivett Aug 13 '22 at 19:44
  • None of these suggestions works for me. Computed height was still 0. – Jason Cheng Mar 13 '23 at 23:23
  • None of these worked for me. Computed height was still zero. I had to wrap my felxbox in another element with display:grid – Jason Cheng Mar 13 '23 at 23:25
7

Sorry for replying to an old post. I was frantically trying to solve a similar issue where I had some nested flex content that had a height of 0 even though it clearly had children, and the children had height!

I solved it (after an hour of cussing flex box to death) by setting flex: 0 0 auto on the element with 0 height. Setting the flex-basis alone wasn't enough. I hope this will work for anyone else coming across this post.

Before that, I tried all sorts. A min-height also worked, but I really wanted to avoid hard-coding the height, as it should be dynamic.

AlexMorley-Finch
  • 6,785
  • 15
  • 68
  • 103
  • Nice, this helped me out. Something to note about the flex attribute syntax, the values go: `flex: flex-grow | flex-shrink | flex-basis` And in this case, setting `flex-shrink: 0` is all that's required, the default is 1. More here: https://developer.mozilla.org/en-US/docs/Web/CSS/flex – jket Mar 29 '19 at 02:47
2

While I cannot offer an explanation as to why this works (yet), I have two solutions that work in cases where the other answers do not. I had a situation where an element nested in multiple levels of flex columns and rows were refusing to stretch to the full height of its immediate parent when given height: 100%.

Two solutions I found worked in this case:

  1. Give all elements in the problem element's ancestry a height of 100%.
  2. (Probably a better solution): Remove height: 100% from the problem element and replace it with align-self stretch or add align-items: stretch to the immediate parent.
hayavuk
  • 351
  • 2
  • 6
0

None of the other suggestions worked for. While fiddling around with the parent container, I noticed that the browser said that flex wouldn't work because the parent container was display:block.

I then created a new wrapper div around my flexbox div and set it's display:grid which is the display setting using by my page's body tag (don't blame me. Blame shopify).

So the structure is now

<body style="display:grid">
<div style="display:block">
<div id="newWrapperDiv" style="display:grid">
<div style="display:flex">
...my actual content...

All I can say is that HTML & CSS are lame and needs to be refactored entirely.

Jason Cheng
  • 180
  • 2
  • 12