BOOTSTRAP 4 - Move col between under another in desktop [duplicate] - html

This question already has answers here:
Is it possible for flex items to align tightly to the items above them?
(5 answers)
Make a div span two rows in a grid
(2 answers)
Closed 5 years ago.
On large screens, I'd like to have a column on the left, and another column on the right that has 2 stacked boxes.
On small screens, these columns should stack into a single column. However, the order of the boxes should be 2,1,3.
Here is an illustration:
I've set the flex container with flex-direction: column and flex-wrap: wrap, and box 1 to flex-basis: 100%, but that doesn't make the second two items wrap to the next column.
How can this layout be achieved in flexbox?
Here is a demo of where I'm at so far:
.container {
display: flex;
flex-direction: column;
flex-wrap: wrap;
}
.cell {
background: #ccc;
border: solid 3px black;
width: 50%;
}
.cell-1 {
flex-basis: 100%;
}
#media (max-width: 500px) {
.cell {
width: 100%;
}
.cell-1 {
order: 2;
}
.cell-2 {
order: 1;
}
.cell-3 {
order: 3;
}
}
<h1>Vertical Boxes</h1>
<p>Goal: Have one box on the left, and two boxes on the right that are stacked. All without nesting, so that the order of the boxes can be changed on smaller screen sizes.</p>
<div class="container">
<div class="cell cell-1">
<h2>One</h2>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Iusto pariatur rerum, illum accusantium cupiditate ipsam, eaque quae fugit cum assumenda ad. Modi, excepturi. Assumenda, nobis, consequatur? Aliquid repellendus quis, iure. Lorem ipsum dolor sit
amet, consectetur adipisicing elit. Iusto pariatur rerum, illum accusantium cupiditate ipsam, eaque quae fugit cum assumenda ad. Modi, excepturi. Assumenda, nobis, consequatur? Aliquid repellendus quis, iure. Lorem ipsum dolor sit amet, consectetur
adipisicing elit. Iusto pariatur rerum, illum accusantium cupiditate ipsam, eaque quae fugit cum assumenda ad. Modi, excepturi. Assumenda, nobis, consequatur? Aliquid repellendus quis, iure. Lorem ipsum dolor sit amet, consectetur adipisicing elit.
Iusto pariatur rerum, illum accusantium cupiditate ipsam, eaque quae fugit cum assumenda ad. Modi, excepturi. Assumenda, nobis, consequatur? Aliquid repellendus quis, iure.
</div>
<div class="cell cell-2">
<h2>Two</h2>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Iste mollitia temporibus id sint illum doloremque pariatur nulla vel soluta, nostrum vitae, suscipit ea natus sed eaque in velit deserunt deleniti!
</div>
<div class="cell cell-3">
<h2>Three</h2>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Impedit, architecto perferendis voluptatum accusantium est ipsam fugit, laudantium fugiat nostrum consectetur earum. Asperiores, similique deleniti nobis nemo error, iste iure architecto.
</div>
</div>
http://codepen.io/dloewen/pen/qOzogG

