Я хочу создать элемент управления хлебными крошками для веб-приложения. Текст в каждом дочернем элементе flex-box должен быть обрезан всякий раз, когда места в контейнере недостаточно для отображения всего. Проблема в том, что дети меньшего роста уменьшаются вместе с детьми большего размера и быстро становятся нечитаемыми.
Например. 3 низкорослых ребенка выглядят прекрасно:
Но как только у одного ребенка будет больше текста, все будет сжато:
Есть ли способ настроить flex box, чтобы он сначала уменьшал более крупных дочерних элементов до того же размера, что и меньшие дочерние элементы?
В идеале пример должен выглядеть так:
.container {
display: flex;
width: 600px;
}
.child {
font-size: 30px;
margin: 0 10px;
text-overflow: ellipsis;
white-space: nowrap;
overflow: hidden;
} <div class = "container">
<div class = "child">
Link A
</div>
<div class = "child">
Link B
</div>
<div class = "child">
Link C this is a really long link with lots of text
</div>
</div>





Вы пытались установить ширину для детей?
.container {
display: flex;
width: 600px;
}
.child {
font-size: 30px;
margin: 0 10px;
text-overflow: ellipsis;
white-space: nowrap;
overflow: hidden;
width: 200px;
} <div class = "container">
<div class = "child">Link A</div>
<div class = "child">Link B</div>
<div class = "child">Link C this is a really long link with lots of text</div>
</div>Вы можете добавить минимальная ширина маленьких детей, который вы хотите отобразить полностью
.container {
display: flex;
width: 600px;
}
.child {
font-size: 30px;
margin: 0 10px;
text-overflow: ellipsis;
white-space: nowrap;
overflow: hidden;
min-width:86px;
}<div class = "container">
<div class = "child">Link A</div>
<div class = "child">Link B</div>
<div class = "child">Link C this is a really long link with lots of text</div>
</div>Спасибо! Минимальная ширина зависит от длины неусеченного текста. Есть ли способ получить это динамически?
Я думаю, вы ищете flex-shrink, flex-grow и flex-basis, чтобы узнать больше прочитайте это
.container {
display: flex;
width: 300px;/*I changed this just for testing*/
}
.child {
flex: 1 1 33%;/*Add this line*/
font-size: 30px;
margin: 0 10px;
text-overflow: ellipsis;
white-space: nowrap;
overflow: hidden;
}<div class = "container">
<div class = "child">
Link A
</div>
<div class = "child">
Link B
</div>
<div class = "child">
Link C this is a really long link with lots of text
</div>
</div>С flexbox я предполагаю, что вариант состоит в том, чтобы сделать гибкие элементы растянутым на равную величину, задав flex: 1 элементу child (но у этого есть недостаток, заключающийся в том, что элементы child не имеют ширины авто) — см. демонстрацию ниже:
.container {
display: flex;
width: 600px;
}
.child {
font-size: 30px;
margin: 0 10px;
text-overflow: ellipsis;
white-space: nowrap;
overflow: hidden;
border: 1px solid blue;
flex: 1;
}<div class = "container">
<div class = "child">
Link A
</div>
<div class = "child">
Link B
</div>
<div class = "child">
Link C this is a really long link with lots of text
</div>
</div>Используя CSS Grid layout, вы можете легко добиться этого:
Используйте display: grid на контейнере.
Добавьте grid-template-columns: repeat(3, auto), чтобы сделать три столбца с автоматической шириной
См. демо ниже:
.container {
display: grid; /* defines a grid container */
grid-template-columns: repeat(3, auto); /* all three columns with auto width */
width: 600px;
}
.child {
font-size: 30px;
margin: 0 10px;
text-overflow: ellipsis;
white-space: nowrap;
overflow: hidden;
border: 1px solid blue;
}<div class = "container">
<div class = "child">
Link A
</div>
<div class = "child">
Link B
</div>
<div class = "child">
Link C this is a really long link with lots of text
</div>
</div>Работает на меня! Спасибо
другой интересная тема