This question already has answers here:
Make a div span two rows in a grid
(2 answers)
Closed 6 months ago.
I am trying to do exactly this:
Where my mark up is:
<div>
<div>A</div>
<div>B</div>
<div>C</div>
</div>
Is this possible with grid, bootstrap or flex? Without changing the order of elements?
I think it has a lot of way to do.
And one of that way is using grid. then use grid-template for solve this question
.container {
display: grid;
grid-template:
"a b"
"c b";
}
.a {
grid-area: a;
background: red;
}
.b {
grid-area: b;
background: blue;
}
.c {
grid-area: c;
background: green;
}
or using grid-area
.container {
display: grid;
}
.a {
background: red;
}
.b {
grid-area: 1 / 2 / 3;
background: blue;
}
.c {
background: green;
}
.wrapper {
height: 300px;
display: grid;
grid-template-columns: repeat(2, 50%);
}
.wrapper div {
color: white;
display: flex;
align-items: center;
justify-content: center;
}
.wrapper div:nth-child(1) {
background-color: green;
}
.wrapper div:nth-child(2) {
grid-row: 2 span;
background-color: blue;
}
.wrapper div:nth-child(3) {
background-color: red;
}
<div class="wrapper">
<div>A</div>
<div>B</div>
<div>C</div>
</div>
Is that something you wanted to achive ? "Grid" is kind of made for this kind of situations.
As for bootstrap - last version uses "flex" as default.
Using one more div will be the solution, do the following.
Hope it is useful.
https://codepen.io/Olmedo12/pen/WNdGOMB
HTML
<div class="container">
<div class="subcontainer">
<div class="box blue">BOX 1</div>
<div class="box red">BOX 3</div>
</div>
<div class="box green">
Box 2
</div>
<div>
CSS
.container {
display: flex;
}
.box {
width: 60px;
height: auto;
}
.red {
background: red;
}
.blue {
background: blue;
}
.green {
background: green;
}
Related
I have created flexbox to rearrange the boxes. Here is the demo code
The problem is that when I resize for smaller screen, I want to insert B between A and C.
How can I achieve that ? Thanks in advance.
grid is the grid-system you may need for this kind of layout.
here is a short example
/* let make a grid of 2 columns */
.main {
display: grid;
grid-template-columns: 1fr 1fr;
margin: 20px;
}
/* b should span through 2 rows */
.b {
grid-row: span 2;
}
/* clear the grid system on small screen */
#media (max-width: 639px) {
.main {
display: block;
}
}
/* original styling for bg */
.a {
background-color: red;
}
.b {
background-color: green;
}
.c {
background-color: blue;
}
<div class="main">
<div class="a">A - I have some content</div>
<div class="b">B - I have some content</div>
<div class="c">C - I have some content</div>
</div>
As you can see, you may need little HTML and CSS to get this organized. Mind to set your media query in last position so it is not overridden ;)
see https://css-tricks.com/snippets/css/complete-guide-grid/ to dig further into grid
Here a solution that gather all classes a b and c in one container:
html code :
<div class="main">
<div class="container-1">
<div class="a">A - I have some content</div>
<div class="b">B - I have some content</div>
<div class="c">C - I have some content</div>
</div>
</div>
CSS code :
.a {
background-color: red;
height: 20px;
}
.b {
background-color: green;
height: 30px;
}
.c {
background-color: blue;
height: 20px;
}
.container-1 div{
width: 45%;
display: inline-block;
}
#media (max-width: 639px) {
.container-1 div {
width: 100%;
display: block;
}
}
I think it would be easier using grid instead flex:
HTML
<div class="main">
<div class="a">a</div>
<div class="b">b</div>
<div class="c">c</div>
</div>
CSS
.main {
display: grid; /* If you don't wanna add a gap between blocks, put this attribute into the media query */
gap: 1rem; /* To add a gap between blocks */
}
.a {
background: red;
}
.b {
background: green;
}
.c {
background: blue;
}
#media (min-width: 768px) {
.main {
grid-template-columns: 1fr 1fr;
grid-template-rows: 1fr 1fr;
}
.a {
grid-area: 1 / 1 / 2 / 2;
}
.b {
grid-area: 1 / 2 / 3 / 3;
}
.c {
grid-area: 2 / 1 / 3 / 2;
}
}
This question already has an answer here:
Why are CSS named grid areas not in quotes?
(1 answer)
Closed 7 months ago.
I'm trying to create a grid layout with two boxes that I want to be in one line.
the result was :
.container {
display: grid;
grid-template-areas: "1 2 2";
}
.sidebar {
background-color: aqua;
grid-area: 1;
height: 50vh;
}
.content {
background-color: black;
grid-area: 2;
height: 50vh;
}
<div class="container">
<div class="sidebar"></div>
<div class="content"></div>
</div>
why it doesn't follow the template I designated
If you want to use numbers you need to prefix them with \3 but I don't really recommend
.container {
display: grid;
grid-template-areas: "1 2 2";
}
.sidebar {
background-color: aqua;
grid-area: \31;
height: 50vh;
}
.content {
background-color: black;
grid-area: \32;
height: 50vh;
}
<div class="container">
<div class="sidebar"></div>
<div class="content"></div>
</div>
I don't believe you can use unescaped numbers as grid area names.
Just use letters instead.
.container {
display: grid;
grid-template-areas: "a b b"
}
.sidebar {
background-color: aqua;
grid-area: a;
height: 50vh;
}
.content {
background-color: black;
grid-area: b;
height: 50vh;
}
<div class="container">
<div class="sidebar">
</div>
<div class="content"></div>
</div>
I have a grid container and I want to align three divs like this, also doing them responsive (all stacked). I don't have the heights of the divs.
It would be two columns, in one two rows (two divs one below another), in another column a div centered vertically having in mind the height of the two first divs.
I can use grid or flexbox.
Thanks
Using Flexbox:
.wrapper {
width: 400px;
border: 2px solid black;
height: 300px;
display: flex;
flex-wrap: wrap;
flex-direction: column;
justify-content: center;
align-items: center;
}
.child {
height: 100px;
width: 150px;
border: 1px solid black;
margin-top: 20px;
margin-bottom: 20px;
}
<div class="wrapper">
<div class="child"></div>
<div class="child"></div>
<div class="child"></div>
</div>
and also grid :
Example from2 columns and the third element spanning through 2 rows and margin itself in the middle.
section {
display: grid;
grid-template-columns: repeat(2, minmax(270px, 1fr));/* or any value you need */
grid-gap: 2em;/* or any value you need */
padding: 2em;/* or any value you need */
counter-reset: divs; /*demo*/
width:max-content;/* or any value you need */
margin:auto;/* or any value you need */
}
div {
border: solid red;
min-height: 30vh;/* or any value you need */
width: 270px;/* or any value you need */
display: flex; /* demo*/
}
div {
margin-left: auto;
}
div:nth-child(3) {
grid-column: 2;
grid-row: 1 / span 2;
margin: auto 0;
}
/*demo*/
div:before {
counter-increment: divs;
content: counter(divs);
margin: auto;
font-size: 3em;
}
<section>
<div></div>
<div></div>
<div></div>
</section>
To play with the grid system, you can use : https://css-tricks.com/snippets/css/complete-guide-grid/ / http://gridbyexample.com/ and https://codepen.io/pen/ for the playground.
Here's a jsfiddle: https://jsfiddle.net/5csL2dqy/
.container {
display: flex;
flex-direction: row;
width: 100%;
}
.right {
display: inherit;
align-items: center;
}
.a, .b, .c {
box-sizing: border-box;
width: 150px;
margin: 20px;
border: 1px solid black;
}
<div class="container">
<div class="left">
<div class="a">
<p>
First div
</p>
</div>
<div class="b">
<p>
second div
</p>
</div>
</div>
<div class="right">
<div class="c">
<p>
Third div
</p>
</div>
</div>
</div>
You use the following inline-flex styles
#media only screen and (min-width: 600px) {
.container {
display: inline-flex;
}
.container div {
border: 1px solid red;
padding: 1em;
margin: 1em;
}
.container>div+div {
margin: auto;
}
}
#media only screen and (max-width: 600px) {
.container div:not(first-child) {
border: 1px solid red;
}
}
<div class="container">
<div>
<div>
1
</div>
<div>
2
</div>
</div>
<div>
3
</div>
</div>
This is one of only two answers with equal width/height gaps. G-Cyr's is the other:
.grid{
display: grid;
grid-template-columns: repeat(9,1fr);
grid-template-rows: repeat(7, 1fr);
height: 90vh;
width: 120vh;
}
.grid > div{
border: solid 3px orangered;
font: 26px sans-serif;
display: flex;
align-items:center;
justify-content:center;
}
.grid > div:nth-child(1){
grid-row: 1/span 3;
grid-column: 1/span 4;
}
.grid > div:nth-child(2){
grid-row: 3/span 3;
grid-column: 6/span 4;
}
.grid > div:nth-child(3){
grid-row: 5/span 3;
grid-column: 1/span 4;
}
<div class="grid">
<div>1</div>
<div>2</div>
<div>3</div>
</div>
I have the following code:
.wrapper {
display: flex;
flex-direction: column;
flex-wrap: wrap;
}
.a {
background-color: red;
width: 65%;
}
.b {
background-color: green;
width: 35%;
}
.c {
background-color: blue;
width: 65%;
height: 100px;
}
.d {
background-color: orange;
width: 35%;
}
.e {
background-color: teal;
width: 65%;
}
.f {
background-color: purple;
width: 35%;
}
<div class="wrapper container">
<div class="a">A</div>
<div class="b">B</div>
<div class="c">C</div>
<div class="d">D</div>
<div class="e">E</div>
<div class="f">F</div>
</div>
I am trying to make the F be right under D, like this:
The main reason I am doing this instead of 2 separate columns is because I want to be able to arrange the columns in mobile later by using order. Is this possible in Flexbox, or is there another way?
In order for a flex column to wrap, you need to define a fixed height on the container. Otherwise, without a breakpoint, the column has no reason to wrap. It will simply expand the container as a single column.
This problem is explained in more detail here:
Is it possible for flex items to align tightly to the items above them?
You're probably better off with a grid solution.
.wrapper {
display: grid;
grid-template-columns: 65% 35%;
grid-template-areas: " a b "
" c d "
" c f "
" c . "
" c . "
" e . " ;}
.a { grid-area: a; background-color: red; }
.b { grid-area: b; background-color: green; }
.c { grid-area: c; background-color: blue; height: 100px; }
.d { grid-area: d; background-color: orange; }
.e { grid-area: e; background-color: teal; }
.f { grid-area: f; background-color: purple; }
<div class="wrapper">
<div class="a">A</div>
<div class="b">B</div>
<div class="c">C</div>
<div class="d">D</div>
<div class="e">E</div>
<div class="f">F</div>
</div>
Browser support is now pretty strong for CSS Grid.
Also, the order property works in both Grid and Flex layouts. But you may not need order in Grid to re-arrange your layout for mobile screens. You can simply use grid properties.
Inspired from Easy-Masonry-Layout-With-Flexbox
Seems to do the job for me
<div id="masonry">
<img src="irina.jpg" alt>
<img src="daniella.jpg" alt>
<img src="karina.jpg" alt>
…
</div>
div#masonry {
display: -ms-flexbox;
-ms-flex-direction: column;
-ms-flex-wrap: wrap;
display: flex;
flex-direction: column;
flex-wrap: wrap;
height: 100vw;
font-size: 0;
}
div#masonry img {
width: 33.3%;
transition: .8s opacity;
}
I made a layout with flexbox consisting of 2 columns. When everything is the same height it works fine:
https://codepen.io/anon/pen/rJZeJm
<div class="cont">
<div class="a">
A
</div>
<div class="b">B</div>
<div class="c">C</div>
</div>
* {
box-sizing: border-box;
}
.cont {
width: 400px;
background: gold;
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: space-between;
}
.a,
.b,
.c {
width: 45%;
padding: 10px;
}
.a {
background: red;
}
.b {
background: blue;
color: white;
}
.c {
background: orange;
}
However in reality div.b may be taller than div.a, but I don't want the height of div.a or the position of div.c to change:
https://codepen.io/anon/pen/KQxzoz
<div class="cont">
<div class="a">
A
</div>
<div class="b">
B
<br>
More B
</div>
<div class="c">C</div>
</div>
Can I achieve my desired layout with flexbox? Im sure grid could do this but I'm reluctant to use it as its still not properly supported in IE.
Flexbox can't do that by itself in a flexible way.
As you mentioned yourself, CSS Grid can do this, though since you didn't want that, this suggestion is based on the assumption that you at narrower screens want the b to be at the bottom.
By simply initially use float, and then at a certain break point, swap between float and display: flex, it is very easy to take advantage of both.
Additionally, using Flexbox's order property, one can easily position the elements in any desired order, and with this avoid script and get a reasonable level of browser support.
Updated codepen
Stack snippet
* {
box-sizing: border-box;
}
.cont {
background: gold;
overflow: hidden;
}
.a,
.b,
.c {
width: 45%;
padding: 10px;
float: left;
}
.a {
background: red;
}
.b {
background: blue;
color: white;
float: right;
}
.c {
background: orange;
}
#media (max-width: 500px) {
.a,
.b,
.c {
width: auto;
float: none;
}
.b {
order: 1;
}
.cont {
display: flex;
flex-direction: column;
}
}
<div class="cont">
<div class="a">
A
</div>
<div class="b">
B
<br>
More B
</div>
<div class="c">C</div>
</div>