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

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>

Related

how to make a div wrapped in a flex container such that it allows other element to be in same row using html css?

Hi below is the html code,
.wrapper {
min-width: 0%;
max-width: 100%;
line-height: 21px;
position:relative;
overflow: hidden;
text-overflow:ellipsis;
white-space: nowrap;
max-height: 50px;
text-align:center;
}
.iconwrapper {
min-width:0;
}
<div class="wrapper">
<div class="iconwrapper">
<svg/>
</div>
<span>sometext</span>
<div>
now for some reason the div with class iconwrapper takes up complete space in the div and span appears in the next row or below in wrapper div.
how can i fix this. could someone help me with this. thanks.
try adding display: flex; property to .wrapper.
.wrapper {
...
display: flex;
}
Check the snippet. Like this you can provide.
.wrapper {
overflow: hidden;
text-overflow:ellipsis;
white-space: nowrap;
max-height: 50px;
text-align:center;
display: flex;
border: 1px solid #ddd;
}
.iconwrapper {
min-width:0;
flex: 1;
border: 1px solid #ccc;
}
<div class="wrapper">
<div class="iconwrapper">
<svg/>
</div>
<span>sometext</span>
<div>

How to make div (inside another div) horizontally and vertically align

I have 3 divs which are horizontally aligned (aqua color). Inside each div, there are two divs (red and black one).
What I am trying to do is, align the black divs horizontally regardless of the red div. The css for the black div is
.black-div {
width: 100%;
height: 45px;
max-width: 235px;
display: inline-block;
color: #33244a;
font-size: 16px;
text-transform: uppercase;
font-weight: normal;
text-align: center;
line-height: 43px;
border: 2px dashed #d5d1d8;
border-radius: 6px;
box-sizing: border-box;
}
Output will something like this
I am not good at all in css. I have tried using position: fixed / absolute but no luck.
Try it.
Use div and min-height.
section{
display: inline-block;
border: 1px solid red;
width: 100px;
}
.textarea-wrap{
overflow: hidden;
min-height: 200px;
}
.textarea-wrap > textarea{
width: 100%;
resize: none;
}
.red{
background-color: red;
}
<div>
<section>
<div class="textarea-wrap">
<textarea rows="3">12312312</textarea>
</div>
<div class="red">
red
</div>
</section>
<section>
<div class="textarea-wrap">
<textarea rows="10">12312312</textarea>
</div>
<div class="red">
red
</div>
</section>
<section>
<div class="textarea-wrap">
<textarea rows="6">12312312</textarea>
</div>
<div class="red">
red
</div>
</section>
</div>
You should use table to make it more manageable, or use absolute positioning on the black div so you can position them measure from the bottom of the blue div.
There may be a solution without the spacer. Im looking for it :)
found solution without spacer justify-content: space-between;
.wrapper {
display: flex;
flex-direction: row; /* flex in a row inside (make columns .col) */
}
.col {
display: flex;
flex-direction: column; /* flex in a column inside */
justify-content: space-between; /* since the elements must not grow, fill the space between them */
flex: 1 1 100px; /* grow and shrink of col allowed to fill row evenly starting at 100px*/
margin: 5px;
border: 3px solid black;
background-color: aqua;
}
.red {
flex: 0 1 auto; /* no vertical (col) growing (so it does not expand vertically) */
border: 3px solid black;
border-radius: 10px;
background-color: red;
margin: 5px;
padding: 10px;
}
.black {
background-color: black;
color: white;
margin: 5px;
padding: 10px;
display: block;
flex: 0 1 auto; /* no growing allowed */
}
.resize {
overflow: hidden;
resize: vertical;
}
<div class='wrapper'>
<div class='col'>
<div class='red'>Some wide wide wide wide wide wide Text</div>
<div class='black'>Footer</div>
</div>
<div class='col'>
<div class='red'>Some<br/>much<br/>longer<br/>Text</div>
<div class='black'>Footer</div>
</div>
<div class='col'>
<div class='red resize'>Some Text<br><b><u>Resize me!</u></b></div>
<div class='black'>Footer</div>
</div>
</div>
Edit removed spacer div
Edit2 added css commenting for easier understanding

Overflow area of div has no style [duplicate]

This question already has answers here:
Make background color extend into overflow area
(4 answers)
Closed 4 years ago.
I'm trying to create a table like structure that is scrollable horizontally. To do that I have a wrapper div that has overflow-x: auto, a div for each row and a div for each cell.
I want to apply a style to the row but the style is only applied to those elements that are visible.
.inner {
flex: 1 0 10em;
height: 2em;
background-color: green;
}
.outer {
border-bottom: 10px solid red;
display: flex;
}
.box {
width: 20em;
overflow-x: scroll;
}
<div class="box">
<div class="outer">
<div class="inner">1</div>
<div class="inner">2</div>
<div class="inner">3</div>
</div>
</div>
I want all of the green boxes to have a red bottom border, but the border only appears on those items that are not overflowing. What am I missing?
You may try this instead:
.inner {
flex: 1 0 10em;
width:10em; /*Specify a width */
height: 2em;
background-color: green;
}
.outer {
border-bottom: 10px solid red;
display: inline-flex; /* to take the width of content and not container*/
}
.box {
width: 20em;
overflow-x: scroll;
}
<div class="box">
<div class="outer">
<div class="inner">1</div>
<div class="inner">2</div>
<div class="inner">3</div>
</div>
</div>
I dont know exactly what you mean, but I hope this helps:
Since the CSS you use only for the outer,It does just that and put it only for the part that is visible. To achieve bottom red border for all of them, you have to put the border on the inner part.
HTML:
<div class="box">
<div class="outer">
<div class="inner">1</div>
<div class="inner">2</div>
<div class="inner">3</div>
</div>
</div>
CSS:
.inner {
flex: 1 0 10em;
height: 2em;
background-color: green;
border-bottom: 10px solid red;
}
.outer {
width: 20em;
display: flex;
}
.box {
width: 20em;
overflow-x: scroll;
}
Hope it helps :)

