Flexbox CSS Arrangment of Divs - html

Trying to arrange divs using Flexbox and not sure what I want to do is even possible.
Desired look at 769 pixels is to have column F expand down the entire height of the container on the left, and G and H stack on top of each other to the right (run the code snippet to see).
Once the screen resolution gets below 768 the look I have currently is the desired result for that view (F and G on the same row and H below spanning the width of both).
body {
background: #f5f5f5;
}
.wrap {
max-width: 1100px;
margin: 0 auto;
padding: 40px;
}
.title {
font-family: 'Montserrat';
font-size: 24px;
line-height: 30px;
display: inline-block;
vertical-align: middle;
}
.card {
margin: 20px 0;
box-shadow: 0px 5px 10px 0px rgba(0, 0, 0, 0.2);
background-color: #fff;
}
.container.show-border, .element.show-border {
border: 1px red solid;
padding: 9px;
}
.content {
color: #fff;
background-color: #cccccc;
padding: 20px;
font-family: 'Montserrat';
font-size: calc(1vw + 1em);
}
#media (max-width: 375px) {
.content {
padding: 5px;
font-size: 0;
text-indent: -9999999em;
}
}
.container, .element {
padding: 10px;
}
.flex {
display: flex;
box-sizing: border-box;
}
.row {
flex-flow: row wrap;
}
.col {
flex-flow: column wrap;
}
.element {
min-width: 100px;
}
.content {
flex: 1 100%;
}
.structure {
min-height: 480px;
}
.structure-2 {
flex: 1 44.4444444444%;
}
.f {
flex: 1 25%;
min-height: 480px;
}
.g {
flex: 1 75%;
}
.h {
flex: 1 75%;
}
#media (max-width : 768px ){
.structure-2, .f, .g {
flex: 1 50%;
min-height: 480px;
}
.h {
flex: 1 100%;
min-height: 480px;
}
}
<p>This table is a visual representation of what I am trying to accomplish at 769 px or higher. The view at 768 pixels or lower is as I want.</p>
<table width="200" border="1">
<tbody>
<tr>
<td rowspan="2">F</td>
<td>G</td>
</tr>
<tr>
<td>H</td>
</tr>
</tbody>
</table>
<div class="wrap">
<div class="container card">
<div class="flex row structure">
<div class="flex row structure-2">
<div class="flex element f">
<div class="content"> F </div>
</div>
<div class="flex element g">
<div class="content"> G </div>
</div>
<div class="flex element h">
<div class="content"> H </div>
</div>
</div>
</div>
</div>
</div>

You could change the flex-direction after 769px;. Something like this:
#media (min-width: 769px ){
.structure-2{
flex-direction: column;
flex-wrap: wrap;
}
.element{
flex: 1 1 auto;
width:50%;
}
.f{
height:100%;
}
}
P.S. I changed also your #media (max-width : 1608px ) to #media (max-width : 768px ) to remove some conflicts.
body {
background: #f5f5f5;
}
.wrap {
max-width: 1100px;
margin: 0 auto;
padding: 40px;
}
.title {
font-family: 'Montserrat';
font-size: 24px;
line-height: 30px;
display: inline-block;
vertical-align: middle;
}
.card {
margin: 20px 0;
box-shadow: 0px 5px 10px 0px rgba(0, 0, 0, 0.2);
background-color: #fff;
}
.container.show-border, .element.show-border {
border: 1px red solid;
padding: 9px;
}
.content {
color: #fff;
background-color: #cccccc;
padding: 20px;
font-family: 'Montserrat';
font-size: calc(1vw + 1em);
}
#media (max-width: 375px) {
.content {
padding: 5px;
font-size: 0;
text-indent: -9999999em;
}
}
.container, .element {
padding: 10px;
}
.flex {
display: flex;
box-sizing: border-box;
}
.row {
flex-flow: row wrap;
}
.col {
flex-flow: column wrap;
}
.element {
min-width: 100px;
}
.content {
flex: 1 100%;
}
.structure {
min-height: 480px;
}
.structure-2 {
flex: 1 44.4444444444%;
}
.f {
flex: 1 25%;
min-height: 480px;
}
.g {
flex: 1 75%;
}
.h {
flex: 1 75%;
}
#media (max-width : 768px ){
.structure-2, .f, .g {
flex: 1 50%;
min-height: 480px;
}
.h {
flex: 1 100%;
min-height: 480px;
}
}
#media (min-width: 769px ){
.structure-2{
flex-direction: column;
flex-wrap: wrap;
}
.element{
flex: 1 1 auto;
width:50%;
}
.f{
height:100%;
}
}
<div class="wrap">
<div class="container card">
<div class="flex row structure">
<div class="flex row structure-2">
<div class="flex element f">
<div class="content"> F </div>
</div>
<div class="flex element g">
<div class="content"> G </div>
</div>
<div class="flex element h">
<div class="content"> H</div>
</div>
</div>
</div>
</div>
</div>