It looks like you were almost there. Just two more steps:
Define a height for the flex container
Without a defined height some browsers may not know where to wrap. Try this:
.container {
display: flex;
flex-direction: column;
flex-wrap: wrap;
height: 500px; /* new; value just for demo purposes */
}
Turn off wrap on mobile view
#media (max-width: 500px) {
.container { flex-wrap: nowrap; } /* new */
.cell { width: 100%; }
.cell-1 { order: 2; }
.cell-2 { order: 1; }
.cell-3 { order: 3; }
}
.container {
display: flex;
flex-direction: column;
flex-wrap: wrap;
height: 500px;
}
.cell {
background: #ccc;
border: solid 3px black;
width: 50%;
}
.cell-1 {
flex-basis: 100%;
}
#media (max-width: 500px) {
.container {
flex-wrap: nowrap;
}
.cell {
width: 100%;
}
.cell-1 {
order: 2;
}
.cell-2 {
order: 1;
}
.cell-3 {
order: 3;
}
}
<h1>Vertical Boxes</h1>
<p>Goal: Have one box on the left, and two boxes on the right that are stacked. All without nesting, so that the order of the boxes can be changed on smaller screen sizes.</p>
<div class="container">
<div class="cell cell-1">
<h2>One</h2>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Iusto pariatur rerum, illum accusantium cupiditate ipsam, eaque quae fugit cum assumenda ad. Modi, excepturi. Assumenda, nobis, consequatur? Aliquid repellendus quis, iure. Lorem ipsum dolor sit
amet, consectetur adipisicing elit. Iusto pariatur rerum, illum accusantium cupiditate ipsam, eaque quae fugit cum assumenda ad. Modi, excepturi. Assumenda, nobis, consequatur? Aliquid repellendus quis, iure. Lorem ipsum dolor sit amet, consectetur
adipisicing elit. Iusto pariatur rerum, illum accusantium cupiditate ipsam, eaque quae fugit cum assumenda ad. Modi, excepturi. Assumenda, nobis, consequatur? Aliquid repellendus quis, iure. Lorem ipsum dolor sit amet, consectetur adipisicing elit.
Iusto pariatur rerum, illum accusantium cupiditate ipsam, eaque quae fugit cum assumenda ad. Modi, excepturi. Assumenda, nobis, consequatur? Aliquid repellendus quis, iure.
</div>
<div class="cell cell-2">
<h2>Two</h2>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Iste mollitia temporibus id sint illum doloremque pariatur nulla vel soluta, nostrum vitae, suscipit ea natus sed eaque in velit deserunt deleniti!
</div>
<div class="cell cell-3">
<h2>Three</h2>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Impedit, architecto perferendis voluptatum accusantium est ipsam fugit, laudantium fugiat nostrum consectetur earum. Asperiores, similique deleniti nobis nemo error, iste iure architecto.
</div>
</div>
revised codepen

First we create a container .Box and set width and height. Honestly, it is just for demo i set 80vw and 80vh. And that container should be a flex element with column orientation flex-flow: column wrap;. And we set the max-width of the largest blocks (.BoxItem--large { max-width: 80%; height: 100%; }) 100% of parents height. It is for wrapping the other elements on the new line/column. So elements from another column should fill all space, so we set .BoxItem--small { max-width: 20%; height: auto; } - yes, 80% + 20% == 100%, that the math. lets change behaviour on the low-width screens. #media screen and (max-width: 600px) { .Box { flex-flow: column nowrap; } } we dont need to wrap elements, so just change that. After that we want every BoxItem element to inherit parents width. .BoxItem { width: 100%; max-width: 100%; } max-width: 100%; for cascading, we dont want care about some old rules like .BoxItem--large { max-width: 80%; } and .BoxItem--small { max-width: 20%; }. And set some order changes.
*,
*:before,
*:after {
box-sizing: inherit;
}
html {
box-sizing: border-box;
}
html,
body {
height: 100%;
margin: 0;
padding: 0;
}
body {
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
-webkit-box-align: center;
-webkit-align-items: center;
-ms-flex-align: center;
align-items: center;
-webkit-box-pack: center;
-webkit-justify-content: center;
-ms-flex-pack: center;
justify-content: center;
background-color: #F72F4E;
overflow: hidden;
}
.Box {
width: 80vw;
height: 80vh;
background-color: rgba(0,0,0,.2);
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
-webkit-flex-flow: column wrap;
-ms-flex-flow: column wrap;
flex-flow: column wrap;
-webkit-box-align: baseline;
-webkit-align-items: baseline;
-ms-flex-align: baseline;
align-items: baseline;
}
.BoxItem {
background-color: #fff;
border: 1px solid #000;
}
.BoxItem--large {
max-width: 80%;
height: 100%;
}
.BoxItem--small {
max-width: 20%;
height: auto;
}
#media screen and (max-width: 600px) {
.Box {
-webkit-flex-flow: column nowrap;
-ms-flex-flow: column nowrap;
flex-flow: column nowrap;
}
.BoxItem {
width: 100%;
max-width: 100%;
}
.BoxItem--large {
height: auto;
}
.BoxItem--1 {
-webkit-box-ordinal-group: 3;
-webkit-order: 2;
-ms-flex-order: 2;
order: 2;
}
.BoxItem--2 {
-webkit-box-ordinal-group: 2;
-webkit-order: 1;
-ms-flex-order: 1;
order: 1;
}
.BoxItem--3 {
-webkit-box-ordinal-group: 4;
-webkit-order: 3;
-ms-flex-order: 3;
order: 3;
}
}
<div class="Box">
<div class="BoxItem BoxItem--large BoxItem--1">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Iusto omnis rerum ea assumenda velit. Dolores ea unde iste esse illo sit, repellat molestias deleniti, voluptas expedita commodi odio possimus amet?</div>
<div class="BoxItem BoxItem--small BoxItem--2">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Iusto omnis rerum ea assumenda velit.</div>
<div class="BoxItem BoxItem--small BoxItem--3">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Iusto omnis rerum ea assumenda velit.</div>
</div>

