How to make dynamic width in flex box row with two columns? [duplicate] - html

This question already has answers here:
Fill the remaining height or width in a flex container
(2 answers)
Closed 2 years ago.
I would like the second column out of the two, to shrink when needed so that the first column can take more space. How would I go about to do this with flex box? I would like to achieve dynamic width behavior and not enter any specific pixel values.
.container {
border: 1px solid black;
width: 300px;
}
.container .column {
border: 1px solid magenta;
}
/* Second column */
.container .column+column {}
/* Flex grid */
.flex {
display: flex;
}
.row {
display: flex;
flex-direction: row;
flex-wrap: wrap;
width: 100%;
}
.column {
display: flex;
flex-direction: column;
flex-basis: 100%;
flex: 1;
}
<div class="flex container">
<div class="row">
<div class="column">
Some very long descriptional text and some extra words here and there so to say.
</div>
<div class="column">
123£
</div>
</div>
</div>
A fiddle to fiddle around width:
https://jsfiddle.net/TheJesper/5wexr384/7/

This happens automatically with flex, so here is the minimum css you need to make it work:
.container {
border: 1px solid black;
width: 300px;
}
.flex {
display: flex;
width: 100%;
}
.column {
border: 1px solid magenta;
}
Then change your HTML:
<div class="container">
<div class="flex">
<div class="column">
// ...
</div>
<div class="column">
// ...
</div>
</div>
</div>
I always find this guide very useful:
https://css-tricks.com/snippets/css/a-guide-to-flexbox/

Related

Can I Left justify and Center justify two objects inside the same flex container? [duplicate]

This question already has answers here:
Fill the remaining height or width in a flex container
(2 answers)
Closed 2 years ago.
I have a flex-container(row), where Im looking for the first object to be left justified at a static width, and then for the next object to be centered and fill the remainder of the container.
[ (obj1) | <----------(obj2)---------> ]
I know that I could accomplish this easier with the grid styling below, but my goal here is to educate myself in flex.
display:grid;
grid-template-columns: 100px 1fr;
Thanks!
Please see the code snippets for the flex implementation.
.wrapper {
display: flex;
}
.obj-a {
background: lime;
flex-basis: 100px;
}
.obj-b {
background: skyblue;
flex-grow: 1;
display: flex;
justify-content: center;
}
<div class="wrapper">
<div class="obj-a">obj-a</div>
<div class="obj-b">obj-b</div>
</div>
yes this can be done in flex
best read is here
you need to use
flex-shrink, flex-grow, flex-basis the short form as below
flex: shrink grow basis ie. flex: 1 1 auto
below is the example I use flex short-form and added a border for representation purposes.
* {
borx-sizing: border-box;
}
.flex-container {
display: flex;
flex-direction: row;
border: 1px solid black;
height: 200px;
padding: 1em;
}
.flex-container .left {
width: 100px;
border: 2px solid red;
height: 200px;
}
.flex-container .main {
flex: 1 1 auto;
border: 2px solid green;
height: 200px;
}
<div class="flex-container">
<div class="left"></div>
<div class="main"></div>
</div>

Why are flexbox child items not stretching to fill parent container (incl. jsfiddle)? [duplicate]

This question already has answers here:
Flexbox fill available space vertically
(2 answers)
Fill remaining vertical space with CSS using display:flex
(6 answers)
Closed 4 years ago.
According to the documentation on CSS-tricks, using justify-content: stretch, a flexbox child item should stretch within its container to fill the available space.
I have a flexbox container where I set the main axis to be column direction, then in this container I place two div flexbox items. I want them to fill the available vertical space equally (so I believe flex: 0 1 auto should do it).
Here is the JS fiddle:
body {
height: 100%
}
.container {
height: 300px;
display: flex;
flex-direction: column;
justify-content: stretch;
border: 1px solid grey;
}
.in {
height: 25px;
border: 1px solid black;
flex: 0 1 auto;
}
<div class="container">
<div class="in">Inside</div>
<div class="in">
Inside
</div>
</div>
As pointed out by #Temani Afif:
body {
height: 100%
}
.container {
height: 300px;
display: flex;
flex-direction: column;
justify-content: stretch;
border: 1px solid grey;
}
.in {
height: 25px;
border: 1px solid black;
flex: 1 1 auto;
}
<div class="container">
<div class="in">Inside</div>
<div class="in">
Inside
</div>
</div>

How can I get even heights of unknown elements inside a column? [duplicate]

