Why does text-overflow not work inside nested flex-box? [duplicate] - html

This question already has answers here:
Why don't flex items shrink past content size?
(5 answers)
Closed 11 months ago.
My overflow text has two parents, each have display: flex. The overflow with ellipsis seems only to work if on parent is removed. Otherwise the text keeps it width and determines the parent width. You can test it in the fiddle by reducing the browser width. What is wrong?
jsfiddle
.wrap {
display: flex;
flex-wrap: wrap;
align-items: center;
background: lightgrey;
max-width: 600px;
}
p {
text-overflow: ellipsis;
white-space: nowrap;
overflow: hidden;
margin-right: 20px;
}
<div class="wrap">
<div class="wrap">
<p>
1) looooooooooooooooooooooooooooooooooooong text
</p>
</div>
</div>

A very common problem when we try to make CSS shortcut for text is: when we try to combine flex-box layout (display: flex;) with text-overflow: ellipsis;.
The solution for this problem is to use min-width: 0; for parent element that we want to shortcut text.
So you can simply use min-width: 0px; in your parent div.
like
.wrap{
display: flex;
flex-wrap: wrap;
align-items: center;
background: lightgrey;
min-width: 0px; // use 0px in min width
}
try this your problem will be solved.

You have to define an explicit with (in this case 100%) on the flex containers.
It will work with this code:
.wrap {
display: flex;
flex-wrap: wrap;
align-items: center;
background: lightgrey;
max-width: 600px;
width: 100%;
}
p {
text-overflow: ellipsis;
white-space: nowrap;
overflow: hidden;
margin-right: 20px;
display: block;
width: 100%;
}
<div class="wrap">
<div class="wrap">
<p>
1) looooooooooooooooooooooooooooooooooooong text
</p>
</div>
</div>

Related

Horizontal scroll not working as expected

Hello I am trying to implement horizontal scrolling (using scroll bars)but I can't get it to work properly
I have 2 divs 1 for scroll and 1 main div I want to display the items on the main div but when I do that the whole div scrolls, instead of only the items. How can I get this to work?
scroll div
<div class="scrolling-wrapper">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
main div
<div class="main">
</div>
CSS
.scrolling-wrapper {
overflow-x: scroll;
overflow-y: hidden;
white-space: nowrap;
}
.item {
display: inline-block;
align-self: flex-start;
background-color: var(--pampas);
border-radius: 10px;
flex-shrink: 0;
height: 269px;
margin-left: 210px;
margin-top: 13px;
width: 200px;
}
.main {
align-items: center;
background-color: white;
display: flex;
flex-direction: column;
height: auto;
justify-content: flex-start;
min-height: 1080px;
padding: 26px 0;
width: 1920px;
}
Here is how it looks right now.
Thank you <3
If I understand your question correctly, you want all the items to scroll together (i.e. not each one individually) but only inside div.scrolling-wrapper which is a child of div.main.
This only works if you restrict the width of div.scrolling-wrapper.
You most likely need
.scrolling-wrapper {
width: 100%;
/* you can also use max-width instead */
}
The 100% refer to the parent's width, in this case that of div.main. This is necessary because you have display:flex set on div.main which has the effect that its child div.scrolling-wrapper gets the sum of its children' (the .items) widths as its default width.
As a sidenote: Please mind that the following rules related to flexbox that you set on .item:
align-self: flex-start;
flex-shrink: 0;
don't have any effect unless there is some rule that you didn't mention in your original post that sets .scrolling-wrapper to display:flex.

Build cards in CSS

I want to build this kind of cards, that are in the same row, and which can be scroll through horizontally. Can you recommend any tool for doing so?
Cards
I'm using display:flex on my container but it doesn't do it.
You can set the overflow -x auto of the parent div container of the card, make it a flex container and flex-wrap with nowrap.
something like this
.parent-container {
display: flex;
width: 100%;
overflow-x: auto;
flex-wrap: nowrap;
}
.card-container {
min-width: 300px;
height: 380px;
background: green;
margin: 30px;
}
<div class="parent-container">
<div class="card-container"></div>
<div class="card-container"></div>
...
<div class="card-container"></div>
<div class="card-container"></div>
</div>
you can use display-flex; to parent div and with flex-direction: row;
to scroll add overflow-x: auto; scroll-behaviour: smooth; So it will adjust with you div element width;

CSS text-overflow property

