How to center divs at every breakpoint with flexbox? - html

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>

Related

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%;

Scroll in a deeply nested flex container - min-height not working

I have the following flex layout. I need overflow to occur in div.stretchy. I would like div.stretchy to reach the boundary of the page and then overflow it's content. Per this stackoverflow post, I have tried many combinations of min-height: 0 and overflow: hidden, but div.stretchy will not shrink.
body {
margin: 0;
}
.page-wrapper {
display: flex;
height: 100vh;
width: 100vw;
overflow: hidden;
}
.sidebar {
background: blue;
flex: 0 0 40px;
}
.main {
background: green;
display: flex;
flex-direction: column;
flex: 1;
min-width: 0;
}
.topbar {
display: flex;
flex: 0 0 40px;
background-color: red;
}
.content {
display: flex;
overflow: auto;
}
.row {
display: flex;
}
.column {
display: flex;
flex-direction: column;
}
.grow {
flex-grow: 1;
}
.card {
height: 300px;
border: solid 1px;
min-width: 600px;
}
.card .row {
justify-content: space-between;
}
.wrapper {
padding: 16px;
height: fit-content;
}
.stats {
padding: 8px;
background-color: pink;
}
.overflow-hidden {
overflow: hidden;
}
.body .column {
background-color: indigo;
}
.wide-content {
background-color: yellow;
height: 50px;
width: 800px;
}
.block {
flex: none;
width: 300px;
height: 200px;
&:nth-of-type(odd) {
background: darken(green, 10%);
}
}
<div class="page-wrapper">
<div class="sidebar">sidebar</div>
<div class="main">
<div class="topbar">topbar</div>
<div class="content">
<div class="wrapper">
<div class="card column grow">
<div class="stats row">
<span>12345</span>
<span>12345</span>
<span>12345</span>
</div>
<div class="body row grow">
<div class="column">
<span>Dynamic Width Content</span>
</div>
<div class="stretchy column grow overflow-hidden">
<div class="wide-content grow"></div>
</div>
<div class="column">
<span>Dynamic Width Content</span>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
This is a tough battle. The enemy is cunning, deceitful and ruthless. I say we launch a massive carpet bombing campaign, showering the whole area with min size overrides. That should clear out 80% of the problem. Then we send in the ground troops to finish the job :-)
* {
min-width: 0 !important;
min-height: 0 !important;
}
.page-wrapper {
display: flex;
height: 100vh;
/* width: 100vw; */
/* overflow: hidden; */
}
.sidebar {
background: cornflowerblue;
/* flex: 0 0 40px; */
flex: 0 0 100px; /* changed for demo purposes */
}
.main {
background: lightgreen;
display: flex;
flex-direction: column;
flex: 1;
}
.topbar {
display: flex;
flex: 0 0 40px;
background-color: orangered;
}
.content {
display: flex;
/* overflow: auto; */
flex: 1; /* added */
}
.row {
display: flex;
}
.column {
display: flex;
flex-direction: column;
}
.grow {
flex-grow: 1;
}
.card {
/* height: 300px; */
border: solid 1px;
min-width: 600px;
}
.stretchy {
overflow: auto;
}
.card .row {
justify-content: space-between;
}
.wrapper {
padding: 16px;
/* height: fit-content; */
display: flex; /* added */
}
.stats {
padding: 8px;
background-color: pink;
}
.overflow-hidden {
/* overflow: hidden; */
}
.body .column {
background-color: violet;
}
.wide-content {
background-color: yellow;
height: 50px;
width: 800px;
}
body {
margin: 0;
}
/*
.block {
flex: none;
width: 300px;
height: 200px;
&:nth-of-type(odd) {
background: darken(green, 10%);
}
}
*/
<div class="page-wrapper">
<div class="sidebar">sidebar</div>
<div class="main">
<div class="topbar">topbar</div>
<div class="content">
<div class="wrapper">
<div class="card column grow">
<div class="stats row">
<span>12345</span>
<span>12345</span>
<span>12345</span>
</div>
<div class="body row grow">
<div class="column">
<span>Dynamic Width Content</span>
</div>
<div class="stretchy column grow overflow-hidden">
<div class="wide-content grow">test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br></div>
</div>
<div class="column">
<span>Dynamic Width Content</span>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
jsFiddle demo

How to make middle div higher than first and second