This question already has answers here:
Equal height flex items in flex columns
(3 answers)
Closed 5 years ago.
I'm using flex to create even columns and vh to make them the same height. That's working fine but inside the columns I can have an x number of items in them. I'd like for elements in each column to be even height depending on how many items are present (using css).
1 = 100%
2 = 50%
3 = 33.33%
etc.
I know I can do this through JS but I'd like to automate this through css via flex, grid, or something elese.
I've tried replicating your problem. Use flex: 1 on .items so that each and every item take equal space (according to the problem statement).
Have a look at the snippet below:
body {
margin: 0;
}
.parent {
width: 80%;
padding: 20px;
display: flex;
border: 1px solid red;
}
.child {
flex: 1;
display: flex;
flex-direction: column;
align-items: stretch;
justify-content: flex-end;
padding: 20px;
border: 1px solid blue;
height: 60vh;
}
.item {
flex: 1;
background: lightGreen;
border: 1px solid green;
}
<div class="parent">
<div class="child">
<div class="item">33.33%</div>
<div class="item">33.33%</div>
<div class="item">33.33%</div>
</div>
<div class="child">
<div class="item">50%</div>
<div class="item">50%</div>
</div>
<div class="child">
<div class="item">100%</div>
</div>
</div>
Hope this is what you are trying to achieve.
This is all you need to make it work with the Flexbox:
.flex-container {
display: flex;
}
.flex-item {
display: flex;
flex-direction: column;
flex: 1;
}
.item {
flex: 1;
border: 1px solid;
}
<div class="flex-container">
<div class="flex-item">
<div class="item">1/1</div>
</div>
<div class="flex-item">
<div class="item">1/2</div>
<div class="item">1/2</div>
</div>
<div class="flex-item">
<div class="item">1/3</div>
<div class="item">1/3</div>
<div class="item">1/3</div>
</div>
<div class="flex-item">
<div class="item">1/4</div>
<div class="item">1/4</div>
<div class="item">1/4</div>
<div class="item">1/4</div>
</div>
</div>

why do flexbox children have the same height? [duplicate]

This question already has answers here:
How to disable equal height columns in Flexbox?
(4 answers)
Closed 7 years ago.
I have two items inside a flex container, I gave the first item a specific height and I want the other's height to fits its actual content.
So the code is pretty simple: jsfiddle
.main {
display: -webkit-flex;
display: flex;
-webkit-flex-flow: row nowrap;
/* Safari 6.1+ */
flex-flow: row nowrap;
}
.sub {
border: 1px black solid;
flex-grow: 1;
}
#sub1 {
height: 300px;
margin-right: 50px;
}
<div class="main">
<div class="sub" id="sub1"> </div>
<div class="sub" id="sub2"> </div>
</div>
As you can see the second div stretched as the first ones's height increased, so how to prevent that, how to make the next child with no height specification hold its actual height as its actual content
Because the default value for align-items is stretch. Change that to flex-start if you want them to be top aligned and no stretch.
.main {
display: flex;
flex-flow: row nowrap;
align-items: flex-start;
}
.sub {
border: 1px black solid;
flex-grow: 1;
}
#sub1 {
height: 300px;
margin-right: 50px;
}
<div class="main">
<div class="sub" id="sub1"> </div>
<div class="sub" id="sub2"> </div>
</div>

Detect wrap with flexbox

I know, it's a weird question.
Imagine a list of items. Placed vertically. Is there a way to "detect" when my list has to be on multiple columns? (because of a lack of space) ?
I guess an example is more efficient:
http://jsfiddle.net/KCarnaille/au8mpo9t/4/
.right{
display: flex;
flex-flow: column wrap;
justify-content: flex-start; //change the option when 2 cols needed
}
I'm not talking about media queries What I want depends on items length, not screen size.
I dont think it's possible with CSS but, let's see :)
What I want :
I think you may want something like this:
.right::after {
content: ''; /* Insert pseudo-element at the end */
margin: auto; /* Push other elements as much as possible */
}
body {
display: flex;
}
.right {
display: flex;
flex-flow: column wrap;
justify-content: space-between;
background-color: black;
height: 400px;
flex: 1;
margin: 5px;
}
.right::after {
content: '';
margin: auto;
}
.thumb {
color: #fff;
border: 1px solid red;
padding: 10px;
background-color: #fff;
color: #000;
}
<div class="right">
<div class="thumb">1</div>
<div class="thumb">2</div>
<div class="thumb">3</div>
<div class="thumb">4</div>
<div class="thumb">5</div>
<div class="thumb">Layout doesn't need to be on two columns,<br/>so I change justify-content property to flex-start</div>
</div>
<div class="right">
<div class="thumb">1</div>
<div class="thumb">2</div>
<div class="thumb">3</div>
<div class="thumb">4</div>
<div class="thumb">5</div>
<div class="thumb">6</div>
<div class="thumb">7</div>
<div class="thumb">8</div>
<div class="thumb">9</div>
<div class="thumb">Layout needs to be on two columns,<br/>so I change justify-content property to space-between</div>
</div>