So I have a code like this more or less
<div class="content">
<div class="c1">
<div>
<div class="c2">
<div>
<div class="c3">
<div>
<div class="c4">
<div>
</div>
By using flex, can I do this?
<c1> <c3>
<c2> <c4>
I know I can use <ul> or <table>, but I am just wondering if I can do it using flex. I have tried using flex but cannot accomplish it.
DEMO
Yes, this is possible with Flexbox - Set a height on the .content and the child divs (e.g. content: 100px, divs 50px) and specify flex-flow: column wrap
.content {
display: flex;
align-items: center;
justify-content: center;
flex-flow: column wrap;
align-content: stretch;
height: 100px;
}
.content div {
height: 50px;
width: 50px;
}
Flex-flow is shorthand for flex-direction and flex-wrap.
Flex-direction sets the direction - row, column, row-reverse, column-reverse.
Flex-wrap wraps the flexible items (or not). By default this is nowrap, the demo uses wrap and you can also use wrap-reverse
You can achieve this by modifying your markup to the following structure:
<div class="content">
<div class="left-col">
<div class="c1"><div>
<div class="c2"><div>
</div>
<div class="right-col">
<div class="c3"><div>
<div class="c4"><div>
</div>
</div>
Now you have two columns. Simply add the following styles to your CSS:
.content {
display: flex;
justify-content: space-between; // or whatever you want
flex-direction: row; // row is the default value, so you don't need to specify it. It will place the cols beside each other.
}
.left-col, .right-col {
display: flex;
flex-direction: column;
}
By specifying flex-direction: column; the elements will be positioned below each other.
Here's some great resource: https://css-tricks.com/snippets/css/a-guide-to-flexbox/
Maybe you also want to check out the new native grid-layout: https://www.w3schools.com/css/css_grid.asp
You can easily do this with CSS grid, this is in my opinion more useful to use but this is the code that uses CSS grid
HTML
<div class="content">
<div class="c1">test 1</div>
<div class="c2">test 2</div>
<div class="c3">test 3</div>
<div class="c4">test 4</div>
</div>
CSS:
.content {
display: grid;
grid-template-columns: 1fr 1fr;
}
And what about this trick ?
.content
{
display: flex;
flex-direction: column;
height: 50px;
flex-wrap: wrap;
}
<div class="content">
<div class="c1">1</div>
<div class="c2">2</div>
<div class="c3">3</div>
<div class="c4">4</div>
</div>
Yes you can do please consider below code.
HTML
<div class="content ">
<div class="col-12 d-flex justify-content-between">
<div class="c1 col-6 text-center">c1
</div>
<div class="c2 col-6 text-center">c2
</div>
</div>
<div class="col-12 d-flex justify-content-between">
<div class="c3 col-6 text-center">c3
</div>
<div class="c4 col-6 text-center">c4
</div>
</div>
</div>
CSS
.justify-content-around {
-ms-flex-pack: distribute!important;
justify-content: space-around!important;
}
.d-flex {
display: -webkit-box!important;
display: -ms-flexbox!important;
display: flex!important;
}
Related
I want to create 3 widgets that are are equal in width and height and equal distance from each other, I understand that I must use CSS flexbox but not 100% where to start
Any help would be appreciated!
You can do this by creating a div with attributes display: flex and flex-direction: row. Then inside this div, create 3 smaller divs with your width and height, then you can use column-gap to create the gap between your widgets.
If you want to center your widgets, you can use the justify-content: center property.
Use justify-content in flex container.
Widgets Evenly spaced in the row.
.container {
display: flex;
justify-content: space-evenly;
}
.widget {
width: 100px;
height: 100px;
background: #ce8888;
text-align: center;
}
<div class="container">
<div class="widget">Widget 1</div>
<div class="widget">Widget 2</div>
<div class="widget">Widget 3</div>
</div>
Equal spaces between Widgets excluding the start and end space.
.container {
display: flex;
justify-content: space-between;
}
.widget {
width: 100px;
height: 100px;
background: #ce8888;
text-align: center;
}
<div class="container">
<div class="widget">Widget 1</div>
<div class="widget">Widget 2</div>
<div class="widget">Widget 3</div>
</div>
I am making a row with the columns, but having some problems with the width. The row has 2 columns, where the left one has a transparent pink color.
The problem is that the pink background-color is going over the <div class="flex-wrap">. I would like that everything I do within the class="flex-wrap" is staying in there. So the columns should not go over the border.
As I see it is on the row where it is wrong? I can see the margin-left: 0px;, but then the problem is on the right side, when the viewport is getting smaller.
Does somebody have a clue how to solve this?
I have made a working JSFiddle her
.flex-wrap .row {
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
flex-wrap: wrap;
}
.flex-wrap .row>[class*='col-'] {
display: flex;
flex-direction: column;
}
.section {
border: 1px solid #B9B9B9;
}
<div class="container">
<div class="section">
<div class="flex-wrap">
<div class="row">
<div class="col-xs-12 col-sm-3" style="background-color: rgba(201, 76, 76, 0.3);">
<h3>Here is a headline</h3>
</div>
<div class="col-xs-12 col-sm-9">
<img src="https://www.q95da.com/uploads/4/5/7/0/45709989/published/6391827.jpg">
</div>
</div>
</div>
</div>
</div>
This question already has answers here:
When flexbox items wrap in column mode, container does not grow its width
(9 answers)
Closed 4 years ago.
I have problem with flexbox wrapping into column. The container doesn't fit the content width as seen in the snippet below.
This works if you replace both flex-flow of .wrapper and .container with flex-flow: row wrap, the height fit the content height its children, but the problem then is that the columns then flow horizontally and appear under each other, rather than flowing vertically and beside each other.
I expect the following result:
.wrapper {
display: flex;
flex-flow: column wrap;
max-height: 500px;
max-width: 500px;
overflow: scroll;
}
.container {
display: flex;
flex-flow: column wrap;
align-content: flex-start;
background-color: red;
margin: 5px;
}
.product {
margin: 3px;
min-width: 100px;
min-height: 100px;
background-color: #ccc;
text-align: center;
font-weight: bold;
line-height: 100px;
}
<div class='wrapper'>
<div class='container'>
<div class="product">0.1</div>
<div class="product">0.2</div>
<div class="product">0.3</div>
<div class="product">0.4</div>
<div class="product">0.5</div>
<div class="product">0.6</div>
<div class="product">0.7</div>
<div class="product">0.8</div>
</div>
<div class='container'>
<div class="product">1.1</div>
<div class="product">1.2</div>
<div class="product">1.3</div>
<div class="product">1.4</div>
<div class="product">1.5</div>
<div class="product">1.6</div>
<div class="product">1.7</div>
<div class="product">1.8</div>
</div>
<div class='container'>
<div class="product">2.1</div>
<div class="product">2.2</div>
<div class="product">2.3</div>
<div class="product">2.4</div>
<div class="product">2.5</div>
<div class="product">2.6</div>
<div class="product">2.7</div>
<div class="product">2.8</div>
</div>
<div class='container'>
<div class="product">3.1</div>
<div class="product">3.2</div>
<div class="product">3.3</div>
<div class="product">3.4</div>
<div class="product">3.5</div>
<div class="product">3.6</div>
<div class="product">3.7</div>
<div class="product">3.8</div>
</div>
</div>
The problem is that the .container doesn't have a width defined, so how .wrapper does have a maximum of with and it's a Flexbox, all the children (.container) will fit automatically to their parent, that's the problem.
You can solve it by setting a with to the container class.
Something like this: width: 212px;
I have some elements I am displaying in a column direction within a display: flex container.
This places the containers within into a stacked view.
Say I have 5 stacked elements I need to make two of them display inline or flex direction row.
Without placing the two elements in question into another div that i can apply flex direction row to...
Is it possible to get these elements to sit side by side?
.flex-column {
display: flex;
flex-direction: column;
}
.flex-column .column-item:nth-of-type(4),
.flex-column .column-item:nth-of-type(5) {
width: calc(50% - 10px);
display: inline-block;
float: left;
}
.flex-column .column-item:nth-of-type(4) {
margin-right: 20px;
}
<div class="flex-column">
<div class="column-item">a</div>
<div class="column-item">b</div>
<div class="column-item">c</div>
<div class="column-item">d</div>
<div class="column-item">e</div>
</div>
I don't think it is possible once you have given your parent the flex-direction:column; property, alternatively you can enable flex-wrap:wrap and control the width of elements using the flex-basis property. This allows you to achieve the effect you want without altering your html structure.
.flex-column {
display: flex;
flex-wrap:wrap;
}
.flex-column .column-item {
flex-basis:100%;}
.flex-column .column-item:nth-of-type(4),
.flex-column .column-item:nth-of-type(5) {
flex-basis:50%;
}
<div class="flex-column">
<div class="column-item">a</div>
<div class="column-item">b</div>
<div class="column-item">c</div>
<div class="column-item">d</div>
<div class="column-item">e</div>
</div>
The layout is fairly simple with flexbox. You don't need flex-direction: column.
Just use flex-direction: row with flex-wrap: wrap.
Then make each element long enough to occupy a full row.
Reduce the flex-basis on the elements that are to share a row.
.flex-column {
display: flex;
flex-wrap: wrap;
}
.column-item {
flex: 1 0 61%; /* flex-grow, flex-shrink, flex-basis */
margin: 2px;
background-color: lightgreen;
}
.column-item:nth-of-type(4),
.column-item:nth-of-type(5) {
flex-basis: 40%;
}
<div class="flex-column">
<div class="column-item">a</div>
<div class="column-item">b</div>
<div class="column-item">c</div>
<div class="column-item">d</div>
<div class="column-item">e</div>
</div>
With flex-grow: 1 defined in the flex shorthand, there's no need to use calc().
Since flex-grow will consume free space on the row, flex-basis only needs to be large enough to enforce a wrap. In this case, with flex-basis: 61%, there's plenty of space for the margins, but never enough space for a second item.
There's an even simpler and more efficient solution using CSS Grid:
.flex-column {
display: grid;
grid-template-columns: 1fr 1fr;
grid-gap: 5px;
}
.column-item:nth-of-type(-n + 3) {
grid-column: span 2;
}
.column-item {
background-color: lightgreen;
}
<div class="flex-column">
<div class="column-item">a</div>
<div class="column-item">b</div>
<div class="column-item">c</div>
<div class="column-item">d</div>
<div class="column-item">e</div>
</div>
.flex-column {
display: flex;
flex-wrap:wrap;
}
.column-item {
}
<div class="flex-column">
<div class="column-item">a</div>
<div class="column-item">b</div>
<div class="column-item">c</div>
<div class="column-item">d</div>
<div class="column-item">e</div>
</div>
Background
At first, I wanted to narrow the space between two lines of text through line-height in the FlexBox, but line-height can't work on the flex-direction:row, it will always fill the height itself, unless I change the direction to column
Question
How can I remove filling the heights in the flexbox children when the direction is row like as column, or some articles explain the different strategies for row and column filling the heights
I had tried to use align-item: center, but it can't work, you can see my snippet, it still has the heights.
.container {
width : 200px;
height : 200px;
display : inline-flex;
flex-wrap : wrap;
}
.container_row {
flex-direction : row;
}
.box {
width : 100%;
background-color : red;
}
.container_column {
flex-direction : column;
}
.container_align_center {
align-items : center;
}
}
<h2>row</h2>
<div class="container container_row">
<div class="box">Box1</div>
<div class="box">Box2</div>
<div class="box">Box3</div>
</div>
<br/>
<h2>row with align center<h2>
<div class="container container_row container_align_center">
<div class="box">Box1</div>
<div class="box">Box2</div>
<div class="box">Box3</div>
</div>
<br/>
<h2> column </h2>
<div class="container container_column">
<div class="box">Box1</div>
<div class="box">Box2</div>
<div class="box">Box3</div>
</div>
An initial setting in a flex container is align-content: stretch.
That means that free space in the container – in the cross-axis – will be distributed evenly among flex items.
As a result, in a row-direction container, items will expand vertically consuming all available height.
You can override this default setting with flex-start, flex-end, center or other possible values.
.container {
width: 200px;
height: 200px;
display: inline-flex;
flex-wrap: wrap;
}
.container_row {
flex-direction: row;
align-content: flex-start; /* NEW */
}
.box {
width: 100%;
background-color: red;
}
.container_column {
flex-direction: column;
}
<div class="container container_row">
<div class="box">Box1</div>
<div class="box">Box2</div>
<div class="box">Box3</div>
</div>
<div class="container container_column">
<div class="box">Box1</div>
<div class="box">Box2</div>
<div class="box">Box3</div>
</div>