I’ve always had an issue where a text element simply doesn’t break when it goes beyond its parent element. I know that if I set, for example, max-width: 100px, then it will break, but that’s not practical. I want it to break only when it encounters the block.
.card {
display: grid;
background-color: white;
border: 1px solid #e0e0e0;
border-radius: 0.5rem;
overflow: hidden;
}
.card-description {
padding: 1rem;
overflow-wrap: break-word;
}
I tried to set max-width: 100px, but it not responsible, i wanna find some of good solution. I want it to break only when it encounters the block.
2
Answers
you can try this
The root of your problem is that grid items under certain conditions have a default
min-width
(andmin-height
) ofauto
. In this case, that is equal to the width of your text content. Refer to the spec for more details on this.If you set
min-width: 0;
on your grid items, you should be able to override that default and successfully setoverflow-wrap: break-word;
orword-wrap: break-word;
.You can also set
overflow: hidden;
on your grid items (rather than the grid container, as you’re doing now). This should cause the browser to automatically set grid itemmin-width
to0
rather thanauto
(thus enabling the use ofoverflow-wrap: break-word;
orword-wrap: break-word;
).Just for the sake of completeness, there are a couple other potential solutions:
Use
overflow-wrap: anywhere;
instead ofoverflow-wrap: break-word;
. The difference between the two is thatanywhere
will force the browser to consider breakpoints ("soft wrap opportunities") when calculating the element’s intrinsic minimum content size. That is to say, it’s more aggressive, so you may get undesirable results if you usewidth: min-content;
alongside it.Use
word-break: break-all;
. This is sort of an extreme solution because it will ignore other line break opportunities and wrap your text when it hits the end of its container.