When device width is under 769px:
- class ".f" will take the whole width - 100%
- classes ".g, .h" will take the half width - 50%
#media (max-width: 769px) {
.f {
flex: 1 100%;
}
.g,
.h {
flex:0 50%;
}
}

Related

wrap and stretch inputs on form resize

I have two inputs on form in one row. First one should be stretching on form resize, second is of fixed width. But when form is narrowed to a particular breakpont, second input should wrap to second line and stretch as well as first one.
Is it possible to achieve using CSS?
Tried using grid, but it won't wrap at all.
When using flexbox the result is better, but still have to set flex-grow for second input and it's width is not fixed, while inputs are in one row
.box {
background: grey;
padding: 20px 20px 20px 20px;
}
.wrapper {
display: grid;
grid-column-gap: 6px;
grid-row-gap: 6px;
grid-template-columns: minmax(320px, 1fr) 200px;
}
.flexWrapper {
display: flex;
flex-wrap: wrap;
}
.flex1 {
flex-basis: 320px;
flex-grow: 10;
flex-shrink: 0;
}
.flex2 {
flex-basis: 200px;
flex-shrink: 0;
flex-grow: 1;
}
<div class="wrapper">
<div class="box">One</div>
<div class="box">Two</div>
</div>
<div class="flexWrapper">
<div class="flex1 box">One</div>
<div class="flex2 box">Two</div>
</div>
https://codepen.io/C4off/pen/WNKjJaK
The easiest way to do this is to use a media query. At 600px I've reset the wrapper to be display: block with the children at 100% width which forces them to stack on top of each other. I've set the width of flex2 to 200px to fix it at that.
.box {
background: grey;
padding: 20px 20px 20px 20px;
}
.flexWrapper {
display: flex;
padding-top: 12px;
column-gap: 6px;
row-gap:6px;
}
.flex1 {
flex: 1;
}
.flex2 {
width: 200px;
}
#media only screen and (max-width: 600px) {
.flexWrapper {
display: block;
}
.flex1, .flex2 {
width: 100%;
}
}
<div class="container">
<div class="flexWrapper">
<div class="flex1 box">One</div>
<div class="flex2 box">Two</div>
</div>
</div>
Using flexbox only and the min-width proprty. Note .flex2 will overflow at container widths less than 200px
.container {
width: 60%;
outline: 1px solid red;
}
.box {
background: grey;
padding: 20px 20px 20px 20px;
}
.flex-wrapper {
display: flex;
padding-top: 12px;
column-gap: 6px;
row-gap:6px;
flex-wrap: wrap;
}
.flex1 {
flex: 2 0;
}
.flex2 {
min-width: 200px;
flex-grow: 1;
}
<div class="container">
<div class="flex-wrapper">
<div class="flex1 box">One</div>
<div class="flex2 box">Two</div>
</div>
</div>
The final way this can be done is using container queries which are quite well supported now. The max size is applied to the container and not the screen as the example below
* {
box-sizing:border-box;
}
.container {
container-type: inline-size;
container-name: my-container;
width: 60%;
}
.box {
background: grey;
padding: 20px 20px 20px 20px;
}
.flex-wrapper {
display: flex;
padding-top: 12px;
column-gap: 6px;
row-gap:6px;
}
.flex1 {
flex: 1;
}
.flex2 {
width: 200px;
}
#container my-container (max-width: 600px) {
.container {outline: 1px solid red;}
.flex-wrapper {
display: block;
}
.flex1, .flex2 {
width: 100%;
}
}
<div class="container">
<div class="flex-wrapper">
<div class="flex1 box">One</div>
<div class="flex2 box">Two</div>
</div>
</div>

how to put the last block in place of the first one in a media query

