Element is not 100% width in div - html

I have a div with some elements. I want these elements to be 100% width of the container div.
.container {
height: 100px;
width: 100px;
display: flex;
flex-direction: column;
overflow: scroll;
border: 1px solid black;
}
.inner {
width: 100%;
background-color: gray;
white-space: nowrap;
}
<div class="container">
<span class="inner">LONG TEXT LONG TEXT LONG TEXT LONG TEXT LONG TEXT</span>
<span class="inner">XXX</span>
<span class="inner">XXX</span>
</div>
As you can see if you horizontally scroll, the background is not 100% complete.

update your code like below:
.container {
height: 100px;
width: 100px;
display: flex;
flex-direction: column;
overflow: scroll;
border: 1px solid black;
align-items:flex-start; /* disable the stretch alignment*/
}
.inner {
min-width: 100%; /* min-width instead of width*/
background-color: gray;
white-space: nowrap;
}
<div class="container">
<span class="inner">LONG TEXT LONG TEXT LONG TEXT LONG TEXT LONG TEXT</span>
<span class="inner">XXX</span>
<span class="inner">XXX</span>
</div>
Or like below if you want all elements to get full coloration
.container {
height: 100px;
width: 100px;
overflow: scroll;
border: 1px solid black;
}
.container>div {
min-width: 100%;
display: inline-block;
}
.inner {
display:block;
background-color: gray;
white-space: nowrap;
}
<div class="container">
<div>
<span class="inner">LONG TEXT LONG TEXT LONG TEXT LONG TEXT LONG TEXT</span>
<span class="inner">XXX</span>
<span class="inner">XXX</span>
</div>
</div>

Here's the flex solution.
Replace flex-direction: column with flex-flow: column wrap in .container. And remove width: 100% from .inner.
And you get the desired result.
.container {
height: 100px;
width: 100px;
display: flex;
/*flex-direction: column;*/
flex-flow: column wrap;
overflow: scroll;
border: 1px solid black;
}
.inner {
/*width: 100%;*/
background-color: gray;
white-space: nowrap;
}
<div class="container">
<span class="inner">LONG TEXT LONG TEXT LONG TEXT LONG TEXT LONG TEXT</span>
<span class="inner">XXX</span>
<span class="inner">XXX</span>
</div>

UPDATE: I have came up with this non-flexbox solution (A curiosity driven actioin) Initially I misunderstood the question.
SOLUTION 1: Adding a wrapper div around your spans.
.container {
height: 100px;
width: 100px;
border: 1px solid black;
overflow-x:scroll;
}
.wrapper{
background:orange;
height:auto;
width:max-content;
}
.inner{
display:block;
min-width:100px;
}
<div class="container">
<div class="wrapper">
<span class="inner">LONG TEXT LONG TEXT LONG TEXT LONG TEXT LONG TEXT</span>
<span class="inner">XXX</span>
<span class="inner">XXX</span>
</div>
</div>

Just put a display: block in the span elements because their default display is inline, and that's why they behave the way they are behaving,

Related

CSS white-space nowrap expand flexbox's child width

I'm learning CSS and I'm having a problem with text-overflow. I have a flex-box with two child. I want child 2 fit into its parent and the text-content will be cut if it too long. But when I add white-space: nowrap to text then child-2 width is expanded. What did I do wrong here ?
.parent {
display: flex;
flex-wrap: nowrap;
max-width: 200px;
margin-right: 20px;
margin-bottom: 20px;
padding: 15px;
border: 1px solid black;
}
.child-1 {
white-space: nowrap;
border: 1px solid black;
margin-right: 20px;
}
.text {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
border: 1px solid red;
}
<div class="parent">
<div class="child-1">This is child 1</div>
<div class="child-2">
<div class="text">A really long long long long text</div>
<div class="text">Another really long long long long text</div>
</div>
</div>
That's what white-space: nowrap does - it stops the text from breaking at white space and wrapping to a new line.
Your .parent is set at a max-width: 200px so child-2 it cannot grow to to fit the longer text, and the single line is too long to fit into the space beside child-1, so it has to extend outside the parent - there is nowhere else for it to go.
I presume what you want to do is hide the part of the text that is extending out from the parent?
To do that you can use overflow to hide any content that extends outside of the child-2 div like this:
.child-2 { overflow:hidden; }
See more about the overflow property here: Mozilla MDN Web Docs
Example hiding the overflow:
.parent {
display: flex;
flex-wrap: nowrap;
max-width: 200px;
margin-right: 20px;
margin-bottom: 20px;
padding: 15px;
border: 1px solid black;
}
.child-2{
overflow:hidden;
}
.child-1 {
white-space: nowrap;
border: 1px solid black;
margin-right: 20px;
}
.text {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
border: 1px solid red;
}
<div class="parent">
<div class="child-1">This is child 1</div>
<div class="child-2">
<div class="text">A really long long long long text</div>
<div class="text">Another really long long long long text</div>
</div>
</div>

