Search code examples
cssoverflowcss-gridword-wrap

css-grid and `fr` unit does not work with `overflow-wrap: break-word`


I have a grid container with a fixed width and a single row and column containing a single word. When I use the fr unit to size the column, overflow-wrap: break-word does not wrap the word causing the word in the column to overflow.

To the best of my understanding and docs, overflow-wrap: break-word is supposed to break at any point to prevent overflow if there is no acceptable break point.

Two things seem to fix this. One is to have a px unit for column size. Second is having overflow: hidden on the text container. I observed the same behaviour on Chrome(v73) and Firefox(v66).

Does anybody know the reason behind this behaviour ?

https://jsfiddle.net/vdx90qeg/2/

<div class="container">
  <div class="item1">
    someverylongword
  </div>
</div>
.container {
  display: grid;
  grid-template-columns: 1fr;
  /*using 'px' instead of 'fr' works */
  grid-template-areas: "item1";
  width: 80px;
  border: 1px solid black;
}

.item1 {
  grid-area: "item1";
  overflow-wrap: break-word;
  /* overflow: hidden; */
  /* works when uncommented */
}

Solution

  • Looks like this problem is very similar to what https://css-tricks.com/preventing-a-grid-blowout/ solves

    Set column to minmax(0, 1fr) instead of just 1fr. This approach gives the column a minimum width which was default to auto. The goal is to turn the column from indefinitely sized container to a definitely sized container.

    codepen link: https://codepen.io/thissushiguy/pen/mYdxjb