I have an example of a simple grid in which when the screen size is less than 767 pixels, all blocks stretch to fill the screen.
Section 2 moves to the very bottom when this size (767 pixels) is reached. And I have something like this: Section 1, footer, section 2. Is it possible to somehow make section 2 at the very top? (section 2, section 1, footer)
* {
box-sizing: border-box;
}
.wrapper {
max-width: 1200px;
width: 100%;
padding: 0 0px;
margin: 0 auto;
}
.leftcolumn {
float: left;
width: 65%;
}
.rightcolumn {
float: left;
width: 35%;
padding-left: 20px;
}
.row:after {
content: "";
display: table;
margin-top: 20px;
}
.footer {
padding: 20px;
text-align: center;
background: rgba(221, 221, 221, 0.3);
margin-top: 20px;
}
#media screen and (max-width: 767px) {
.leftcolumn, .rightcolumn {
width: 100%;
padding: 0;
}
}
<div class="wrapper">
<div class="row">
<div class="leftcolumn">
1 section
<div class="footer">
footer
</div>
</div>
<div class="rightcolumn">
2 section
</div>
</div>
</div>
Here is what I have now:
Here is what I want to get:
I did these changes in your code:
Use grid instead float. Then set grid-column and grid-row for grid items. Then change these for screen size smaller than 768px.
Note also I changed your HTML for this purpose:
* {
box-sizing: border-box;
}
.wrapper {
max-width: 1200px;
width: 100%;
padding: 0 0px;
margin: 0 auto;
}
.row {
display: grid;
grid-template-columns: 65% 35%;
}
.rightcolumn {
grid-column: 2 / 3;
padding-left: 20px;
}
.row:after {
content: "";
display: table;
margin-top: 20px;
}
.footer {
grid-row: 2 / 3;
padding: 20px;
text-align: center;
background: rgba(221, 221, 221, 0.3);
margin-top: 20px;
}
#media screen and (max-width: 767px) {
.row {
grid-template-columns: 100%;
}
.rightcolumn {
grid-row: 1 / 2;
grid-column: 1 / 2;
padding-left: 0;
}
.footer{
grid-row: 3 / 4;
}
}
<div class="wrapper">
<div class="row">
<div class="leftcolumn">
1 section
</div>
<div class="footer">
footer
</div>
<div class="rightcolumn">
2 section
</div>
</div>
</div>
You can simply do it by using flexbox and a little bit of change in your html code.
* {
box-sizing: border-box;
}
.wrapper {
max-width: 1200px;
width: 100%;
border: 2px solid blue;
padding: 0 0px;
margin: 0 auto;
}
.left {
width: 65%;
}
.rightcolumn {
width: 35%;
padding-left: 20px;
}
.row {
display: flex;
justify-content: space-between;
}
.footer {
padding: 20px;
width: 100%;
text-align: center;
background: rgba(221, 221, 221, 0.3);
margin-top: 20px;
}
#media screen and (max-width: 767px) {
.row {
flex-direction: column;
}
.left,
.rightcolumn {
width: 100%;
padding: 0;
}
.left {
order: 2;
}
.rightcolumn {
order: 1;
}
}
<div class="wrapper">
<div class="row">
<div class="left">
<div class="leftcolumn">
1 section
</div>
<div class="footer">
footer
</div>
</div>
<div class="rightcolumn">
2 section
</div>
</div>
</div>
You can set the width of .footer div according to your preference above 767 pixels, I have set it 100%;

