Forum Bugs

Flexbox component not shrinking

Jellby
I'm trying to have a fullpage illustration + caption, where the caption can be long, and the illustration should be resized to the maximum possible size that still fits in a page/screen together with the caption. In a browser (Firefox) I could get it to work with either "display:grid" or "display:flex", but with Prince (12.5), the latter is not working as expected. Instead of a page with image + caption, I get a full-page image and then a blank page (not even with the caption). It's as if the .image box is not shrinking to make room for the caption.

Here is a sample code:

<?xml version='1.0' encoding='utf-8'?>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:ops="http://www.idpf.org/2007/ops" xml:lang="en">
<head>
<title></title>
<style>
@page {
  size: 9cm 12cm;
  margin: 1mm;
}
.illustration {
  page-break-inside: avoid;
  page-break-before: always;
  page-break-after: always;
  text-align: center;
  text-indent: 0;
  margin: 0;
}
img {
  max-width: 100%;
  max-height: 100%;
}
.illustration .caption {
  font-style: italic;
}
.fullpage {
  height: 100vh;
  width: 100%;
  text-align: center;
}
@supports (display: grid) {
  .illustration {
    display: grid;
    grid-template: "image" minmax(0,max-content) "caption" min-content;
    grid-row-gap: 0.5em;
    height: 100vh;
    width: 100%;
    margin: 0em auto;
    box-sizing: border-box;
  }
  .illustration .image {
    display: block;
    grid-area: image;
  }
  .illustration .caption {
    display: block;
    grid-area: caption;
  }
}
@supports (display: flex) {
  .illustration {
    display: flex;
    flex-direction: column;
    align-items: center;
    height: 100vh;
    width: 100%;
    box-sizing: border-box;
  }
  .illustration .image {
    min-height: 1em;
    flex-shrink: 1;
    flex-grow: 0;
  }
  .illustration .caption {
    flex-shrink: 0;
    flex-grow: 1;
  }
}
</style>
</head>
<body>

<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>

<div class="illustration">
<span class="image"><img src="https://blog.photoshopcreative.co.uk/wp-content/uploads/2017/10/xfinal-1-760x1024.jpg.pagespeed.ic.xtQ1opfcsy.jpg" alt=""/></span>
<span class="caption">Donec eu ex non dolor ornare tristique. Phasellus orci leo, auctor nec interdum quis, bibendum eu lacus. Integer erat nibh, gravida id varius id, pharetra sed nibh. Interdum et malesuada fames ac ante ipsum primis in faucibus. Vestibulum nulla mi, faucibus et tortor viverra, euismod fringilla mi.</span>
</div>

<div class="illustration">
<span class="image"><img src="https://blog.photoshopcreative.co.uk/wp-content/uploads/2017/10/xfinal-1-760x1024.jpg.pagespeed.ic.xtQ1opfcsy.jpg" alt=""/></span>
<span class="caption">Duis lorem nibh, convallis at ante quis, facilisis mollis lectus. Sed dignissim mauris ac mi dictum sodales. Nulla tincidunt orci iaculis mi mollis congue. Suspendisse et imperdiet mi. Vivamus mi justo, accumsan hendrerit egestas id, posuere vel turpis. Cras ac ipsum nunc. Proin pellentesque ex ac scelerisque egestas. Morbi ac elit at lectus vestibulum ultrices eget id dui. Proin facilisis tortor vel vehicula rutrum. Nunc euismod, nulla in elementum elementum, arcu nunc placerat quam, fermentum accumsan mauris est eu erat.</span>
</div>

<div class="fullpage">
<img src="https://blog.photoshopcreative.co.uk/wp-content/uploads/2017/10/xfinal-1-760x1024.jpg.pagespeed.ic.xtQ1opfcsy.jpg" alt=""/>
</div>

</body>
</html>


It works fine if I increase the page height (to e.g. 24cm), but that's of course not a solution, and on the browser it works no matter how I resize the window (as long as the caption fits, at least).

Is there some other solution or workaround? This code is used in other renderers, so I'd prefer a CSS-only solution.
Jellby
Some other tests made me think "height: 100vh;" worked in prince, but apparently it doesn't. Anyway, setting a fixed height, e.g. "illustration{height:118mm}", doesn't help.

Edited by Jellby

markbrown
I'll look into this.
markbrown
The problem here is that we're unable to properly resolve the percentage value for max-width on the <img> element. As a workaround, are you able to set max-width to a fixed value? E.g.:

img {
    max-width: 88mm;
    max-height: 100%;
}

Jellby
Are you sure it's the width? I could set a fixed with, but "width:100%" has always worked too. I applied your suggestion and didn't see any difference, the image is full page (by chance) and the caption goes to the next page. For a clearer view use:
@page {
  size: 9cm 9cm;
}

("Correct" rendering would be with more like "height: 56mm" on page 2 and "height: 31mm" on page 3.)
markbrown
Note it's 'max-width' not 'width', and also that using fixed height instead of vh units is also required. I've attached the test file I'm using, as well as the output from a development build of prince (the output from 12.5 is the same). Does that look right?

  1. s1.pdf183.0 kB
  2. s1.xhtml2.6 kB
Jellby
Finally, I got it working in my actual document (which is a bit more complex than the example, with several style files overwriting each other...). I must have been doing something wrong, because if I now take my initial example and just change the "100vh" to "118mm", it seems to work too.

