Understanding the automatic minimum size of flex items

Praveen Murali

By Praveen Murali

on August 17, 2023

Introduction

Flexbox is a powerful CSS layout module that allows us to create flexible and responsive user interfaces. However, when dealing with flex items that contain text, we may encounter issues where the text overflows its container, disrupting the user interface. In this article, we'll explore the problem of text overflow in flex items and learn about the automatic minimum size behavior of flex items.

The problem

Consider a scenario where we have a card component with a title that can be lengthy. To prevent the card from becoming too large and breaking the layout, we want to truncate the title and show an ellipsis when it overflows its container.

However, even after applying the necessary CSS properties such as white-space: nowrap, overflow: hidden, and text-overflow: ellipsis to card__title, the text doesn't truncate as expected. The card content doesn't shrink to accommodate the overflowing text, resulting in an undesired UI.

issue.png

HTML

1<div class="card">
2  <div class="card__avatar"></div>
3  <div class="card__content">
4    <p class="card__title">This is anexampleofalengthytitle</p>
5    <p class="card__description">some text here</p>
6  </div>
7</div>

CSS

1.card {
2  display: flex;
3  align-items: center;
4  width: 260px;
5  gap: 10px;
6  padding: 10px;
7  margin: 20px;
8  background-color: #ffffff;
9  border-radius: 10px;
10}
11
12.card__avatar {
13  flex-shrink: 0;
14  width: 50px;
15  height: 50px;
16  background-color: #aac4ff;
17  border-radius: 50%;
18}
19
20.card__content {
21}
22
23.card__title {
24  white-space: nowrap;
25  overflow: hidden;
26  text-overflow: ellipsis;
27
28  font-size: 20px;
29  margin: 0;
30}
31
32.card__description {
33  font-size: 14px;
34  margin: 0;
35}

Why is it not shrinking?

The default value of flex-shrink of a flex item is 1, which means the card__content should be able to shrink as much as it wants to. Surprisingly, the result is not what we expected!

The flexbox algorithm refuses to shrink a child below its minimum size.

When there is text inside an element, the minimum size is determined by the length of the longest string of characters that cannot be broken.

Flexbox specification:

To provide a more reasonable default minimum size for flex items, the used value of a main axis automatic minimum size on a flex item that is not a scroll container is a content-based minimum size; for scroll containers the automatic minimum size is zero, as usual.

The solutions

To overcome the issue of the flex item not shrinking as expected, we can apply one of the following solutions:

Solution 1 : Set min-width: 0;

1.card__content {
2  min-width: 0;
3}

By explicitly setting min-width: 0; on the flex item, we can override the default behavior and allow the element to shrink beyond its automatic minimum size. This change enables the flex item to adjust its size to accommodate the ellipsis and prevent UI disruption.

May-26-2023 18-33-48.gif

Solution 2: Set overflow: hidden;

1.card__content {
2  overflow: hidden;
3}

Setting overflow: hidden; alone can also help. This property ensures that any overflowing content is hidden, which indirectly allows the flex item to shrink properly.

May-26-2023 18-34-28.gif

codesandbox demo

Conclusion

Understanding the automatic minimum size behavior of flex items is crucial for creating effective and visually pleasing web layouts. By implementing the suggested solutions, you can overcome the challenges associated with text overflow and achieve the desired UI outcomes.

Happy coding ❤️

Stay up to date with our blogs. Sign up for our newsletter.

We write about Ruby on Rails, ReactJS, React Native, remote work,open source, engineering & design.