How can i do this positioning of divs with FlexBox [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
i want to do this positioning of divs using flexbox, there are 3 divs , 2 must be in the row and the 3rd div must be in the bottom, all three divs are in the same big div
https://prnt.sc/utb29m
Is it what you wanted?
#import "https://fonts.googleapis.com/css?family=Montserrat";
body {
background: #f5f5f5;
}
.wrap {
max-width: 1100px;
margin: 0 auto;
padding: 40px;
}
.title {
font-family: 'Montserrat';
font-size: 24px;
line-height: 30px;
display: inline-block;
vertical-align: middle;
}
.btn-show {
margin-left: 2em;
}
.card {
margin: 20px 0;
box-shadow: 0px 5px 10px 0px rgba(0, 0, 0, 0.2);
background-color: #fff;
}
.container.show-border,
.element.show-border {
border: 1px red solid;
padding: 9px;
}
.content {
color: #fff;
background-color: #cccccc;
padding: 20px;
font-family: 'Montserrat';
font-size: calc(1vw + 1em);
}
#media (max-width: 375px) {
.content {
padding: 5px;
font-size: 0;
text-indent: -9999999em;
}
}
.container,
.element {
padding: 10px;
}
.flex {
display: flex;
box-sizing: border-box;
}
.row {
flex-flow: row wrap;
}
.col {
flex-flow: column wrap;
}
.element {
min-width: 100px;
}
.content {
flex: 1 100%;
}
.structure {
min-height: 480px;
}
.structure-1 {
flex: 1 55.5555555556%;
}
.a {
flex: 1 50%;
}
.b {
flex: 1 50%;
}
.structure-1-2-1 {
flex: 1 20%;
}
.c,
.d {
flex: 1 50%;
}
.e {
flex: 1 100%;
}
.structure-2 {
flex: 1 44.4444444444%;
}
.f {
flex: 1 50%;
}
.structure-2-2 {
flex: 1 50%;
}
.g {
flex: 1 75%;
}
.h {
flex: 1 25%;
}
#media (max-width: 768px) {
.structure-1,
.structure-2,
.f,
.structure-2-2 {
flex: 1 100%;
min-height: 480px;
}
}
<div class="wrap">
<h2 class="title">
Complex layout with flex box
</h2>
<div class="container card">
<div class="flex row structure">
<div class="flex row structure-1">
<div class="flex element a">
<div class="content">
A
</div>
</div>
<div class="flex element b">
<div class="content">
B
</div>
</div>
<div class="flex element e">
<div class="content">
E
</div>
</div>
</div>
</div>
</div>
</div>

How to center divs at every breakpoint with flexbox?

I want to center the divs how I currently have them, but the issue is that when there are two divs they are not aligning with the rectangle above. I know that they would align with justify-content:space between, but that would prevent a div from being centered when it is the only one in the row. Is it possible what I want to do?
.wrapper {
max-width: 1000px;
margin: 0 auto
}
.title {
width: 100%;
height: 100px;
background: green;
margin-bottom: 30px;
}
.outer {
width: 100%;
display: flex;
flex-wrap: wrap;
justify-content: center;
}
.col {
/* flex-grow, flex-shrink, flex-basis*/
flex: 0 0 31%;
margin: 0 1% 2% 1%;
height: 300px;
}
.col1 {
background-color: red;
}
.col2 {
background-color: blue
}
.col3 {
background: black;
}
#media only screen and (max-width: 960px) {
.col {
flex: 0 0 48%;
}
}
#media only screen and (max-width: 480px) {
.col {
flex: 0 0 100%;
}
}
<div class="wrapper">
<div class="title">
</div>
<div class="outer">
<div class="col col1">
</div>
<div class="col col2">
</div>
<div class="col col3">
</div>
</div>
</div>
You can set a margin-left: auto to the .col1 and margin-right: auto to the .col2to push the divs respectively.
.wrapper {
max-width: 1000px;
margin: 0 auto
}
.title {
width: 100%;
height: 100px;
background: green;
margin-bottom: 30px;
}
.outer {
width: 100%;
display: flex;
flex-wrap: wrap;
justify-content: center;
}
.col {
/* flex-grow, flex-shrink, flex-basis*/
flex: 0 0 31%;
margin: 1% 0 2% 0;
height: 300px;
}
.col1 {
background-color: red;
margin-right: auto;
}
.col2 {
background-color: blue;
margin-left: auto;
}
.col3 {
background: black;
}
#media only screen and (max-width: 960px) {
.col {
flex: 0 0 48%;
}
}
#media only screen and (max-width: 480px) {
.col {
flex: 0 0 100%;
}
}
<div class="wrapper">
<div class="title">
</div>
<div class="outer">
<div class="col col1">
</div>
<div class="col col2">
</div>
<div class="col col3">
</div>
</div>
</div>

How to divide a webpage into four parts using flexbox?