I even combined this with "float: top" and now the result is perfect. Only I need to add a wrapper span, because a <span style="display: flex; float: top">, placed inline, causes a line break, even if the flex is moved to another page, but <span style="float: top"><span style="display: flex"> works fine.
markbrown
That last issue can be avoided by using "display: inline-flex" - thanks for letting us know about the problem, though.
Jellby
I should have thought of it. However, there seems to be another problem there. With "display: inline-flex" the linebreak is not introduced, but floats disappear and TOC links are broken:

<?xml version='1.0' encoding='utf-8'?>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:ops="http://www.idpf.org/2007/ops" xml:lang="en">
<head>
<title></title>
<style>
@page {
  size: 9cm 12cm;
  margin: 1mm;
}
.illustration {
  page-break-inside: avoid;
  text-align: center;
  text-indent: 0;
  margin: 0;
}
img {
  max-width: 100%;
  max-height: 100%;
}
.illustration .caption {
  font-style: italic;
}

.illustration {
  display: flex;
  flex-direction: column;
  align-items: center;
  height: 118mm;
  width: 100%;
  float: top unless-fit;
}
.illustration .image {
  min-height: 1em;
  flex-shrink: 1;
  flex-grow: 0;
}
.illustration .caption {
  flex-shrink: 0;
  flex-grow: 1;
}

/* This should work */
span.illustration {
  display: inline-flex;
}
/* This works instead */
/*span.floater{
  float: top unless-fit;
}*/
</style>
</head>
<body>

<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.
<span class="floater">
<span class="illustration">
<span class="image"><img src="https://blog.photoshopcreative.co.uk/wp-content/uploads/2017/10/xfinal-1-760x1024.jpg.pagespeed.ic.xtQ1opfcsy.jpg" alt=""/></span>
<span class="caption">Donec eu ex non dolor ornare tristique. Phasellus orci leo, auctor nec interdum quis, bibendum eu lacus. Integer erat nibh, gravida id varius id, pharetra sed nibh. Interdum et malesuada fames ac ante ipsum primis in faucibus. Vestibulum nulla mi, faucibus et tortor viverra, euismod fringilla mi.</span>
</span>
</span>
Etiam mollis mattis accumsan. Quisque vestibulum felis neque, at tempus diam viverra ut. Nunc interdum orci dictum convallis porttitor. Mauris cursus nulla quis luctus tristique. Duis ultricies ex non erat interdum, ut aliquam sapien convallis. Cras eu elit nec mauris suscipit facilisis. Morbi sed felis justo. Etiam eu nibh quis metus scelerisque accumsan. Nam id aliquet sem.</p>

<div class="illustration">
<span class="image"><img src="https://blog.photoshopcreative.co.uk/wp-content/uploads/2017/10/xfinal-1-760x1024.jpg.pagespeed.ic.xtQ1opfcsy.jpg" alt=""/></span>
<span class="caption">Duis lorem nibh, convallis at ante quis, facilisis mollis lectus. Sed dignissim mauris ac mi dictum sodales. Nulla tincidunt orci iaculis mi mollis congue. Suspendisse et imperdiet mi. Vivamus mi justo, accumsan hendrerit egestas id, posuere vel turpis. Cras ac ipsum nunc. Proin pellentesque ex ac scelerisque egestas. Morbi ac elit at lectus vestibulum ultrices eget id dui. Proin facilisis tortor vel vehicula rutrum. Nunc euismod, nulla in elementum elementum, arcu nunc placerat quam, fermentum accumsan mauris est eu erat.</span>
</div>

<p>Donec aliquet sed diam molestie euismod. Curabitur mollis ultricies nisi, ac aliquet nunc faucibus a. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Phasellus ligula quam, efficitur nec nunc vitae, porttitor ornare neque. Aenean quis ante sit amet leo facilisis molestie. Nunc tortor ipsum, pretium vel justo at, gravida varius odio. In at justo vitae est facilisis tincidunt. Morbi suscipit convallis metus, at rutrum nulla interdum at. Nam vulputate molestie mauris, eu tempor mi. Vestibulum in rutrum neque. Etiam ultrices leo id laoreet iaculis. Quisque a ipsum non augue blandit efficitur ut nec risus. In ultricies erat sit amet odio porttitor ullamcorper. Nulla dui velit, lobortis vel mattis vel, euismod vel ligula. Donec elementum sapien a hendrerit efficitur.</p>

<h1>Title</h1>

<p>Etiam vel aliquet sem. Maecenas imperdiet elementum neque, a elementum nunc lacinia sit amet. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; In ac mollis sapien, vel mollis odio. Maecenas lacinia, urna et auctor tempor, eros ante faucibus felis, vel dictum elit risus vel est. Ut erat risus, venenatis malesuada urna in, vestibulum malesuada neque. Vestibulum bibendum maximus consequat. Donec in pellentesque odio. Sed aliquet turpis id mi fringilla, in interdum metus imperdiet. Pellentesque non blandit massa, at rutrum enim. In rhoncus libero velit, sit amet aliquam nibh consectetur vel. Aenean a tempus turpis, nec placerat ipsum. Proin dictum efficitur metus, sed facilisis lectus maximus in.</p>

</body>
</html>


Try uncommenting just one of the "span.illustration" or "span.floater" rules.