Text-overflow:ellipsis and Flexbox

I want to have a box with text and an Icon inside. The Icon is supposed to stick to the right side of the box, the text to the left.
If the text is too long to fit into the box next to the icon, I want it to be shortend by the text-overflow:ellipsis property. I do not insist on implementing it with Flex-Box, if there is a better way to do it.
It should look like this:
And this is what I achieved so far:
div#Wrapper {
border: 0.2em solid black;
width: 6em;
padding: 0.5em;
display: flex;
}
span#Text {
white-space: nowrap;
text-overflow: ellipsis;
}
span#Icon {
background-color: blue;
border-radius: 50%;
height: 1em;
width: 1em;
display: inline-block;
margin-left: auto;
}
Short Text:
<div id="Wrapper">
<span id="Text">
This
</span>
<span id="Icon">
</span>
</div>
<br><br><br>
Text:
<div id="Wrapper">
<span id="Text">
This is Text
</span>
<span id="Icon">
</span>
</div>
<br><br><br>
Long Text:
<div id="Wrapper">
<span id="Text">
This is long long long Text
</span>
<span id="Icon">
</span>
</div>
You can add flex: 1 and overflow: hidden properties on text element.
div#Wrapper {
border: 0.2em solid black;
width: 6em;
padding: 0.5em;
display: flex;
}
span#Text {
flex: 1;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
span#Icon {
background-color: blue;
border-radius: 50%;
height: 1em;
width: 1em;
margin-left: auto;
}
<div id="Wrapper">
<span id="Text">
This is long long long Text
</span>
<span id="Icon"></span>
</div>
I have replaced the ID's with classes since the ID must be unique in the document. Furthermore, I added overflow: hidden for the text and flex: 0 0 auto for the icon.
div#Wrapper {
border: 0.2em solid black;
width: 6em;
padding: 0.5em;
display: flex;
}
span.Text {
white-space: nowrap;
text-overflow: ellipsis;
overflow: hidden; /* Added */
}
span.Icon {
background-color: blue;
border-radius: 50%;
height: 1em;
width: 1em;
display: inline-block;
margin-left: auto;
flex: 0 0 auto; /* Added */
}
Short Text:
<div id="Wrapper">
<span class="Text">
This
</span>
<span class="Icon">
</span>
</div>
<br><br><br> Text:
<div id="Wrapper">
<span class="Text">
This is Text
</span>
<span class="Icon">
</span>
</div>
<br><br><br> Long Text:
<div id="Wrapper">
<span class="Text">
This is long long long Text
</span>
<span class="Icon">
</span>
</div>
Here you go.
https://jsfiddle.net/hxz05g9j
Another approach is make circle as :after css.

Fixed, match content and fill in same flex container [duplicate]

This question already has answers here:
Why don't flex items shrink past content size?
(5 answers)
Closed 4 years ago.
I would like to make the following layout:
<div class="main">
<div class="container">
<div class="first">
fixed size
</div>
<div class="second">
<span>very very very very very very very long text</span>
<span>other text</span>
</div>
<div class="third">
1000000000000000
</div>
</div>
</div>
to look like this:
Container should have three divs inside:
first div with fixed size
third div with width matching content
second div filling the remaining container witdh. Inside that div there are two spans. The first span should be the same with as it's parent and contain very long line of text that should be dotted if it cannot fit.
I tried the following css code:
* {
box-sizing: border-box;
}
.main {
width: 500px;
height: 500px;
border: 1px solid black;
padding: 10px;
}
.container {
display: flex;
width: 100%;
}
.container div {
display: flex;
border: 1px solid black;
}
.first {
flex: 0 0 100px;
}
.second {
flex-direction: column;
flex: 1;
}
span {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
border: 1px solid black;
}
.third {
flex: 0 1 auto;
}
but the result is not as I would like because the elements are overflowing outside of container and long text is not dotted.
What should I changed in my css to make this work?
Demo: https://stackblitz.com/edit/js-vnr21c
You need to set min-width:0 on the span's parent
* {
box-sizing: border-box;
}
.main {
width: 500px;
height: 500px;
border: 1px solid black;
padding: 10px;
}
.container {
display: flex;
}
.container div {
border: 1px solid black;
}
.first {
flex: 0 0 100px;
}
.second {
display: flex;
flex-direction: column;
min-width: 0;
}
span:first-child {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
border: 1px solid black;
}
.third {
flex: 0 1 auto;
}
<div class="main">
<div class="container">
<div class="first">
fixed size
</div>
<div class="second">
<span>very very very very very very very very long text</span>
<span>other text</span>
</div>
<div class="third">
1000000000000000
</div>
</div>
</div>

