skip to Main Content

I have some text at a narrow width, with word-break: break-word and hyphens: auto. With normal flow display, the text wraps and stays within the boundaries of the container as I would expect. However, with CSS Grid, the text is allowed to escape the container and the word break happens at a different point.

Why does this happen?

#wrapper {
  max-inline-size: 25ch;
  margin-inline: auto;
  word-wrap: break-word;
  hyphens: auto;
}

#grid,
#flow {
  padding: 1em;
  background-color: oldlace;
}

#flow {
  margin-block-start: 2em;
}

.tag {
  padding: 0.25em;
  background-color: azure;
  inline-size: min-content;
  font-family: monospace;
}

#grid {
  display: grid;
  gap: 1em;
}
<div id="wrapper">
  <div id="grid">
    <p class="tag">grid</p>
    <h2>This is a long headline with a verysuperlongtimeword</h2>
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam tortor nisl, sagittis ac lectus et, blandit varius leo. Sed vel finibus nunc. Nam ipsum mi, mattis quis cursus id, hendrerit tristique dui. Class aptent taciti sociosqu ad litora torquent
      per conubia nostra, per inceptos himenaeos. Duis eu placerat quam, vitae pretium tellus. Sed consequat tellus eu diam commodo, quis ultricies libero ultrices. Praesent nec ipsum venenatis, semper magna at, pretium tortor. Aliquam tempus lectus iaculis
      lacus pretium sollicitudin. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Suspendisse dictum, felis eu pulvinar scelerisque, risus diam rutrum velit, vel lacinia leo risus vel purus. Integer ipsum
      justo, auctor eu volutpat non, pellentesque a est. Maecenas sollicitudin fermentum mauris, egestas viverra augue congue vitae. Nam non nisi urna. Nullam non cursus orci.</p>
  </div>

  <div id="flow">
    <p class="tag">flow</p>
    <h2>This is a long headline with a verysuperlongtimeword</h2>
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam tortor nisl, sagittis ac lectus et, blandit varius leo. Sed vel finibus nunc. Nam ipsum mi, mattis quis cursus id, hendrerit tristique dui. Class aptent taciti sociosqu ad litora torquent
      per conubia nostra, per inceptos himenaeos. Duis eu placerat quam, vitae pretium tellus. Sed consequat tellus eu diam commodo, quis ultricies libero ultrices. Praesent nec ipsum venenatis, semper magna at, pretium tortor. Aliquam tempus lectus iaculis
      lacus pretium sollicitudin. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Suspendisse dictum, felis eu pulvinar scelerisque, risus diam rutrum velit, vel lacinia leo risus vel purus. Integer ipsum
      justo, auctor eu volutpat non, pellentesque a est. Maecenas sollicitudin fermentum mauris, egestas viverra augue congue vitae. Nam non nisi urna. Nullam non cursus orci.</p>
  </div>

</div>

https://codepen.io/bronzehedwick/pen/zYVbNWK

2