How to divide a webpage into four equal parts using flex box? Something like this website- www.rileyjshaw.com
The columns should be able to stack upon one another on smaller view ports.
Edit/ Update:
So far I've tried this-
Should I change the line height? How can I achieve distinct blocks?
/* Grid */
.column {
flex-basis: 100%;
width: 50%;
height: 50%;
line-height: 200px;
}
#media screen and (min-width: 800px) {
.row {
display: flex;
flex-direction: row;
flex-wrap: nowrap;
}
.column {
flex: 1;
}
._25 {
flex: 2.5;
}
._5 {
flex: 5;
}
}
/* Style */
body {
font-family: 'Lato', sans-serif;
font-size: 1.3em;
color: #ccc;
background: #000;
/*margin-bottom: 70px;*/
}
.column {
/* padding: 15px;
border: 1px solid #666;
margin: 5px 0;*/
background: #343436;
}
main {
max-width: 1200px;
margin: 0 auto;
}
h1,
h2 {
text-align: center;
}
<div class="row">
<div class="column">
50%
</div>
<div class="column">
50%
</div>
</div>
<div class="row">
<div class="column">
50%
</div>
<div class="column">
50%
</div>
</div>
You can simplify the code and gain the wanted output. Here I removed the row's and used a container. The main benefit with this structure is that you can alter the order of the column's if you find that necessary.
I also choose to use flex-basis instead of flex-grow to make them stay 50% wide no matter their content size.
On wider screens, using the media query, set the column's 50% wide and the container to display: flex; flex-wrap: wrap;.
On narrower screens, and being block elements, they stack on top of each other by default
html, body {
height: 100%;
margin: 0;
}
.container {
height: 100%;
}
.column {
height: 25%;
}
#media screen and (min-width: 600px) {
.container {
display: flex;
flex-wrap: wrap;
}
.column {
flex-basis: 50%;
height: 50%;
}
}
/* general styles */
body {
font-family: 'Lato', sans-serif;
font-size: 1.3em;
color: #ccc;
background: #000;
/*margin-bottom: 70px;*/
}
.column {
padding: 15px;
/*border: 1px solid #666;*/
box-sizing: border-box;
}
.column:nth-child(1) {
background: #5c9;
}
.column:nth-child(2) {
background: #fb0;
}
.column:nth-child(3) {
background: #39f;
}
.column:nth-child(4) {
background: #f33;
}
main {
max-width: 1200px;
margin: 0 auto;
}
h1,
h2 {
text-align: center;
}
<div class="container">
<div class="column">
50%
</div>
<div class="column">
50%
</div>
<div class="column">
50%
</div>
<div class="column">
50%
</div>
</div>
If you still need the original markup structure, here is a sample with that too
html, body {
height: 100%;
margin: 0;
}
.row {
height: 50%;
}
.column {
height: 50%;
}
#media screen and (min-width: 600px) {
.row {
display: flex;
}
.column {
flex-basis: 50%;
height: 100%;
}
}
/* general styles */
body {
font-family: 'Lato', sans-serif;
font-size: 1.3em;
color: #ccc;
background: #000;
/*margin-bottom: 70px;*/
}
.column {
padding: 15px;
/*border: 1px solid #666;*/
box-sizing: border-box;
}
.row:nth-child(1) .column:nth-child(1) {
background: #5c9;
}
.row:nth-child(1) .column:nth-child(2) {
background: #fb0;
}
.row:nth-child(2) .column:nth-child(1) {
background: #39f;
}
.row:nth-child(2) .column:nth-child(2) {
background: #f33;
}
main {
max-width: 1200px;
margin: 0 auto;
}
h1,
h2 {
text-align: center;
}
<div class="row">
<div class="column">
50%
</div>
<div class="column">
50%
</div>
</div>
<div class="row">
<div class="column">
50%
</div>
<div class="column">
50%
</div>
</div>
Updated based on a comment
Centering the column's content can be done with i.e.:
Flexbox - https://jsfiddle.net/0ns6ofcp/
.column {
height: 25%;
display: flex; /* added */
justify-content: center; /* hor. center */
align-items: center; /* ver. center */
}
Transform - https://jsfiddle.net/0ns6ofcp/1/
<div class="column">
<div>50%</div>
</div>
.column {
position: relative; /* added property */
height: 25%;
}
.column > div { /* added rule */
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%,-50%);
}
Here is something you can start with:
/* Grid */
.column {
flex-basis: 100%;
height: 50vh;
}
.row {
display: flex;
}
.row:first-child .column:first-child {
background: red;
}
.row:first-child .column:last-child {
background: blue;
}
.row:last-child .column:first-child {
background: green;
}
.row:last-child .column:last-child {
background: black;
}
/* Style */
body {
font-family: 'Lato', sans-serif;
font-size: 1.3em;
color: #ccc;
background: #000;
}
.column {
background: #343436;
}
<div>
<div class="row">
<div class="column">
50%
</div>
<div class="column">
50%
</div>
</div>
<div class="row">
<div class="column">
50%
</div>
<div class="column">
50%
</div>
</div>
</div>