I have a div that contains text on it and i needed to apply text-overflow: ellipsis; on that text while the div width should be width: fit-content; and its max-width: 100%; (the ellipsis is then supposed to show in case where the actual div width will be greater than its parent width's as the text will be too long in that case)
.tagWrap {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
line-height: 26px;
width: -moz-fit-content;
width: fit-content;
max-width: 100%;
display: inline-block;
}
This works for long texts but for one word texts i found that the ellipsis is being applied too for no reason and as a consequence only the first letter or few letters of a word will shown!
.tagsWrapper{
display: flex;
flex-direction: row;
justify-content: flex-start;
align-items: flex-start;
width:150px;
border:1px solid green;
}
.tagWrap {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
line-height: 26px;
width: -moz-fit-content;
width: fit-content;
max-width: 100%;
display: inline-block;
border:1px solid red;
margin-right:3px;
}
<div class="tagsWrapper">
<div class="tagWrap">OneWordText</div>
<div class="tagWrap">multi words text</div>
</div>
Here is a jsFiddle showing the issue : https://jsfiddle.net/bardelman/u13hb04j/1/
The one word div is placed at the left, it's not supposed to have the ellipis, the second div is the only one that should get the ellipsis as it goes beyong the parent right edge
1 Answer 1
If you want tags to take additional lines but add ellipsis just for tags that are bigger than the container - simply add flex-wrap: wrap.
.tagsWrapper{
display: flex;
flex-wrap: wrap;
flex-direction: row;
justify-content: flex-start;
align-items: flex-start;
width:150px;
border:1px solid green;
}
.tagWrap {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
line-height: 26px;
width: -moz-fit-content;
width: fit-content;
max-width: 100%;
display: inline-block;
border:1px solid red;
margin-right:3px;
}
<!DOCTYPE html>
<html>
<body>
<div class="tagsWrapper">
<div class="tagWrap">OneWordText</div><div class="tagWrap">OneWo</div>
<div class="tagWrap">multi words text</div>
<div class="tagWrap">OneWordTextOneWordTextOneWordText</div>
</div>
</body>
</html>
If you want some tags to take their width and limit others - you should somehow say browser about it. I.E. you can add flex-shrink: 0 to tags that should take their space.
.tagsWrapper{
display: flex;
flex-direction: row;
justify-content: flex-start;
align-items: flex-start;
width:150px;
border:1px solid green;
}
.tagWrap {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
line-height: 26px;
width: -moz-fit-content;
width: fit-content;
max-width: 100%;
display: inline-block;
border:1px solid red;
margin-right:3px;
}
.tagWrap--noshrink {
flex-shrink: 0;
}
<!DOCTYPE html>
<html>
<body>
<div class="tagsWrapper">
<div class="tagWrap tagWrap--noshrink">OneWordText</div>
<div class="tagWrap">multi words text</div>
</div>
</body>
</html>
If this doesn't answer your question - start by defining your desired logic "on paper", then it will be possible to translate your expected logic to code
Additional solution with :last-child
You don't need to set class that way, but it's still not automated and will not work if children before the last item are too long.
.tagsWrapper{
display: flex;
flex-direction: row;
justify-content: flex-start;
align-items: flex-start;
width:150px;
border:1px solid green;
overflow: hidden;
}
.tagWrap {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
line-height: 26px;
width: -moz-fit-content;
width: fit-content;
max-width: 100%;
display: inline-block;
border:1px solid red;
margin-right:3px;
flex-shrink: 0;
}
.tagWrap:last-child {
flex-shrink: 1;
}
<!DOCTYPE html>
<html>
<body>
<p>will work</p>
<div class="tagsWrapper">
<div class="tagWrap">OneWordText</div>
<div class="tagWrap">multi words text</div>
</div>
<p>will not work</p>
<div class="tagsWrapper">
<div class="tagWrap">OneWordText</div>
<div class="tagWrap">OneWordText</div>
<div class="tagWrap">multi words text</div>
</div>
</body>
</html>
Additional solution with fake ellipses (pseudoelement)
You don't need to set class that way, but it's still not automated and will not work if children before the last item are too long.
.tagsWrapper{
position: relative;
display: flex;
flex-direction: row;
justify-content: flex-start;
align-items: flex-start;
width:150px;
border:1px solid green;
overflow: hidden;
}
.tagWrap {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
line-height: 26px;
width: -moz-fit-content;
width: fit-content;
max-width: 100%;
display: inline-block;
border:1px solid red;
margin-right:3px;
flex-shrink: 0;
}
.tagsWrapper::after {
content: "...";
display: inline-block;
padding-left: 1px;
position: absolute;
right: 0;
top: 1px;
bottom: 1px;
line-height: 26px;
background-color: white; /* need to be replaced with tag bg */
}
<!DOCTYPE html>
<html>
<body>
<p>will work</p>
<div class="tagsWrapper">
<div class="tagWrap">OneWordText</div>
<div class="tagWrap">multi words text</div>
</div>
<p>will not work (but there are some hacks to fix it)</p>
<div class="tagsWrapper">
<div class="tagWrap">OneWordText</div>
</div>
</body>
</html>
If none of these solutions are working for you - I'll assume that it's not possible. A browser is a pure machine that interprets code, you should tell him exactly what to do and how to calculate it
.tagWrapboxes are being made to fit in the150pxwide space available. Since, in both cases, their calculated width is too narrow for their contents, the ellipsis is applied.