This question already has answers here:
Make a div span two rows in a grid
(2 answers)
Is it possible for flex items to align tightly to the items above them?
(5 answers)
Closed 4 years ago.
What I am trying to achieve is when the layout breaks down to mobile the image block matches the same height as the title/content blocks.
The layout is quite tricky, it works as expected on desktop view with the title block being at the top full width. I think the issue is using flex-wrap: wrap on the mobile view? (I have got this layout working using CSS grid but unfortunately it cannot be used on production)
.wrapper {
display: flex;
flex-wrap: wrap;
width: 100%;
justify-content: flex-end;
}
.title {
background: tomato;
width: 50%;
}
.image {
background: cornflowerblue;
width: 50%;
height: 150px;
}
.content {
background: gold;
width: 50%;
}
<div class="wrapper">
<div class="image">img</div>
<div class="title">title</div>
<div class="content">content</div>
</div>
http://jsfiddle.net/8dvhew3q/4/
use this code
.wrapper {
display: flex;
flex-wrap: wrap;
width: 100%;
justify-content: flex-end;
}
.title,
.image,
.content {
width: 50%;
}
.title {
background: tomato;
}
.image {
background: cornflowerblue;
}
.content {
background: gold;
}
#media only screen and (min-width: 800px) {
.title {
width: 100%;
order: 1;
}
.image {
order: 2;
height: 150px;
}
.content {
order: 3;
}
}
Related
This question already has answers here:
Make a div span two rows in a grid
(2 answers)
Closed 4 months ago.
The community reviewed whether to reopen this question 4 months ago and left it closed:
Original close reason(s) were not resolved
Hi this is my example layout on mobile device.
https://codesandbox.io/s/cocky-dust-xuurof?file=/src/styles.css
On the wider screen the third element must be the same width like a second yellow element. The second and the third element. The second and third items together are the same height as the first item. The html structure cannot change, I need use Flexbox, not css grid.
.App {
font-family: sans-serif;
text-align: center;
display: flex;
border: 2px solid black;
flex-wrap: wrap;
}
.one {
background-color: red;
flex-basis: 120px;
height: 80px;
}
.two {
background-color: yellow;
flex-grow: 1;
}
.three {
background-color: green;
height: 30px;
flex-basis: 100%;
}
#media only screen and (min-width: 500px) {
.one {}
.two {}
.three {}
}
<div class="App">
<div class="one">1</div>
<div class="two">2</div>
<div class="three">3</div>
</div>
I modified the media query to account for this snippet.
You should see "Large Screen" results in the snippets "Full Page" mode.
.App {
font-family: sans-serif;
text-align: center;
display: flex;
border: 2px solid black;
flex-wrap: nowrap;
}
.one {
background-color: red;
width: 120px;
height: 80px;
align-items: stretch;
}
.two {
background-color: yellow;
flex: 4 1 auto;
}
.three {
background-color: green;
height: initial;
flex: 4 1 auto;
}
#media screen and (width < 1024px) {
.App {
flex-wrap: wrap;
}
.two {
flex: 4 0 auto;
}
.three {
height: 30px;
flex: 4 0 100%;
}
}
<div class="App">
<div class="one">1</div>
<div class="two">2</div>
<div class="three">3</div>
</div>
I've been trying to achieve the layout below using flexbox. I originally had a left hand sidebar containing the image & navigation, and a main content area. On mobile, the sidebar used to wrap under the main content.
The problem with that is that I need the image to remain at the top on mobile, so I've been trying with three sibling divs in one wrapper div.
Is this even possible with flexbox or will I need to use css grid?
Although CSS Grid would be the best approach to achieve the lay-out you want, it is possible using CSS Flexbox.
You just have to create a wrapper div with three divs inside (when doing a mobile first approach) and with .content set to flex: 1 to stretch out the height of your viewport.
Then for desktop (in this case #media screen and (min-width: 1000px)), change the order (MDN reference of order) of .navigation and .content and give all three divs appropriate widths according to their needs. The only change to div.wrapper is that it needs flex-flow: column wrap to wrap correctly.
.wrapper {
display: flex;
flex-direction: column;
height: 100%;
min-height: 100%;
}
.box {
display: flex;
}
.content {
flex: 1;
}
#media screen and (min-width: 1000px) {
.wrapper {
flex-flow: column wrap;
}
.navigation {
order: 2;
}
.content {
order: 3;
}
.image,
.navigation {
width: 200px;
flex: 50%;
}
.content {
width: calc(100% - 200px);
flex: 0 0 100%;
}
}
/* Generic styling */
html,
body {
height: 100%;
}
body {
margin: 0;
padding: 0;
}
.image {
background: orange;
height: 60px;
}
.content {
background: lightblue;
}
.navigation {
background: lightgreen;
height: 60px;
}
<div class="wrapper">
<div class="box image">Image</div>
<div class="box content">Content</div>
<div class="box navigation">Navigation</div>
</div>
Sorry, another flexbox related question :)
I have two flex elements :
A container (red) containing a centered div (yellow)
A footer (blue) with an undefined height
The red container has a flex-grow:1 attribute, forcing it to take the remaining space on the screen
The issue happens when the yellow element is bigger than the screen size. I would like my red container to grow based on its content. Any idea of how I could do that ?
HTML:
<div class="container">
<div class="content"></div>
</div>
<div class="footer"></div>
CSS:
body,
html {
margin: 0;
height: 100%;
display: flex;
flex-direction: column;
}
.container {
flex-grow: 1;
display: flex;
align-items: center;
justify-content: center;
background: red;
}
.content {
background: yellow;
height: 2000px;
width: 100px;
}
.footer {
flex-shrink: 0;
height: 50px;
background-color: blue;
}
https://codepen.io/stepinsight/pen/roRVGQ
== EDIT ==
Andre helped me find the answer, thanks heaps !
The only thing you need to change in the code above is to replace height by min-height and the % by vh for the body/html tags 🎉
body,
html {
margin: 0;
min-height: 100vh;
display: flex;
flex-direction: column;
}
Simply remove the height property on the body element and add height: 100% to html
* { box-sizing: border-box; }
html {
height: 100%
}
body {
display: flex;
flex-direction: column;
margin: 0;
padding: 0;
}
.container {
display: flex;
align-items: center;
justify-content: center;
background: red;
}
.content {
background: yellow;
height: 2000px;
width: 100px;
}
.footer {
height: 50px;
background-color: blue;
}
Corrected: https://codepen.io/ferreirandre/pen/maoVvb
Feel free to play around with the height of .content
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.
I have the following layout using flexbox:
I want to have the div containing 2 on the right hand side, and the Team and Scorers should make up the space to the left of it.
Required layout:
It's the same idea as the 2 div having a rowspan of 2, if using a table.
Is there a way to position Team and Scorers to the left of 2 without wrapping them in their own div? If so, is it worth the trouble?
Here is my CSS so far:
.container {
max-width: 600px;
}
.team {
background-color: chartreuse;
}
.score {
background-color: brown;
}
.scorers {
background-color: steelblue;
}
.cards-desktop {
background-color: goldenrod;
}
.carded-players {
background-color: darkorange;
}
.left-col {
display: flex;
flex-flow: row wrap;
}
.left-col > * {
flex: 1 100%;
}
.team {
order: 1;
}
.score {
order: 3;
}
.scorers {
order: 2;
}
.cards-desktop {
order: 4;
}
.carded-players {
order: 5;
}
.team {
flex: 1 auto;
}
.score {
flex: 0 150px;
font-size: 60px;
}
The layout will be different on other breakpoints, so I want to have one HTML block that doesn't get duplicated or mimicked for other breakpoints. That's why I don't want to wrap these two divs in a container, because it's unnecessary on other breakpoints' layouts.
Codepen Link
Here..
Wrap 1, 2 & 3 in their own div with display:flex / flex-direction:column / flex-wrap:wrap.
Then set widths on the various components to suit.
Unfortunately, I think Chrome this requires a fixed height on that wrapper to force the wrap (it's a bug I think)...and there you have.
* {
box-sizing: border-box;
}
.team {
background: chartreuse;
}
.score {
background: brown;
}
.scorers {
background: steelblue;
}
.cards-desktop {
background: goldenrod;
}
.carded-players {
background: darkorange;
}
.wrap {
width: 80%;
margin: auto;
display: flex;
flex-direction: column;
}
.top > div {
padding: 5px;
}
.bottom > div {
height: 25px;
}
.top {
display: flex;
flex-direction: column;
flex-wrap: wrap;
height: 150px;
}
.team,
.scorers {
height: 50%;
width: 75%;
}
.score {
width: 25%;
flex: 1 0 100%;
display: flex;
align-items: center;
justify-content: center;
font-size: 28px;
}
<div class="wrap">
<div class="top">
<div class="team">Team</div>
<div class="scorers">Scorers</div>
<div class="score">
<h1>2</h1>
</div>
</div>
<div class="bottom">
<div class="cards-desktop">cards-desktop</div>
<div class="carded-players">carded-players</div>
</div>
</div>
I've been looking around and could not find a way to move specific containers to the sides of the page, whilst leaving the other containers intact.
What I would would like to achieve is the following layouts for mobile and desktop screens, respectively: Desktop and Mobile
Note the colors: the third row on the mobile layout should become a left column on the desktop layout, and the fifth row on the mobile layout should become a right column on the desktop layout.
The rest of the rows should stay as a middle column on desktops.
I was trying to achieve that by using Flexbox but could not get it to done properly.
I would love to hear suggestions.
Thanks!
Seemed like an interesting exercise. It's a bit rough but the basics are there.
Codepen demo
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
html,
body {
height: 100%;
}
body {
min-height: 100%;
}
.wrap {
height: 100%;
border: 1px solid grey;
display: flex;
flex-direction: column;
}
.child,
.aside {
flex: 1;
background: plum;
order: 1;
}
.aside {
background: #c0ffee;
}
#media screen and (min-width: 760px) {
.wrap {
flex-wrap: wrap;
}
.child {
order: 2;
width: 80%;
flex: 1 0 25%;
}
.left {
order: 1;
width: 10%;
flex: 0 0 100%;
}
.right {
order: 3;
width: 10%;
flex: 0 0 100%;
}
}
<div class="wrap">
<div class="child">One</div>
<div class="child">Two</div>
<div class="aside left">Three</div>
<div class="child">Four</div>
<div class="aside right">Five</div>
<div class="child">Six</div>
</div>