How to display 2 columns per row using flexbox - html

I'm trying to display 2 columns every row but I can't seem to get it right at the moment.
What i'm trying to replicate is this:
but i'm not sure on how to handle this with using flexbox
.flex {
display: flex;
flex-direction: row;
flex-basis: 100%;
flex: 1;
}
.box {
padding: 20px;
border: 1px solid black;
width: 100px;
height: 100px;
margin: 10px;
}
.red {
background-color: red;
}
.yellow {
background-color: yellow;
}
.blue {
background-color: blue;
}
.green {
background-color: green;
}
<div class="flex">
<div class="box green">positive 1</div>
<div class="box yellow">positive 2</div>
<div class="box blue">positive 3</div>
<div class="box red">negative 1</div>
</div>
https://jsfiddle.net/1a9qLx5w/

The best way to achieve this layout would be with Grid CSS:
.flex {
display: grid;
grid-auto-flow: column;
grid-gap: 20px;
grid-template-rows: 100px 100px;
grid-template-columns: 100px 100px;
padding: 10px;
}
.box {
padding: 20px;
border: 1px solid black;
}
.red {
background-color: red;
}
.yellow {
background-color: yellow;
}
.blue {
background-color: blue;
}
.green {
background-color: green;
}
* {
box-sizing: border-box;
}
<div class="flex">
<div class="box green">positive 1</div>
<div class="box yellow">positive 2</div>
<div class="box blue">positive 3</div>
<div class="box red">negative 1</div>
</div>
But since you're asking for a flexbox solution, here you go:
.flex {
display: flex;
flex-direction: column;
flex-wrap: wrap;
height: 240px;
align-content: flex-start;
}
.box {
flex: 0 0 100px;
width: 100px;
padding: 20px;
border: 1px solid black;
margin: 10px;
}
.red {
background-color: red;
}
.yellow {
background-color: yellow;
}
.blue {
background-color: blue;
}
.green {
background-color: green;
}
* {
box-sizing: border-box;
}
<div class="flex">
<div class="box green">positive 1</div>
<div class="box yellow">positive 2</div>
<div class="box blue">positive 3</div>
<div class="box red">negative 1</div>
</div>

Working demo :
.flex {
display: flex;
flex-direction: row;
flex-basis: 100%;
flex: 1;
}
.box {
padding: 20px;
border: 1px solid black;
width: 100px;
height: 100px;
margin: 10px;
}
.red {
background-color: red;
}
.yellow {
background-color: yellow;
}
.blue {
background-color: blue;
}
.green {
background-color: green;
}
.row {
display: flex;
flex-direction: row;
}
<div class="row">
<div class="box green">positive 1</div>
<div class="box yellow">positive 2</div>
</div>
<div class="row">
<div class="box blue">positive 3</div>
<div class="box red">negative 1</div>
</div>

I just copied your example:
.row{
display: flex;
flex-direction: row;
justify-content: center;
}
.green{
padding: 15px;
border: solid 1px green;
}
.red{
padding: 15px;
border: solid 1px red;
}
.col{
margin-right: 15px;
}
<div class="row">
<div class="col">
<p class="green">Positive 1</p>
<p class="green">Positive 2</p>
</div>
<div class="col">
<p class="red">No Thanks</p>
</div>
</div>

Related

CSS Flexbox align