Make div not expand parent

I searched for this problem for quite a while now, but only found solutions to the opposite problem.
So here is my problem:
I have a side panel that should be only as wide as its content. This panel has a header with a potentially long title. That header should not expand the panel, but instead be ellipsed.
The HTML looks similar to this
<div class="outer">
<div class="inner">
<div class="header">
superlongtextthatshouldbeellipsed
</div>
<div class="line">
short text
</div>
<div class="line">
even shorter text
</div>
</div>
</div>
This is the CSS to demonstrate the problem
.outer {
background-color: #FFAAAA;
display: flex;
flex-direction: row;
}
.inner {
background-color: #AAAAFF;
display: flex;
flex-direction: column;
margin: 5px;
}
.header {
margin: 5px;
background-color: #AAFFAA;
white-space: nowrap;
overflow: none;
text-overflow: ellipsis;
}
.line {
margin: 5px;
background-color: #FFAAFF;
}
https://jsfiddle.net/1yn725hy/9/
In the fiddle the blue box should be as wide as the second purple box (+margin). The text in the green box should be ellipsed.
How do I do this?
EDIT: Just to clarify: The blue box should fit the content of the purple box which has a varying size. A fixed width does not solve the problem.
First of all your container has to have max-width or width fixed. Second of all your overflow has to be hidden instead of none:
.outer {
background-color: #FFAAAA;
display: flex;
flex-direction: row;
}
.inner {
background-color: #AAAAFF;
display: flex;
flex-direction: column;
margin: 5px;
}
.header {
margin: 5px;
background-color: #AAFFAA;
white-space: nowrap;
max-width:200px;
overflow: hidden;
text-overflow: ellipsis;
}
.line {
margin: 5px;
background-color: #FFAAFF;
}
<div class="outer">
<div class="inner">
<div class="header">
superlongtextthatshouldbeellipsed
</div>
<div class="line">
short text
</div>
<div class="line">
even shorter text
</div>
</div>
</div>
Ok, finally solved it myself. The trick is to use the almighty flexbox and wrap the header in it:
.outer {
background-color: #FFAAAA;
display: flex;
flex-direction: row;
}
.inner {
background-color: #AAAAFF;
display: flex;
flex-direction: column;
margin: 5px;
}
.header {
margin: 5px;
background-color: #AAFFAA;
display: flex;
}
.header2 {
width: 0px;
flex-grow: 1;
text-overflow: ellipsis;
white-space: nowrap;
overflow: hidden;
}
.line {
margin: 5px;
background-color: #FFAAFF;
}
<div class="outer">
<div class="inner">
<div class="header">
<div class="header2">
superlongtextthatshouldbeellipsed
</div>
</div>
<div class="line">
short text
</div>
<div class="line">
even shorter text
</div>
</div>
</div>
You should add some width to your div. Otherwise the header text wont ellipsed, because there is no "end".
https://jsfiddle.net/1yn725hy/14/

Prevent text from moving on hover

The span appears when hovering over the container div, but it pushes the previous text to the left, how can I prevent that?
The height, width and border on the wrapper are just there to demonstrate the effect.
#wrapper {
text-align: center;
width: 500px;
height: 200px;
border: 1px solid black;
}
.element {
display: inline-block;
}
.element > span {
display: none;
}
.element:hover > span {
display: inline;
}
<div id="wrapper">
<div class="element">Some Sample Text <span>Some hovered text</span></div>
<br>
<div class="element">Some Sample Text <span>Some hovered text</span></div>
<br> ...
</div>
You can set position: absolute; to the span, also add white-space: nowrap; to the container to prevent wrapping as needed.
#wrapper {
text-align: center;
width: 500px;
height: 200px;
border: 1px solid black;
}
.element {
display: inline-block;
white-space: nowrap; /* NEW */
}
.element > span {
display: none;
position: absolute; /* NEW */
}
.element:hover > span {
display: inline;
}
<div id="wrapper">
<div class="element">Some Sample Text <span>Some hovered text</span></div>
<br>
<div class="element">Some Sample Text <span>Some hovered text</span></div>
<br> ...
</div>