Related

HTML Flex. Center elements

I need to center the elements of the column (like this:)
My actual code:
.education_flexColumn {
flex-direction: column;
display: flex;
align-items: center;
justify-content: center;
flex: 1;
flex-grow: 1;
}
.education_flexRow {
display: flex;
flex: 1;
justify-content: center;
align-items: stretch;
flex-grow: 1;
}
.divfh {
display: flex;
align-self: center;
flex: 1;
justify-content: center;
height: 100%;
flex-grow: 1;
}
<main>
<div class="education_flexRow">
<div class="education_flexColumn">
Lorem, ipsum dolor sit amet consectetur adipisicing elit. Placeat voluptatum accusamus ipsum nulla, facere perspiciatis, quos quod, sint atque illum maiores iure. Excepturi harum quisquam iste ut esse ea non.
</div>
<div class="education_flexColumn">
<div class="divfh">Lorem ipsum dolor, sit amet consectetur adipisicing elit. Nesciunt id at dolore veniam illo ut et hic incidunt sit nostrum, laboriosam ipsa porro recusandae facere dolores. Laudantium amet placeat molestiae!</div>
<div class="divfh">Lorem ipsum dolor sit, amet consectetur adipisicing elit. Minus, voluptatem reprehenderit explicabo magnam totam animi neque aliquam? Iure, minima aut ducimus placeat aliquam mollitia ad tempora rerum ex commodi sunt!</div>
</div>
</div>
</main>
On my actual result, the 2nd column content is not centered align.
You need an align-items: center; on the .divfh. The align-self does nothing since you want to align the children:
html, body, main, .education_flexRow {
height: 100%;
}
.education_flexColumn {
flex-direction: column;
display: flex;
align-items: center;
justify-content: center;
border-right: 1px solid black;
}
.education_flexColumn > .divfh:first-child {
border-bottom: 1px solid red;
}
.education_flexRow {
display: flex;
justify-content: center;
align-items: stretch;
border: 1px solid black;
}
.divfh {
display: flex;
align-items: center;
justify-content: center;
height: 100%;
}
<main>
<div class="education_flexRow">
<div class="education_flexColumn">
Lorem, ipsum dolor sit amet consectetur adipisicing elit. Placeat voluptatum accusamus ipsum nulla, facere perspiciatis, quos quod, sint atque illum maiores iure. Excepturi harum quisquam iste ut esse ea non.
</div>
<div class="education_flexColumn">
<div class="divfh">Lorem ipsum dolor, sit amet consectetur adipisicing elit. Nesciunt id at dolore veniam illo ut et hic incidunt sit nostrum, laboriosam ipsa porro recusandae facere dolores. Laudantium amet placeat molestiae!</div>
<div class="divfh">Lorem ipsum dolor sit, amet consectetur adipisicing elit. Minus, voluptatem reprehenderit explicabo magnam totam animi neque aliquam? Iure, minima aut ducimus placeat aliquam mollitia ad tempora rerum ex commodi sunt!</div>
</div>
</div>
</main>
If you don't mind the opportunity to slightly change your approach, I would suggest you to use the grid template layout.
Your current layout has problems when reducing the box contents and they don't align correctly anymore if their width didn't grow because of their content too small to fit the whole remaining horizontal space.
In this demo I just flattened the html structure so that the second column doesn't need anymore to be its own parent element and gave to the first .area item the class wide so that it will span 2 rows.
The content of the boxes will be centered both on the horizontal and vertical axes because of display: flex.
.areas {
display: grid;
grid-template-columns: 1fr 1fr;
grid-auto-rows: 4rem; /*row min height*/
grid-gap: 5px; /*distance between boxes*/
}
.wide {
grid-row: span 2;
}
.area {
outline: solid 1px lightgray;
display: flex;
align-items: center; /*content centered vertically*/
justify-content: center; /*content centered horizontally*/
}
<main>
<div class="areas">
<div class="area wide">Lorem, ipsum dolor sit ame...</div>
<div class="area">Lorem ipsum dolotiae!...</div>
<div class="area">Lorem ipsum ex commodi sunt!...</div>
</div>
</main>