I am currently learning HTML, CSS, how am I going to output this?
HTML Code
<body>
<div class="container">
<div id=“one"> 1</div>
<div id=“two"> 2</div>
<div id="three">3</div>
<div id=“four"> 4</div>
<div id=“five"> 5</div>
</div>
</body>
CSS Code
.container {
width: 240px;
height: 200px;
border: 1px solid gray;
}
.container > div {
width: 80px;
height: 50px;
border: 1px solid red;
}
I knew that I have to use flexbox to do that, but I have no idea how to change it, below is my modification of the CSS, but the result is wrong.
.container {
width: 240px;
height: 200px;
border: 1px solid gray;
display: inline-flex;
flex-direction: column-reverse;
flex-wrap: wrap;
align-content: flex-start;
}
.container > div {
width: 80px;
height: 50px;
border: 1px solid red;
}
You need flex-wrap: wrap-reverse; align-content: flex-start;
.container {
width: 240px;
height: 200px;
border: 1px solid gray;
display: flex;
flex-wrap: wrap-reverse;
align-content: flex-start;
}
.container>div {
width: 80px;
height: 50px;
border: 1px solid red;
box-sizing: border-box; /* don't forget this */
}
<div class="container">
<div id="one"> 1</div>
<div id="two"> 2</div>
<div id="three">3</div>
<div id="four"> 4</div>
<div id="five"> 5</div>
</div>

Html CSS - div center, and left, right alignment [duplicate]

This question already has answers here:
Force flex item to span full row width
(2 answers)
Closed 2 years ago.
How do I make the top div of the full horizontal length of the main container, while keeping the next two div, .left and .right in flex to each other?
To look like this -
.main {
border: 1px solid red;
display: inline-flex;
}
.main div.top {
border: 1px solid orange;
width: auto;
display: inline-block;
}
.main div.left {
border: 1px solid blue;
}
.main div.right {
border: 1px solid green;
}
<html>
<body>
<div class="main">
<div class="top">
<h1>top</h1>
</div>
<div class="left">
<h1>left</h1>
</div>
<div class="right">
<h1>right</h1>
</div>
</div>
</body>
</html>
Another way to do this is with grid:
.main {
display: grid;
grid-template-columns: 1fr 1fr;
}
.main .top {
grid-column: 1/3;
}
.main {
border: 1px solid red;
display: grid;
grid-template-columns: 1fr 1fr;
grid-gap: 4px;
padding: 4px;
}
.main .top {
border: 1px solid orange;
grid-column: 1/3;
}
.main .left {
border: 1px solid blue;
}
.main .right {
border: 1px solid green;
}
<div class="main">
<div class="top">
<h1>top</h1>
</div>
<div class="left">
<h1>left</h1>
</div>
<div class="right">
<h1>right</h1>
</div>
</div>
Use display: flex; and flex-wrap: wrap on the parent container, 100% width on the first child and flex-grow: 1 on the other children, or also use percentage widths on the second and third DIVs.
* {
box-sizing: border-box;
}
.main {
border: 1px solid red;
display: flex;
flex-wrap: wrap;
}
.main div.top {
border: 1px solid orange;
width:100%;
}
.main div.left {
border: 1px solid blue;
width: 40%;
}
.main div.right {
border: 1px solid green;
width: 60%;
}
<html>
<body>
<div class="main">
<div class="top"> <h1>top</h1>
</div>
<div class="left"> <h1>left</h1>
</div>
<div class="right"> <h1>right</h1>
</div>
</div>
</body>
</html>
Any width of .left and .right
.main {
border: 1px solid red;
display: flex;
flex-wrap: wrap;
}
.main div.top {
border: 1px solid orange;
width: auto;
display: inline-block;
flex: 1 1 100%;
}
.main div.left {
border: 1px solid blue;
flex: 1 1 auto;
}
.main div.right {
border: 1px solid green;
flex: 1 1 auto;
}
<div class="main">
<div class="top">
<h1>top</h1>
</div>
<div class="left">
<h1>left 11111111</h1>
</div>
<div class="right">
<h1>right</h1>
</div>
</div>
You can try something like this:
I just added 2 extra divs if that is not an issue?
#MainDiv {
width: 200px;
border: 1px solid red;
}
.main {
width: auto;
display: flex;
flex-direction: column;
}
#lower {
display: flex;
flex-direction: row;
}
.left,
.right {
width: 100px;
border: 1px solid black;
}
<html>
<body>
<div id="MainDiv">
<div class="main">
<div class="top">
<h1>top</h1>
</div>
<div id="lower">
<div class="left">
<h1>left</h1>
</div>
<div class="right">
<h1>right</h1>
</div>
</div>
</div>
</div>
</body>
</html>
You can two div to create additional sections.
.main {
border: 1px solid red;
display: flex;
flex-direction:column;
}
.main div.top {
border: 1px solid orange;
width:auto;
}
.main div.left {
border: 1px solid blue;
flex:1
}
.main div.right {
border: 1px solid green;
flex:1
}
.main__section2 {
display:flex
}
<html>
<body>
<div class="main">
<div class="main__section1">
<div class="top">
<h1>top</h1>
</div>
</div>
<div class="main__section2">
<div class="left">
<h1>left</h1>
</div>
<div class="right">
<h1>right</h1>
</div>
</div>
</div>
</body>
</html>