Answers


  1. I can’t explain why this happens, although the fact that this behaviour is consistent across Safari/Chrome (whose codebases have a common origin) and Firefox (whose codebase is completely separate) indicates that the behaviour probably derives from the standards.

    However, I have discovered an easy workaround. Set overflow: hidden on #grid h2, and it behaves as per the flow layout.

    #wrapper {
      max-inline-size: 25ch;
      margin-inline: auto;
      word-wrap: break-word;
      hyphens: auto;
    }
    
    #grid, #flow {
      padding: 1em;
      background-color: oldlace;
    }
    
    #flow {
      margin-block-start: 2em;
    }
    
    .tag {
      padding: 0.25em;
      background-color: azure;
      inline-size: min-content;
      font-family: monospace;
    }
    
    #grid {
      display: grid;
      gap: 1em;
    }
    
    #grid h2 {
      overflow: hidden;
    }
    <div id="wrapper">
      <div id="grid">
        <p class="tag">grid</p>
        <h2>This is a long headline with a verysuperlongtimeword</h2>
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam tortor nisl, sagittis ac lectus et, blandit varius leo. Sed vel finibus nunc. Nam ipsum mi, mattis quis cursus id, hendrerit tristique dui. Class aptent taciti sociosqu ad litora torquent
          per conubia nostra, per inceptos himenaeos. Duis eu placerat quam, vitae pretium tellus. Sed consequat tellus eu diam commodo, quis ultricies libero ultrices. Praesent nec ipsum venenatis, semper magna at, pretium tortor. Aliquam tempus lectus iaculis
          lacus pretium sollicitudin. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Suspendisse dictum, felis eu pulvinar scelerisque, risus diam rutrum velit, vel lacinia leo risus vel purus. Integer ipsum
          justo, auctor eu volutpat non, pellentesque a est. Maecenas sollicitudin fermentum mauris, egestas viverra augue congue vitae. Nam non nisi urna. Nullam non cursus orci.</p>
      </div>
    
      <div id="flow">
        <p class="tag">flow</p>
        <h2>This is a long headline with a verysuperlongtimeword</h2>
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam tortor nisl, sagittis ac lectus et, blandit varius leo. Sed vel finibus nunc. Nam ipsum mi, mattis quis cursus id, hendrerit tristique dui. Class aptent taciti sociosqu ad litora torquent
          per conubia nostra, per inceptos himenaeos. Duis eu placerat quam, vitae pretium tellus. Sed consequat tellus eu diam commodo, quis ultricies libero ultrices. Praesent nec ipsum venenatis, semper magna at, pretium tortor. Aliquam tempus lectus iaculis
          lacus pretium sollicitudin. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Suspendisse dictum, felis eu pulvinar scelerisque, risus diam rutrum velit, vel lacinia leo risus vel purus. Integer ipsum
          justo, auctor eu volutpat non, pellentesque a est. Maecenas sollicitudin fermentum mauris, egestas viverra augue congue vitae. Nam non nisi urna. Nullam non cursus orci.</p>
      </div>
    
    </div>
    Login or Signup to reply.
  2. CSS grid applies a default min-width configuration to its grid items preventing them from shrinking past a minimum size which is basically equal to "min-content".

    In your case, the min-content is defined by the longest word verysuperlongtimeword. If you inspect the grid, you can easily notice this.

    min width content of the grid

    And since it’s a grid, that item will define the width of the column where all the items will get placed so even the text below it will have that same width and will overflow as well.

    To fix this, you have to disable the min-width constraint by using min-width: 0

    #wrapper {
      max-inline-size: 25ch;
      margin-inline: auto;
      word-wrap: break-word;
      hyphens: auto;
    }
    
    #grid,
    #flow {
      padding: 1em;
      background-color: oldlace;
    }
    
    #flow {
      margin-block-start: 2em;
    }
    
    .tag {
      padding: 0.25em;
      background-color: azure;
      inline-size: min-content;
      font-family: monospace;
    }
    
    #grid {
      display: grid;
      gap: 1em;
    }
    
    #grid > * {
      min-width: 0;
    }
    <div id="wrapper">
      <div id="grid">
        <p class="tag">grid</p>
        <h2>This is a long headline with a verysuperlongtimeword</h2>
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam tortor nisl, sagittis ac lectus et, blandit varius leo. Sed vel finibus nunc. Nam ipsum mi, mattis quis cursus id, hendrerit tristique dui. Class aptent taciti sociosqu ad litora torquent
          per conubia nostra, per inceptos himenaeos. Duis eu placerat quam, vitae pretium tellus. Sed consequat tellus eu diam commodo, quis ultricies libero ultrices. Praesent nec ipsum venenatis, semper magna at, pretium tortor. Aliquam tempus lectus iaculis
          lacus pretium sollicitudin. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Suspendisse dictum, felis eu pulvinar scelerisque, risus diam rutrum velit, vel lacinia leo risus vel purus. Integer ipsum
          justo, auctor eu volutpat non, pellentesque a est. Maecenas sollicitudin fermentum mauris, egestas viverra augue congue vitae. Nam non nisi urna. Nullam non cursus orci.</p>
      </div>
    
      <div id="flow">
        <p class="tag">flow</p>
        <h2>This is a long headline with a verysuperlongtimeword</h2>
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam tortor nisl, sagittis ac lectus et, blandit varius leo. Sed vel finibus nunc. Nam ipsum mi, mattis quis cursus id, hendrerit tristique dui. Class aptent taciti sociosqu ad litora torquent
          per conubia nostra, per inceptos himenaeos. Duis eu placerat quam, vitae pretium tellus. Sed consequat tellus eu diam commodo, quis ultricies libero ultrices. Praesent nec ipsum venenatis, semper magna at, pretium tortor. Aliquam tempus lectus iaculis
          lacus pretium sollicitudin. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Suspendisse dictum, felis eu pulvinar scelerisque, risus diam rutrum velit, vel lacinia leo risus vel purus. Integer ipsum
          justo, auctor eu volutpat non, pellentesque a est. Maecenas sollicitudin fermentum mauris, egestas viverra augue congue vitae. Nam non nisi urna. Nullam non cursus orci.</p>
      </div>
    
    </div>

    You may also think that word-break: break-word and hyphens: auto should avoid this but no. The minimum size is calculated before applying those properties, then the width of the column is defined and only later we consider those properties. They will have no effect since there is enough space for the longest word which is logical since that same word is the one defining that space.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search