CSS Layout with a container and two sidebars

I've tried to achieve this layout with CSS, however, the sidebars (in red) expand whenever there is content in them. I would like if the sidebars maintained a constant width regardless of the content inside of them.
.wrapper {
display: flex;
}
.container {
flex-grow: 1;
max-width: 1024px;
width: 100%;
}
.sidebar {
flex-grow: 1;
min-width: fit-content;
}
You can also use flex-basis CSS property to set the width of each section as the code below:
.wrapper {
display: flex;
}
.container {
flex-grow: 1;
flex-shrink: 1;
flex-basis: 70%;
border: 1px solid #000;
}
.sidebar {
flex-grow: 1;
flex-shrink: 1;
flex-basis: 15%;
border: 1px solid red;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>css flex</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="wrapper">
<section class="sidebar">side-left Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ad aliquid dicta facere fuga harum incidunt maiores molestiae necessitatibus nihil, nisi obcaecati odio odit quis quisquam rem reprehenderit totam vel voluptatibus?</section>
<section class="container">container Lorem ipsum dolor sit amet, consectetur adipisicing elit. Animi debitis deserunt dolore eius enim eum exercitationem explicabo, fuga iusto nam nesciunt nostrum obcaecati odio odit quas rem sapiente suscipit tempora tenetur velit. Accusantium aliquam dignissimos ea eos laboriosam molestiae nemo nobis optio pariatur, rerum tenetur.</section>
<section class="sidebar">side-right Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ab assumenda consectetur dignissimos eos et hic in ipsa ipsum nam officiis perferendis qui, quisquam recusandae, repellendus, repudiandae rerum tempore. Asperiores at atque consectetur cum, deleniti deserunt dolorum error exercitationem fuga harum illum ipsam nisi nostrum omnis placeat provident, quaerat, qui quos sed sequi temporibus velit voluptatum?</section>
</div>
</body>
</html>
Here is an example of a basic CSS layout with a container element and two sidebars:
HTML:
<div class="container">
<div class="sidebar-left">
<!-- sidebar content goes here -->
</div>
<div class="main-content">
<!-- main content goes here -->
</div>
<div class="sidebar-right">
<!-- sidebar content goes here -->
</div>
</div>
CSS:
.container {
display: flex;
}
.sidebar-left {
width: 20%;
background-color: #ccc;
}
.main-content {
width: 60%;
background-color: #fff;
}
.sidebar-right {
width: 20%;
background-color: #ccc;
}```
flex-basis is the way.
.wrapper {
display: flex;
}
.container {
flex-shrink: 1;
flex-grow: 1;
flex-basis: 1024px;
}
.sidebar {
flex-shrink: 1;
flex-grow: 1;
flex-basis: calc((100vw - 1024px)/2);
}
Replacing 1024px with whatever you desire the container width to be.

Flex inside flex with overflow-x

I'd like to create 2 columns inline. The first column has fixed width of 200px and second column should take up the rest of the screen. The second column should autoscroll on the x-axis. I've been trying to use flex but I'm very confused by the markup. Can someone please explain what I'm doing wrong.
Path: html
<section className="container">
<div className="container-name">1</div>
<div className="container-time">
<div className="element">
222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222999999
</div>
</div>
</section>
Path: CSS
.container {
flex: 0 0 auto;
}
.container-name {
width: 200px;
border: 1px dotted;
}
.container-time {
flex: 0 0 auto;
border: 1px solid;
}
.element {
display: flex;
overflow-x: auto;
}
You can get the desired output using:
max-width on the container
display: inline-flex; on the container
max-width on the container-time
.container {
max-width: 100%;
display: inline-flex;
}
.container-name {
width: 200px;
border: 1px solid red;
}
.container-time {
flex-grow: 1;
max-width: calc(100% - 200px);
border: 1px solid green;
}
.element {
display: flex;
overflow-x: auto;
}
<section class="container">
<div class="container-name">1</div>
<div class="container-time">
<div class="element">
222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222
</div>
</div>
</section>
Hope this solution will help you!
* {
margin: 0;
padding: 0;
outline: 0;
box-sizing: border-box;
}
.container {
position: relative;
width: 100%;
height: 100vh;
display: flex;
min-height: 100vh;
}
.container .split {
position: relative;
width: 100%;
height: 100%;
}
.container .split__left {
max-width: 400px;
background-color: coral;
}
.container .split__right {
padding: 2rem;
overflow-x: scroll;
background-color: pink;
}
.container .split__right p {
white-space: nowrap;
}
<div class="container">
<div class="split split__left"></div>
<div class="split split__right">
<p>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Nihil repellat sapiente assumenda. Ex fugit at amet sed libero eaque. Debitis obcaecati aperiam hic possimus unde. Voluptatibus cumque illo dolores architecto.</p>
<p>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Nihil repellat sapiente assumenda. Ex fugit at amet sed libero eaque. Debitis obcaecati aperiam hic possimus unde. Voluptatibus cumque illo dolores architecto.</p>
<p>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Nihil repellat sapiente assumenda. Ex fugit at amet sed libero eaque. Debitis obcaecati aperiam hic possimus unde. Voluptatibus cumque illo dolores architecto.</p>
<p>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Nihil repellat sapiente assumenda. Ex fugit at amet sed libero eaque. Debitis obcaecati aperiam hic possimus unde. Voluptatibus cumque illo dolores architecto.</p>
<p>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Nihil repellat sapiente assumenda. Ex fugit at amet sed libero eaque. Debitis obcaecati aperiam hic possimus unde. Voluptatibus cumque illo dolores architecto.</p>
</div>
</div>
[NOTE] see this in bigger screen.

wrong Flexbox size

I want to make 3 blocks with flex.
the first must occupy an entire column and the other 2 must be arranged in the second column with 50% of the height each. the first of the second column being an image, I would like the third, which contains only text, to be the same height.
unfortunately, even if this text block seems to have the same size as my image, the size of the 1st column is limited to the end of the text in this block.
.superposition {
display: flex;
width: 70%;
}
.block-orange {
background-color: #F26522;
color: white;
padding: 10px;
flex: 0 0 30%;
}
.superposition .flex-col {
display: flex;
flex-direction: column;
}
.superposition div div {
flex: 0 0 50%;
}
.bg-white {
background-color: yellow;
color: #627188;
}
.bg-grey{
background-color: grey;
}
<section class="superposition">
<div class="block-orange">
<h2>bright ass orange</h2>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Cumque sunt possimus tenetur porro aliquam, tempora itaque aperiam perspiciatis reiciendis dignissimos assumenda odit incidunt sit voluptatem quae laudantium. Accusamus, cum at?</p>
</div>
<div class="flex-col">
<div class="bg-grey">
<img src="img/header-soleil.png" alt="couché de soleil">
</div>
<div class="bg-white">
<h2>finaly a layout that blows your head off</h2>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit.Totam quod excepturi laboriosam vero numquam tenetur corporis iusto magni quaerat eaque dolore, assumenda unde est nostrum saepe fugiat nam doloremque esse.
</p>
</div>
</div>
</section>
why the first column (block-orange) does not adapt in height to the second column?
It seems that you want a grid in which:
Content can expand column height.
Column heights always match.
Two rows are each 50% column height.
This sounds like a "two-dimensional" layout, controlled by both row and column.
Building such layouts with flexbox will likely be a struggle and/or produce fragile layouts.
For reference, see Relationship of grid layout to other layout methods:
do I only need to control the layout by row or column – use a flexbox
do I need to control the layout by row and column – use a grid
Also see Equal height rows in CSS Grid Layout.
I recommend a grid layout instead.
Here's a demonstration:
body {
margin: 0;
}
.superposition {
display: grid;
grid-template-columns: 30% 40%;
grid-auto-rows: 1fr;
}
.block-orange {
background-color: orange;
grid-row: 1/3;
}
.bg-grey {
background-color: grey;
}
.bg-white {
background-color: yellow;
}
.bg-grey img {
width: 100%;
height: 100%;
object-fit: cover;
}
.cellpad {
box-sizing: border-box;
padding: 1em;
}
<section class="superposition">
<div class="block-orange cellpad">
<h2>bright orange</h2>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Cumque sunt possimus tenetur porro aliquam, tempora itaque aperiam perspiciatis reiciendis dignissimos assumenda odit incidunt sit voluptatem quae laudantium. Accusamus, cum at?</p>
</div>
<div class="bg-grey">
<img src="https://fakeimg.pl/440x320/282828/eae0d0/" alt="">
</div>
<div class="bg-white cellpad">
<h2>finally a layout that blows your mind</h2>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit.Totam quod excepturi laboriosam vero numquam tenetur corporis iusto magni quaerat eaque dolore, assumenda unde est nostrum saepe fugiat nam doloremque esse.
</p>
</div>
</section>
To achieve the result you are looking for, one approach would be to apply
flex-direction: column;
to the entire .superposition parent <div>, in combination with:
flex-wrap: wrap;
which will ensure that if .block-orange occupies 100% of the height of .superposition, then .bg-grey will follow it by starting at the top of .superposition, to the right of .block-orange.
i.e. The divs are still wrapping but they are wrapping horizontally, rather than wrapping vertically.
Working Example:
.superposition {
display: flex;
flex-direction: column;
flex-wrap: wrap;
width: 70%;
height: 500px;
}
.block-orange {
flex: 1 0 100%;
width: 30%;
padding: 0 10px;
color: white;
background-color: #F26522;
}
.bg-grey,
.bg-white {
flex: 0 0 50%;
width: 70%;
padding: 0 10px;
}
.bg-white {
color: #627188;
background-color: yellow;
}
.bg-grey {
background-color: grey;
}
<section class="superposition">
<div class="block-orange">
<h2>bright ass orange</h2>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Cumque sunt possimus tenetur porro aliquam, tempora itaque aperiam perspiciatis reiciendis dignissimos assumenda odit incidunt sit voluptatem quae laudantium. Accusamus, cum at?</p>
</div>
<div class="bg-grey">
<img src="img/header-soleil.png" alt="couché de soleil">
</div>
<div class="bg-white">
<h2>Finally, a layout that blows your head off</h2>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit.Totam quod excepturi laboriosam vero numquam tenetur corporis iusto magni quaerat eaque dolore, assumenda unde est nostrum saepe fugiat nam doloremque esse.</p>
</div>
</section>

Flexbox rearrange vertically and horizontally [duplicate]

This question already has answers here:
Is it possible for flex items to align tightly to the items above them?
(5 answers)
Make a div span two rows in a grid
(2 answers)
Closed 5 years ago.
On large screens, I'd like to have a column on the left, and another column on the right that has 2 stacked boxes.
On small screens, these columns should stack into a single column. However, the order of the boxes should be 2,1,3.
Here is an illustration:
I've set the flex container with flex-direction: column and flex-wrap: wrap, and box 1 to flex-basis: 100%, but that doesn't make the second two items wrap to the next column.
How can this layout be achieved in flexbox?
Here is a demo of where I'm at so far:
.container {
display: flex;
flex-direction: column;
flex-wrap: wrap;
}
.cell {
background: #ccc;
border: solid 3px black;
width: 50%;
}
.cell-1 {
flex-basis: 100%;
}
#media (max-width: 500px) {
.cell {
width: 100%;
}
.cell-1 {
order: 2;
}
.cell-2 {
order: 1;
}
.cell-3 {
order: 3;
}
}
<h1>Vertical Boxes</h1>
<p>Goal: Have one box on the left, and two boxes on the right that are stacked. All without nesting, so that the order of the boxes can be changed on smaller screen sizes.</p>
<div class="container">
<div class="cell cell-1">
<h2>One</h2>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Iusto pariatur rerum, illum accusantium cupiditate ipsam, eaque quae fugit cum assumenda ad. Modi, excepturi. Assumenda, nobis, consequatur? Aliquid repellendus quis, iure. Lorem ipsum dolor sit
amet, consectetur adipisicing elit. Iusto pariatur rerum, illum accusantium cupiditate ipsam, eaque quae fugit cum assumenda ad. Modi, excepturi. Assumenda, nobis, consequatur? Aliquid repellendus quis, iure. Lorem ipsum dolor sit amet, consectetur
adipisicing elit. Iusto pariatur rerum, illum accusantium cupiditate ipsam, eaque quae fugit cum assumenda ad. Modi, excepturi. Assumenda, nobis, consequatur? Aliquid repellendus quis, iure. Lorem ipsum dolor sit amet, consectetur adipisicing elit.
Iusto pariatur rerum, illum accusantium cupiditate ipsam, eaque quae fugit cum assumenda ad. Modi, excepturi. Assumenda, nobis, consequatur? Aliquid repellendus quis, iure.
</div>
<div class="cell cell-2">
<h2>Two</h2>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Iste mollitia temporibus id sint illum doloremque pariatur nulla vel soluta, nostrum vitae, suscipit ea natus sed eaque in velit deserunt deleniti!
</div>
<div class="cell cell-3">
<h2>Three</h2>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Impedit, architecto perferendis voluptatum accusantium est ipsam fugit, laudantium fugiat nostrum consectetur earum. Asperiores, similique deleniti nobis nemo error, iste iure architecto.
</div>
</div>
http://codepen.io/dloewen/pen/qOzogG
It looks like you were almost there. Just two more steps:
Define a height for the flex container
Without a defined height some browsers may not know where to wrap. Try this:
.container {
display: flex;
flex-direction: column;
flex-wrap: wrap;
height: 500px; /* new; value just for demo purposes */
}
Turn off wrap on mobile view
#media (max-width: 500px) {
.container { flex-wrap: nowrap; } /* new */
.cell { width: 100%; }
.cell-1 { order: 2; }
.cell-2 { order: 1; }
.cell-3 { order: 3; }
}
.container {
display: flex;
flex-direction: column;
flex-wrap: wrap;
height: 500px;
}
.cell {
background: #ccc;
border: solid 3px black;
width: 50%;
}
.cell-1 {
flex-basis: 100%;
}
#media (max-width: 500px) {
.container {
flex-wrap: nowrap;
}
.cell {
width: 100%;
}
.cell-1 {
order: 2;
}
.cell-2 {
order: 1;
}
.cell-3 {
order: 3;
}
}
<h1>Vertical Boxes</h1>
<p>Goal: Have one box on the left, and two boxes on the right that are stacked. All without nesting, so that the order of the boxes can be changed on smaller screen sizes.</p>
<div class="container">
<div class="cell cell-1">
<h2>One</h2>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Iusto pariatur rerum, illum accusantium cupiditate ipsam, eaque quae fugit cum assumenda ad. Modi, excepturi. Assumenda, nobis, consequatur? Aliquid repellendus quis, iure. Lorem ipsum dolor sit
amet, consectetur adipisicing elit. Iusto pariatur rerum, illum accusantium cupiditate ipsam, eaque quae fugit cum assumenda ad. Modi, excepturi. Assumenda, nobis, consequatur? Aliquid repellendus quis, iure. Lorem ipsum dolor sit amet, consectetur
adipisicing elit. Iusto pariatur rerum, illum accusantium cupiditate ipsam, eaque quae fugit cum assumenda ad. Modi, excepturi. Assumenda, nobis, consequatur? Aliquid repellendus quis, iure. Lorem ipsum dolor sit amet, consectetur adipisicing elit.
Iusto pariatur rerum, illum accusantium cupiditate ipsam, eaque quae fugit cum assumenda ad. Modi, excepturi. Assumenda, nobis, consequatur? Aliquid repellendus quis, iure.
</div>
<div class="cell cell-2">
<h2>Two</h2>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Iste mollitia temporibus id sint illum doloremque pariatur nulla vel soluta, nostrum vitae, suscipit ea natus sed eaque in velit deserunt deleniti!
</div>
<div class="cell cell-3">
<h2>Three</h2>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Impedit, architecto perferendis voluptatum accusantium est ipsam fugit, laudantium fugiat nostrum consectetur earum. Asperiores, similique deleniti nobis nemo error, iste iure architecto.
</div>
</div>
revised codepen
First we create a container .Box and set width and height. Honestly, it is just for demo i set 80vw and 80vh. And that container should be a flex element with column orientation flex-flow: column wrap;. And we set the max-width of the largest blocks (.BoxItem--large { max-width: 80%; height: 100%; }) 100% of parents height. It is for wrapping the other elements on the new line/column. So elements from another column should fill all space, so we set .BoxItem--small { max-width: 20%; height: auto; } - yes, 80% + 20% == 100%, that the math. lets change behaviour on the low-width screens. #media screen and (max-width: 600px) { .Box { flex-flow: column nowrap; } } we dont need to wrap elements, so just change that. After that we want every BoxItem element to inherit parents width. .BoxItem { width: 100%; max-width: 100%; } max-width: 100%; for cascading, we dont want care about some old rules like .BoxItem--large { max-width: 80%; } and .BoxItem--small { max-width: 20%; }. And set some order changes.
*,
*:before,
*:after {
box-sizing: inherit;
}
html {
box-sizing: border-box;
}
html,
body {
height: 100%;
margin: 0;
padding: 0;
}
body {
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
-webkit-box-align: center;
-webkit-align-items: center;
-ms-flex-align: center;
align-items: center;
-webkit-box-pack: center;
-webkit-justify-content: center;
-ms-flex-pack: center;
justify-content: center;
background-color: #F72F4E;
overflow: hidden;
}
.Box {
width: 80vw;
height: 80vh;
background-color: rgba(0,0,0,.2);
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
-webkit-flex-flow: column wrap;
-ms-flex-flow: column wrap;
flex-flow: column wrap;
-webkit-box-align: baseline;
-webkit-align-items: baseline;
-ms-flex-align: baseline;
align-items: baseline;
}
.BoxItem {
background-color: #fff;
border: 1px solid #000;
}
.BoxItem--large {
max-width: 80%;
height: 100%;
}
.BoxItem--small {
max-width: 20%;
height: auto;
}
#media screen and (max-width: 600px) {
.Box {
-webkit-flex-flow: column nowrap;
-ms-flex-flow: column nowrap;
flex-flow: column nowrap;
}
.BoxItem {
width: 100%;
max-width: 100%;
}
.BoxItem--large {
height: auto;
}
.BoxItem--1 {
-webkit-box-ordinal-group: 3;
-webkit-order: 2;
-ms-flex-order: 2;
order: 2;
}
.BoxItem--2 {
-webkit-box-ordinal-group: 2;
-webkit-order: 1;
-ms-flex-order: 1;
order: 1;
}
.BoxItem--3 {
-webkit-box-ordinal-group: 4;
-webkit-order: 3;
-ms-flex-order: 3;
order: 3;
}
}
<div class="Box">
<div class="BoxItem BoxItem--large BoxItem--1">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Iusto omnis rerum ea assumenda velit. Dolores ea unde iste esse illo sit, repellat molestias deleniti, voluptas expedita commodi odio possimus amet?</div>
<div class="BoxItem BoxItem--small BoxItem--2">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Iusto omnis rerum ea assumenda velit.</div>
<div class="BoxItem BoxItem--small BoxItem--3">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Iusto omnis rerum ea assumenda velit.</div>
</div>