Creating a layout with Flexbox

hey I'm new in Flexbox and I'm trying to get it as best as I can. However i faces a problem with some heights and orders, maybe some here could help out.
Note: Don't suggest using Grid/tables please.
this is what I have right now:
this is what I want to get:
html:
<div class="movie-container">
<div class="upper-container">
<div class="image">Image</div>
<div class="title">Title</div>
<div class="more">More</div>
</div>
<div class="lower-container">
<div class="runtime">Runtime</div>
<div class="description">Description</div>
<div class="director">Director</div>
</div>
</div>
css:
.movie-container{
display:flex;
flex-flow: column wrap;
}
.upper-container {
display: flex;
width:80%;
margin:0 auto;
flex-flow: raw wrap;
}
.upper-container div {
border: 1px solid black;
padding: 10px;
}
.lower-container {
display: flex;
width:80%;
margin:0 auto;
flex-flow: column wrap;
}
.lower-container div {
border: 1px solid black;
padding: 10px;
}
.image {
flex: 1;
}
.title {
flex: 3;
}
.more {
flex: 0.1;
}
.runtime{
}
.description{
}
.director{
}
Maybe other stuff need to be added beside flexbox I'm not sure, that's why I ask here. Any solution will be helpful!
If you change your HTML structure slightly you can accomplish this fairly easily:
<div class="movie-container">
<div class="upper-container">
<div class="image">Image</div>
<div class="side-container">
<div class="title">Title</div>
<div class="more">More</div>
<div class="runtime">Runtime</div>
<div class="description">Description</div>
</div>
</div>
<div class="lower-container">
<div class="director">Director</div>
</div>
</div>
JSFiddle
Flex isn't very good at stretching across multiple rows / columns like tables or Grid is, while you state you don't want that solution it is typically a better option in cases like this.
I find it easiest to work with flexbox on a row-by-row basis instead of using wrapping (although you can certainly do that too).
As a starting point, I think this snippet is what you're going for?
div {
flex: 1 1 auto;
}
.box {
border: 1px solid black;
}
.flex {
display: flex;
}
.image {
width: 120px;
flex: 0 0 auto;
}
.more {
flex: 0 0 auto;
}
<div class="flex upper">
<div class="box flex image">Image</div>
<div class="upper-detail">
<div class="flex title-container">
<div class="box title">Title</div>
<div class="box more">More</div>
</div>
<div class="box runetime">Runtime</div>
<div class="box director">Director</div>
</div>
</div>
<div class="box description">Description</div>
<div class="box other">Other stuff...</div>
Hope this helps.
.upper-container{
display: flex;
height: 200px;
}
.upper-left{
background: #ddd;
flex: 1;
display: flex;
align-items: center;
justify-content: center;
}
.upper-right{
flex: 3;
display: flex;
flex-direction: column;
height: 100%;
}
.title-more, .runtime, .director{
flex: 1;
border: 1px solid #222;
display: flex;
align-items: center;
}
.lower-container{
border: 1px solid #222;
padding: 10px;
}
.title-more{
justify-content: space-between;
}
.more-button{
height: 30px;
width: 100px;
border: 1px solid #333;
margin-right: 5px;
display: flex;
align-items: center;
justify-content: center;
}
<div class="movie-container">
<div class="upper-container">
<div class="upper-left">
Image
</div>
<div class="upper-right">
<div class="title-more">
<div class="title-container">
Title
</div>
<div class="more-button">
More
</div>
</div>
<div class="runtime">Runtime</div>
<div class="director">Director</div>
</div>
</div>
<div class="lower-container">
Description
</div>
</div>
The key is to add some divs and remove some others:
.movie-container *{padding:.5em;}
.upper-container {
display: flex;
padding:0;
}
.image {
border: 1px solid;
flex: 1 1 25%;
}
.tmrd{flex: 1 1 75%;padding:0}
.title-more {
display: flex;
padding:0;
}
.title{flex: 1 1 75%;border: 1px solid;}
.more{flex: 1 1 25%;border: 1px solid;}
.runtime,.description,.director{border: 1px solid;}
<div class="movie-container">
<div class="upper-container">
<div class="image">Image</div>
<div class="tmrd">
<div class="title-more">
<div class="title">Title</div>
<div class="more">More</div>
</div>
<div class="runtime">Runtime</div>
<div class="description">Description</div>
</div>
</div>
<div class="director">Director</div>
</div>