I want to show the text an ellipsis to the user.
I use the text-overflow property.
Text must be next to numbers, but text is long.
If I define the width, there is a large space, which is ugly. If I put the width auto, the text is long
This is code:
.holder-new {
position: relative;
bottom: 0;
width: 90%;
background: red;
height: 50px;
padding-top: 0;
margin: 0 auto;
display: flex;
justify-content: center;
flex-direction: column;
}
.holder-content {
width: auto;
display: inline-flex;
}
.name {
padding: 0!important;
width: auto!important;
padding-right: 5px!important;
text-overflow: ellipsis;
overflow: hidden;
white-space: nowrap;
display: inline-block;
}
.duration {
padding: 0!important;
width: 15%!important;
}
<div class="holder-new">
<div class="holder-content">
<div class="name"><span>The text-overflow property </span></div>
<div class="duration"><span>1:02</span></div>
</div>
</div>
<div class="holder-new">
<div class="holder-content">
<div class="name"><span>The text-overflow property specifies how overflowed content that is not displayed should be signaled to the user </span></div>
<div class="duration"><span>1:02</span></div>
</div>
</div>
Demo:
https://jsfiddle.net/xpvt214o/689404/
Just set a fixed px or percentage width for the right DIV (the time) and set width: auto for the left DIV (i.e. the text). If you use a pixel value for the right DIV, it will work for all container sizes / be responsive:
https://jsfiddle.net/em2svpko/

Fix display value changing on child of flexbox. Inline-block being overridden

I've recently starting using flexbox and this is the first problem I've run into. I want my .wrp class below to remain display: inline-block; but one line seems to disable this value. That line is: flex-direction: column. When I remove that line my .wrp class starts behaving like an inline-block element again but then of course it loses it's flex-direction value. Is there a simple solution that doesn't require restructuring my HTML too much to keep the flex-direction behavior of flexbox but also keep the inline-block behavior on .wrp?
.contr {
display: flex;
flex-direction: column; /* this line seems to be breakig my display on .wrp */
justify-content: center;
height: 10rem;
background-color: #eee;
}
.wrp {
display: inline-block;
height: 5rem;
background-color: #ddd;
}
p {
width: 100%;
background-color: #ccc;
}
<div class="contr">
<div class="wrp">
<p>I want this paragraph to stretch to fit it's content. Not full width.</p>
</div>
</div>
You can't have an inline-block element within a flex. It looks like you may be looking for display: inline-table:
.contr {
display: flex;
flex-direction: column; /* this line seems to be breakig my display on .wrp */
justify-content: center;
height: 10rem;
background-color: #eee;
}
.wrp {
display: inline-table;
height: 5rem;
background-color: #ddd;
}
p {
width: 100%;
background-color: #ccc;
}
<div class="contr">
<div class="wrp">
<p>I want this paragraph to stretch to fit it's content. Not full width.</p>
</div>
</div>
Hope this helps! :)

Vertically centering with flexbox

I'm trying to center a div on a webpage using flexbox. I'm setting the following CSS properties. I see that it's being centered horizontally, but not vertically.
.flex-container {
display: flex;
align-items: center;
justify-content: center;
}
Here's the fiddle: JSFIDDLE
Can you explain what I'm doing wrong?
A <div> element without an explicit height defaults to the height of it's contents, as all block elements do. You'd probably want to set it to 100% of it's parent, the <body>, but that's not enough, since that is also a block element. So again, you need to set that to 100% height, to match it's parent, the <html>. And yet again, 100% is still required.
But once all that is done, you get that annoying vertical scroll bar. That's a result of the default margin the body has, and the way the box model is defined. You have several ways you can combat that, but the easiest is to set your margins to 0.
See corrected fiddle.
html, body {
height: 100%;
margin: 0px;
}
.flex-container {
display: flex;
align-items: center;
justify-content: center;
height: 100%;
}
.item {
background-color: blue;
height: 50px;
width: 50px;
}
<div class="flex-container">
<div class="item">
</div>
</div>
You just need to set html, body, and your flex container to height: 100%. The reason it wasn't working is that your flex container didn't have an explicit height set, so it defaulted to the height of its contents.
Live Demo:
html, body {
height: 100%;
}
.flex-container {
display: flex;
align-items: center;
justify-content: center;
height: 100%;
}
.item {
background-color: blue;
height: 50px;
width: 50px;
}
<div class="flex-container">
<div class="item">
</div>
</div>
JSFiddle Version: http://jsfiddle.net/d4vkq3s7/3/