Read more

CSS: Flex and "min-width"

Dominik Schöler
March 25, 2019Software engineer at makandra GmbH

min-width is known as a CSS property that can be set to define a least width for an element. Surprisingly, it can also be used to set something that feels like max-width.

min-width in a flex context

Illustration online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
Read more Show archive.org snapshot

While the default min-width value is 0 (zero), for flex items it is auto Show archive.org snapshot . This can make block elements take up much more space than desired, even stretching their container beyond the screen edge on small screens.

Image

min-width is defined to win against competing width and max-width Show archive.org snapshot , meaning as soon as the element's width would shrink below its implicit auto width, min-width:auto will kick in and keep the element from shrinking. The fix is obvious now:

min-width: 0

It tells the browser that this element has no right to take up any minimum width, and now it will be rendered properly, taking up only as much width as is available:

Image


A note about flex-shrink: While flex-shrink sounds like it could help here, it does not. The described issue is about element-sizing based on the element's content, while flex-shrink defines shrinkage relative to other flex elements in the same context.
Confer How to prevent a 1fr grid column overflow.

Posted by Dominik Schöler to makandra dev (2019-03-25 13:27)