Aligning alternating items to different places in a container with CSS positioning via flexbox or grid?

Is it possible to layout the following markup to be like the linked screenshot? Of course it would be easy to rearrange the HTML, but how might I start to approach it with only CSS?
<div class="container">
<div class="foo"></div>
<div class="bar"></div>
<div class="foo"></div>
<div class="bar"></div>
<div class="foo"></div>
<div class="bar"></div>
</div>
This CSS doesn't quite get there, but it's close (sort of).
.container{
width: 60%;
margin: 0 auto;
background-color: #ddd;
display: flex;
flex-flow: column nowrap;
}
.foo, .bar {
border: 1px solid black;
}
.foo{
width: 100px;
height: 40px;
background-color: #555;
align-self: flex-start;
}
.bar{
width: 450px;
height: 100px;
background-color: gray;
align-self: flex-end;
order: 2;
}
https://jsfiddle.net/joeashworth/h90nc2qL/3/
I know you mentioned not wanting to change the HTML but this is how I would go about creating this layout.
.container{
width: 60%;
margin: 0 auto;
background-color: #ddd;
display: flex;
flex-flow: column nowrap;
justify-content: space-between;
}
.boxed {
display: flex;
justify-content: space-around;
}
.foo, .bar {
border: 1px solid black;
}
.foo{
width: 100px;
height: 40px;
background-color: #555;
align-self: flex-start;
}
.foo-2 {
margin-top: -60px;
}
.foo-3 {
margin-top: -120px;
}
.bar{
width: 450px;
height: 100px;
background-color: gray;
align-self: flex-end;
order: 2;
margin-bottom: 10px;
}
<div class="container">
<div class="boxed">
<div class="foo foo-1"></div>
<div class="bar"></div>
</div>
<div class="boxed">
<div class="foo foo-2"></div>
<div class="bar"></div>
</div>
<div class="boxed">
<div class="foo foo-3"></div>
<div class="bar"></div>
</div>
</div>

Can you make a nested column flexbox as small but as wide as possible