I have three divs and I want the middle div to be higher than other two.
I tried using top:-50 for the second div, but it didn't work.
.cards {
margin: 0 auto;
display: flex;
flex-wrap: wrap;
justify-content: center;
}
.card {
flex: 0 0 31%;
margin: 0 1% 2% 1%;
height: 300px;
background-color: red;
}
#media only screen and (max-width: 460px) {
.card {
flex: 0 0 48%;
}
}
#media only screen and (max-width: 360px) {
.card {
flex: 0 0 100%;
}
}
<div class="cards">
<div class="card">
</div>
<div class="card">
</div>
<div class="card">
</div>
</div>
You're using flexbox! Don't rely on margins.
codepen: https://codepen.io/nstanard/pen/gqYrjZ
.cards {
margin: 0 auto;
display: flex;
flex-wrap: wrap;
height: 350px;
justify-content: bottom;
align-items: flex-end;
}
.card {
flex: 0 0 31%;
margin: 0 10px;
height: 300px;
background-color: red;
}
.card:nth-child(2) {
align-self: flex-start;
}
#media only screen and (max-width: 460px) {
.card {
flex: 0 0 48%;
}
}
#media only screen and (max-width: 360px) {
.card {
flex: 0 0 100%;
}
}
<div class="cards">
<div class="card">
</div>
<div class="card">
</div>
<div class="card">
</div>
</div>
snippet:
.cards {
margin: 0 auto;
display: flex;
flex-wrap: wrap;
height: 350px;
align-items: center;
}
.card {
flex: 0 0 31%;
margin: 0 1% 2% 1%;
height: 300px;
background-color: red;
}
.card:nth-child(2) {
align-self: flex-start;
}
#media only screen and (max-width: 460px) {
.card {
flex: 0 0 48%;
}
}
#media only screen and (max-width: 360px) {
.card {
flex: 0 0 100%;
}
}
<div class="cards">
<div class="card">
</div>
<div class="card">
</div>
<div class="card">
</div>
</div>
for second div use margin-top and margin-bottom is -50px it will works...
so use this class
.card:nth-child(2){
margin:-50px 0;
}
.cards {
margin: 0 auto;
display: flex;
flex-wrap: wrap;
justify-content: center;
padding-top:60px;
}
.card {
flex: 0 0 31%;
margin: 0 1% 2% 1%;
height: 300px;
background-color: red;
}
.card:nth-child(2){
margin:-50px 0;
}
#media only screen and (max-width: 460px) {
.card {
flex: 0 0 48%;
}
}
#media only screen and (max-width: 360px) {
.card {
flex: 0 0 100%;
}
}
<div class="cards">
<div class="card">
</div>
<div class="card">
</div>
<div class="card">
</div>
</div>

Flexbox CSS Arrangment of Divs

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%;
}
}

Wrap element to new line/row using flexbox

I have trouble forcing an item into the next row in a flexbox layout.
How can I do something like the following image?
This is what I got so far:
#wrap {
display: flex;
width: 86vw;
height: auto;
box-sizing: border-box;
margin: 0 auto;
}
.item1,
.item2 {
width: 50%;
height: 24.5vw;
background: #4add69;
}
.item1 {
margin-right: 10px;
}
.item2 {
margin-left: 10px;
}
.item3 {
width: 60%;
height: 40vw;
background: #d56c6c;
}
<div id="wrap">
<div class="item1"></div>
<div class="item2"></div>
<div class="item3"></div>
</div>
Your code is fine but missing two things.
Use flex-wrap: wrap to
create a new row. Modify the width of the first two items to be
present in a single row.
For the last two items, you need to nest it inside a container and
then wrap them again.
Manipulate the dimension(width, height) and margin values to achieve the perfect/suitable layout.
JSfiddle Demo
* {
margin: 0;
padding: 0;
}
body {
background: #232323;
padding: 10px;
}
#wrap {
display: flex;
width: 86vw;
height: auto;
box-sizing: border-box;
margin: 0 auto;
flex-wrap: wrap;
background: #232323;
/* Added */
}
.item1,
.item2 {
width: 48%;
/* Modified */
height: 24.5vw;
background: #4add69;
margin-bottom: 10px;
}
.item1 {
margin-right: 10px;
}
.item2 {
margin-left: 10px;
}
.item3 {
width: 55%;
height: 40vw;
background: #d56c6c;
margin-right: 10px;
}
.nested-items {
display: flex;
width: 42%;
flex-wrap: wrap;
align-content: space-between;
}
.item4,
.item5 {
background: lightblue;
width: 100%;
height: 49%;
}
<div id="wrap">
<div class="item1"></div>
<div class="item2"></div>
<div class="item3"></div>
<div class="nested-items">
<div class="item4"></div>
<div class="item5"></div>
</div>
</div>
Essentially you need an extra wrapping div for the two 'small' elements like so:
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.wrap {
width: 75%;
margin: 1em auto;
border: 1px solid green;
padding: .25em;
display: flex;
flex-wrap: wrap;
}
.wrap div {
border: 1px solid grey;
margin-bottom: 1px;
}
.box {
height: 80px;
background: lightblue;
flex: 0 0 50%;
}
.tall {
flex: 0 0 65%;
height: 160px;
}
.col {
flex: 0 0 35%;
display: flex;
flex-wrap: wrap;
}
.mini {
flex: 0 0 100%;
height: 80px;
background: pink;
}
<div class="wrap">
<div class="box"></div>
<div class="box"></div>
<div class="box tall"></div>
<div class="box col">
<div class="mini"></div>
<div class="mini"></div>
</div>
</div>
I've used a single overall element here with wrapping but the image suggests that this would be much simpler with actual rows and the extra wrapper mentioned before.
Codepen Demo of 2nd option with rows.