I am a backend programmer by profession. But I have just started to learn flexbox and I want to hit the sky with flexbox.
So, I created a simplest design but which looks most complicated to me when creating it using flexbox.
Here is the design:
Guys, I am not able to figure out, how to use flexbox in such a case as there is no row or column. I don't know but is there anything like rowspan or colspan in flexbox that I can use to arrange these divs as shown in image above?
Here is my code:
HTML:
<div class="wrapper">
<div class="div-wrapper1">
<div class="inner-wrapper1">
<div class="div1"></div>
<div class="fake1"></div>
</div>
<div class="div2"></div>
</div>
<div class="div-wrapper2">
<div class="div3"></div>
<div class="inner-wrapper2">
<div class="fake2"></div>
<div class="div4"></div>
</div>
</div>
<div class="div-center"></div>
</div>
CSS:
.wrapper {
height: 200px;
width: 200px;
border: 1px solid black;
display: flex;
flex-direction: column;
}
.div-wrapper1 {
display: flex;
flex: 1;
}
.div-wrapper2 {
display: flex;
flex: 1
}
.inner-wrapper1 {
display: flex;
flex: 3;
flex-direction: column;
}
.div1 {
background-color: red;
display: flex;
flex: 3
}
.fake1 {
display: flex;
flex: 1
}
.div2 {
background-color: green;
display: flex;
flex: 2
}
.div3 {
background-color: blue;
display: flex;
flex: 2
}
.inner-wrapper2 {
display: flex;
flex: 3;
flex-direction: column;
}
.div4 {
background-color: yellow;
display: flex;
flex: 3
}
.fake2 {
display: flex;
flex: 1
}
.div-center {
background-color: black;
}
This is my output:
Here is the codepen
Maybe a solution is to simply add a negative margin to .div-wrapper1 and you will get the exact layout :
.wrapper {
height: 200px;
width: 200px;
border: 1px solid black;
display: flex;
flex-direction: column;
}
.div-wrapper1 {
display: flex;
flex: 1;
}
.div-wrapper2 {
display: flex;
flex: 1;
margin-top: -30px;
}
.div-wrapper3 {
display: flex;
flex: 1
}
.inner-wrapper1 {
display: flex;
flex: 3;
flex-direction: column;
}
.div1 {
background-color: red;
display: flex;
flex: 3
}
.fake1 {
display: flex;
flex: 1
}
.div2 {
background-color: green;
display: flex;
flex: 2
}
.div3 {
background-color: blue;
display: flex;
flex: 2
}
.inner-wrapper2 {
display: flex;
flex: 3;
flex-direction: column;
}
.div4 {
background-color: yellow;
display: flex;
flex: 3
}
.fake2 {
display: flex;
flex: 1
}
.div-center {
background-color: black;
}
<div class="wrapper">
<div class="div-wrapper1">
<div class="inner-wrapper1">
<div class="div1"></div>
<div class="fake1"></div>
</div>
<div class="div2"></div>
</div>
<div class="div-wrapper2">
<div class="div3"></div>
<div class="inner-wrapper2">
<div class="fake2"></div>
<div class="div4"></div>
</div>
</div>
<div class="div-center"></div>
</div>
And if you want here is another solution without any negative values and a content inside the white part (simply adjust height/width as you need) :
.first,
.second {
display: flex;
height: 100px;
width: 200px;
}
.first:before {
content: "";
background: red;
flex: 3;
}
.first:after {
content: "";
background: green;
flex: 2;
}
.second:before {
content: "";
background: blue;
flex: 2;
}
.second:after {
content: "";
background: yellow;
flex: 3;
}
.fake {
display: flex;
height: 20px;
width: 200px;
}
.fake a {
flex: 1;
text-align: center;
}
.fake:before {
content: "";
background: blue;
flex: 2;
}
.fake:after {
content: "";
background: green;
flex: 2;
}
<div class="first">
</div>
<div class="fake">
link
</div>
<div class="second">
</div>
Here is another solution by simply using multiple linear-gradient:
.box {
display: flex;
height: 220px;
width: 200px;
justify-content: center;
align-items: center;
background-image:linear-gradient(to right,red 66%,green 0%),
linear-gradient(to right,blue 33%,white 0%,white 66%,green 66%),
linear-gradient(to right,blue 33%,yellow 0%);
background-size:100% 100px,100% 20px,100% 100px;
background-position:top,center,bottom;
background-repeat:no-repeat;
}
<div class="box">
link
</div>
Related
I have a flex-box layout where I'm using flex-grow: 1 on multiple elements in order to distribute the layout evenly. However, when I add contents to one of the elements, it immediately expands out and ruins the even layout.
How can I make it so that the parents stay evenly distributed? Or would it be easier to just change it to width: 50% in order to fix that problem? Below is the code.
.app-container {
display: flex;
background: orange;
min-height: 100vh;
min-width: 100vw;
}
.sideBar-container {
display: flex;
background: pink;
width: 10%;
}
.info-container {
display: flex;
flex-direction: column;
flex-grow: 1;
height: 100vh;
}
.top-container {
display: flex;
background: green;
flex-grow: 1;
}
.bottom-container {
flex-grow: 1;
display: flex;
background: wheat;
}
.top-left {
display: flex;
flex-direction: column;
background: blue;
flex-grow: 1;
}
.top-right {
display: flex;
background: red;
flex-grow: 1;
}
.top-two {
display: flex;
justify-content: space-around;
align-items: center;
flex-grow: 1;
background: gold;
}
.bottom-two {
display: flex;
justify-content: space-around;
align-items: center;
flex-grow: 1;
background: cornflowerblue;
}
.number-appts {
background: aqua;
flex-basis: 35%;
height: 45%;
}
.projected-revenue {
background: aquamarine;
flex-basis: 45%;
}
.projected-costs {
background: burlywood;
}
.projected-profit {
background: darkgray;
}
.appointment-consult {
background: seagreen;
}
<div className="app-container">
<div className="sideBar-container">
</div>
<div className="info-container">
<div className="top-container">
<div className="top-left">
<div className="top-two">
<div className="number-appts">test</div>
<div className="projected-revenue">this</div>
</div>
<div className="bottom-two">
<div className="projected-costs">here</div>
<div className="projected-profit">testing</div>
</div>
</div>
<div className="top-right">
<div className="appointment-consult"></div>
</div>
</div>
<div className="bottom-container"></div>
</div>
</div>
I figured it out. Changed my flex-grow: 1 to flex:1 as creating a rule where flex-basis is now 0px rather than auto stops the css from looking inward at the content
I'm trying to replicate the main layout of Spotify, however, I have two problems, which are:
I am not able to place the two containers(Blue and Red) to be the
ideal height.
I am not able to put the Container(Yellow) to be at the bottom.
.main {
height: 100vh;
width: 100vw;
display: flex;
flex-wrap: wrap;
background-color: black;
}
.blue {
background-color: blue;
}
.red {
flex: 1;
display: flex;
background-color: red;
}
.yellow {
height: 91px;
flex: 1 0 100%;
display: flex;
flex-direction: column;
justify-content: flex-end;
background-color: yellow;
}
<div class="main">
<div class="blue">blue</div>
<div class="red">red</div>
<div class="yellow">yellow</div>
</div>
Can I accomplish this grid layout with flexbox? To have the first element take up 2 rows height and then continue after it?
Check image.
You can achive it by dividing this layout in 2 columns while the 2nd column will have a nested flexbox layout as well.
HTML Structure:
<div class="container">
<div class="col box1">1</div>
<div class="col col2">
<div class="box2">2</div>
<div class="box3">3</div>
<div class="box4">4</div>
<div class="box5">5</div>
</div>
</div>
Necessary Styles:
.container {
min-height: 100vh;
display: flex;
}
.col {
flex-grow: 1;
color: #fff;
}
.col2 {
flex-wrap: wrap;
display: flex;
}
.col2 > div {
flex-basis: 50%;
flex-grow: 1;
}
.box1 {
display: flex;
}
* {box-sizing: border-box;}
body {
margin: 0;
}
.container {
min-height: 100vh;
display: flex;
}
.col {
flex-grow: 1;
color: #fff;
}
.col2 {
flex-wrap: wrap;
display: flex;
}
.col2 > div {
flex-basis: 50%;
padding: 10px;
flex-grow: 1;
}
.box1 {
background: brown;
padding: 10px;
display: flex;
}
.box2 {
background: pink;
}
.box3 {
background: black;
}
.box4 {
background: yellow;
}
.box5 {
background: royalblue;
}
<div class="container">
<div class="col box1">1</div>
<div class="col col2">
<div class="box2">2</div>
<div class="box3">3</div>
<div class="box4">4</div>
<div class="box5">5</div>
</div>
</div>
You can use this HTML structure but you need to set fixed height on parent div. Then you just use flex-direction: column and flex-wrap: wrap.
* {
box-sizing: border-box;
}
.content {
height: 100vh;
display: flex;
flex-direction: column;
flex-wrap: wrap;
}
div div:first-child {
flex: 0 0 100%;
width: 50%;
background: #880015;
}
div div:not(:first-child) {
width: 25%;
flex: 0 0 50%;
border: 1px solid black;
}
<div class="content">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box">5</div>
</div>
This question already has an answer here:
Using flex order property to re-arrange items for desktop and mobile views
(1 answer)
Closed 6 years ago.
I've learned that display flex help me reduce working hours for publishing.
However I've got a problem that the layout didn't display what I want.
All I want to display like grid layout like below :
But unfortunately I cannot fix this issue by myself.
Could you give me an advice how can I fix this issue with flex tag?
You can see my problem like below too :
Here is my code :
.item_wrap {
display: flex;
justify-content: space-between;
flex-flow: row-wrap;
}
.item_0 {
width: 500px;
height: 500px;
background: #ff0;
}
.item_1 {
width: 490px;
height: 160px;
background: #00f;
}
.item_2 {
width: 240px;
height: 160px;
background: #ff00e4;
}
.item_3 {
width: 240px;
height: 160px;
background: #ff00e4;
}
.item_4 {
width: 240px;
height: 160px;
background: #1cc600;
}
.item_5 {
width: 240px;
height: 160px;
background: #1cc600;
}
You will need to change HTML structure.
All blocks on the right side should be wrapped in a <div>.
HTML:
<div class="item-wrap">
<div class="item_0">0</div>
<div class="inner-wrap">
<div class="item_1">1</div>
<div class="item_2">2</div>
<div class="item_3">3</div>
<div class="item_4">4</div>
<div class="item_5">5</div>
</div>
</div>
CSS:
.item-wrap {
justify-content: space-between;
display: flex;
}
.inner-wrap {
justify-content: space-between;
flex-wrap: wrap;
display: flex;
}
.item_0,
.inner-wrap {
flex-basis: calc(50% - 5px);
}
.inner-wrap > div {
flex-basis: calc(50% - 5px);
}
.inner-wrap > .item_1 {
flex-basis: 100%;
}
* {box-sizing: border-box;}
body {
margin: 0;
}
.item-wrap {
justify-content: space-between;
height: 100vh;
display: flex;
}
.inner-wrap {
justify-content: space-between;
flex-wrap: wrap;
display: flex;
}
.item_0,
.inner-wrap {
flex-basis: calc(50% - 5px);
}
.inner-wrap > div {
flex-basis: calc(50% - 5px);
padding: 10px;
}
.inner-wrap > div + div {
margin-top: 10px;
}
.inner-wrap > .item_1 {
flex-basis: 100%;
}
.item_0{background:#ff0; padding: 10px;}
.item_1{background:#00f;}
.item_2{background:#ff00e4;}
.item_3{background:#ff00e4;}
.item_4{background:#1cc600;}
.item_5{background:#1cc600;}
<div class="item-wrap">
<div class="item_0">0</div>
<div class="inner-wrap">
<div class="item_1">1</div>
<div class="item_2">2</div>
<div class="item_3">3</div>
<div class="item_4">4</div>
<div class="item_5">5</div>
</div>
</div>
Hi! Please checkout this code
HTML5, CSS3
.item_wrap {
width: 800px;
display: flex;
margin: 0 auto;
}
.item_0 {
height: 500px;
background: red;
flex: 1;
margin: 5px;
}
.item_1 {
height: 500px;
background: yellow;
flex: 1;
margin: 5px;
}
.headbar {
}
.head_column {
height: 200px;
background: green;
flex: 1;
}
.headbar2 {
display: flex;
}
.pinkone {
background: #000;
flex: 1;
height: 150px;
}
.pinktwo {
background: pink;
flex: 1;
height: 150px;
}
.headbar3 {
display: flex;
}
.grayone {
background: gray;
flex: 1;
height: 150px;
}
.graytwo {
background: blue;
flex: 1;
height: 150px;
}
<div class="item_wrap">
<div class="item_0"></div>
<div class="item_1">
<div class="headbar">
<div class="head_column"></div>
</div>
<div class="headbar2">
<div class="pinkone"></div>
<div class="pinktwo"></div>
</div>
<div class="headbar3">
<div class="grayone"></div>
<div class="graytwo"></div>
</div>
</div>
</div>
Moreover you can follow YouTube Channel- LearnWebCode
I am new to flex box. I thought of creating a structure like this
I tried to create this but the last <div> d is always coming in a different row
http://codepen.io/srajagop/pen/wzyNVL
body {
margin: 0;
}
.foo {
display: flex;
flex-wrap: wrap;
flex-direction: row;
display: -webkit-flex;
-webkit-flex-wrap: wrap;
-webkit-flex-direction: row;
height: 400px;
}
.a, .d {
flex: 0 0 50%;
background: green;
height: 200px;
}
.b,.c {
flex: 0 0 25%;
height: 400px;
background: blue;}
.c {
background: red;}
.d {
background: grey;
}
How to solve this?
I wouldn't add a margin top property like the above answer. What you really need is a nested flex container, where you have a row flex container surrounding all items, then a column flex container surrounding a and b. Here is a snippet that generally outlines the idea:
.row,
.col {
display: flex;
}
.row {
flex-direction: row;
height: 400px;
}
.col {
flex: 0 0 50%;
flex-direction: column;
}
.a,
.b {
flex: 0 0 50%;
}
.a {
background-color: green;
}
.b {
background-color: red;
}
.c,
.d {
flex: 0 0 25%;
}
.c {
background-color: blue;
}
.d {
background-color: yellow;
}
<div class="row">
<div class="col">
<div class="a"></div>
<div class="b"></div>
</div>
<div class="c"></div>
<div class="d"></div>
</div>
Used the margin-top property. Seems working.
body {
margin: 0;
}
.foo {
display: flex;
flex-wrap: wrap;
flex-direction: row;
display: -webkit-flex;
-webkit-flex-wrap: wrap;
-webkit-flex-direction: row;
height: 400px;
}
.a,
.d {
flex: 0 0 50%;
background: green;
height: 200px;
}
.b,
.c {
flex: 0 0 25%;
height: 400px;
background: blue;
}
.c {
background: red;
}
.d {
background: grey;
margin-top: -200px;
}
<div class="foo">
<div class="a"></div>
<div class="b"></div>
<div class="c"></div>
<div class="d"></div>
</div>