In the code snippet below, I have two flexbox containers: A primary one, wrapped row, and a second one, nested in the first one, that has a column direction.
I'd like to have the red, green & blue containers displayed like this:
--------------
red | black
--------------
green | gray
--------------
blue |
--------------
instead of simply stacked like this:
--------
red
--------
green
--------
blue
--------
black
--------
gray
--------
Is this a possible behaviour? Thanks!
.flexbox {
display: flex;
flex-wrap: wrap;
}
.flexbox-element {
flex: 1 1 auto;
}
.flexbox-column {
flex-direction: column;
}
.red {
background-color: red;
}
.blue {
background-color: blue;
}
.green {
background-color: green;
}
.yellow {
background-color: yellow;
}
.orange {
background-color: orange;
}
.violet {
background-color: violet;
}
.black {
background-color: black;
}
.gray {
background-color: gray;
}
.flexbox-element {
width: 200px;
min-height: 200px;
}
<div class="flexbox" style="flex: 1; width: 500px">
<div class="flexbox flexbox-element flexbox-column" style="flex: 1 1 100%">
<div class="flexbox-element red"></div>
<div class="flexbox-element green"></div>
<div class="flexbox-element blue"></div>
<div class="flexbox-element black"></div>
<div class="flexbox-element gray"></div>
</div>
<div class="flexbox-element yellow"></div>
<div class="flexbox-element orange"></div>
<div class="flexbox-element violet"></div>
</div>
If you remove the flex-direction: column and the flex: 1 to the first group, you should get what you want.
.flexbox {
display: flex;
flex-wrap: wrap;
}
.flexbox-column {
flex-direction: column;
}
.red {
background-color: red;
}
.blue {
background-color: blue;
}
.black {
background-color: black;
}
.gray {
background-color: gray;
}
.green {
background-color: green;
}
.yellow {
background-color: yellow;
}
.orange {
background-color: orange;
}
.violet {
background-color: violet;
}
.flexbox-element {
width: 200px;
min-height: 200px;
}
.flexbox-group {
width: 400px;
height: 600px;
flex-direction: column;
}
<div class="flexbox" style="flex: 1; width: 500px">
<div class="flexbox flexbox-element flexbox-group">
<div class="flexbox-element red"></div>
<div class="flexbox-element green"></div>
<div class="flexbox-element blue"></div>
<div class="flexbox-element black"></div>
<div class="flexbox-element gray"></div>
</div>
<div class="flexbox-element yellow"></div>
<div class="flexbox-element orange"></div>
<div class="flexbox-element violet"></div>
</div>
CSS3 Multicolumns solution
CSS3 Grid layout solution
Due to the limitations of Flexbox when direction is column (you need to limit the height somehow or else it won't wrap in a second column), it's a simpler job for CSS3 Multicolumns:
.col-2 {
columns: 2;
}
.flexbox {
display: flex;
flex-wrap: wrap;
}
.flexbox-element {
flex: 1 1 auto;
}
.col-2 {
columns: 2;
width: 100%;
outline: 1px dashed blue;
}
.col-2 > * {
width: 50%;
min-height: 200px;
outline: 1px dotted red;
}
.red {
background-color: red;
}
.blue {
background-color: blue;
}
.green {
background-color: green;
}
.yellow {
background-color: yellow;
}
.orange {
background-color: orange;
}
.violet {
background-color: violet;
}
.black {
background-color: black;
}
.gray {
background-color: gray;
}
.flexbox-element {
width: 200px;
min-height: 200px;
}
<div class="flexbox" style="flex: 1; width: 500px">
<div class="flexbox flexbox-element col-2" style="flex: 1 1 100%">
<div class="red">A</div>
<div class="green">B</div>
<div class="blue">C</div>
<div class="black">D</div>
<div class="gray">E</div>
</div>
<div class="flexbox-element yellow"></div>
<div class="flexbox-element orange"></div>
<div class="flexbox-element violet"></div>
</div>
You could also use CSS3 Grid layout but you'll need to position manually each grid item in IE10-11 and Edge 12-15 (current Edge 16 supports the same newer recommendation that other modern browsers)
Grid layout (compat Edge 16+ and other modern browsers. IE11 needs explicit positioning of both row and column of each grid item)
➡️ https://codepen.io/PhilippeVay/pen/GdBpJa?editors=0100
.col-2 {
display: grid;
grid-auto-flow: column;
grid-template-columns: repeat(2, minmax(0, 1fr)); /* 2 columns with strict equal width */
}
.col-2 > :nth-child(odd) {
grid-column: 1; /* even items can now only occupy the 2nd column */
}