Divs inside of Div Fill Entire Parent DIV Without Display:Table [duplicate]

This question already has answers here:
Fill the remaining height or width in a flex container
(2 answers)
Closed 5 years ago.
I am trying to create something similar to a table with rows and cells, but I don't want to use rows and cells, I am currently using DIV elements and everything is going smooth until I try to adjust the width of the inner DIV elements to fill the width of their parent DIV (which would be the row) without having to do exact calculations for the percentage needed.
.container {
width: 100%;
display: flex;
border: 5px solid black;
}
.container > div {
width: inherit;
display: inline-flex;
border: 5px solid red;
}
.container > div > div {
border: 5px solid green;
height: 50px
}
<div class="container">
<div>
<div>
<span>TEST</span>
</div>
<div>
<span>TEST</span>
</div>
</div>
</div>
From MDN:
The flex CSS property is a shorthand property specifying the ability
of a flex item to alter its dimensions to fill available space.
You need to provide flex: 1; to .container > div > div to expand the inner div elements.
.container > div > div {
border: 5px solid green;
height: 50px
}
Demo
You can use the flex property:
.container {
width: 100%;
display: flex;
border: 5px solid black;
}
.container>div {
width: inherit;
display: inline-flex;
border: 5px solid red;
}
.container>div>div {
border: 5px solid green;
height: 50px;
flex: 1;
}
<div class="container">
<div>
<div>
<span>TEST</span>
</div>
<div>
<span>TEST</span>
</div>
</div>
</div>
perhaps this is what you need? (innermost div should inherit width too)
.container {
width: 100%;
display: flex;
border: 5px solid black;
}
.container > div {
width: inherit;
display: inline-flex;
border: 5px solid red;
}
.container > div > div {
width: inherit;
border: 5px solid green;
height: 50px
}
<div class="container">
<div>
<div>
<span>TEST</span>
</div>
<div>
<span>TEST</span>
</div>
</div>
</div>
Here i make a change on your CSS but you need to do a flex when you try to autogrow your divs. Look at the .container > div > div styling.
.container {
width: 100%;
display: flex;
border: 5px solid black;
}
.container > div {
width: inherit;
display: inline-flex;
border: 5px solid red;
}
.container > div > div {
border: 5px solid green;
height: 50px;
flex: 1;
}
<div class="container">
<div>
<div>
<span>TEST</span>
</div>
<div>
<span>TEST</span>
</div>
</div>
</div>

Position element at the bottom with prior element filling up height

First div should fill up remaining height that's left while second div should be positioned at the bottom with it's initial height.
DEMO:
.container {
width: 240px;
height: 400px;
background: #E0E0E0;
display: flex;
flex-direction: column;
}
.first {
border :1px solid black;
padding: 1em;
box-sizing: border-box;
}
.second {
border: 1px solid blue;
padding: 1em;
box-sizing: border-box;
}
<div class="container">
<div class="first">
I SHOULD FILL WHATS REMAINING AFTER SECOND ONE
</div>
<div class="second">
<div>
I SHOULD BE AT THE BOTTOM FILLING ONLY MY OWN HEIGHT
</div>
</div>
The answer to this would vary from markup to markup, but in your case you can just add this to your first element:
height: 100%;
This works because of your flex display property of the container. A different property on the container would likely require another solution.
Demo
Full code
.container {
width: 240px;
height: 400px;
background: #E0E0E0;
display: flex;
flex-direction: column;
}
.first {
height: 100%;
border :1px solid black;
padding: 1em;
box-sizing: border-box;
}
.second {
border: 1px solid blue;
padding: 1em;
box-sizing: border-box;
}
<div class="container">
<div class="first">
I SHOULD FILL WHATS REMAINING AFTER SECOND ONE
</div>
<div class="second">
<div>
I SHOULD BE AT THE BOTTOM FILLING ONLY MY OWN HEIGHT
</div>
</div>
You need to make height auto to container class so depend on length of string your height is increase.
<style>
.container {
width: 240px;
height: auto;
background: #E0E0E0;
display: flex;
flex-direction: column;
}
.first {
border :1px solid black;
padding: 1em;
box-sizing: border-box;
}
.second {
border: 1px solid blue;
padding: 1em;
box-sizing: border-box;
}
</style>
<div class="container">
<div class="first">
I SHOULD FILL WHATS REMAINING AFTER SECOND ONE
</div>
<div class="second">
<div>
I SHOULD BE AT THE BOTTOM FILLING ONLY MY OWN HEIGHT
</div>
</div>