Center divs in flexbox - html

I am using flex and I am trying to achieve something. There are 5 divs shown in the code. In min-width:768px I want the fourth and the fifth div to be centered. Can anyone help me? Here is the code:
css
.row{
display: -ms-flexbox;
display: flex;
-ms-flex-wrap: wrap;
flex-wrap: wrap;
}
.col {
position: relative;
width: 100%;
border:1px solid black;
}
#media (min-width:576px){
.col {
-ms-flex: 0 0 33%;
flex: 0 0 33%;
max-width: 33%;
}
}
#media (min-width: 768px){
.col {
-ms-flex: 0 0 19%;
flex: 0 0 19%;
max-width: 19%;
}
}
html
<div class="row">
<div class="col"><p>first</p> </div>
<div class="col"><p>second</p> </div>
<div class="col"><p>third</p> </div>
<div class="col"><p>fourth</p> </div>
<div class="col"><p>fifth</p> </div>
</div>

You could try this in your media query:
#media (min-width: 768px){
.col {
-ms-flex: 0 0 19%;
flex: 0 0 19%;
max-width: 19%;
}
.col:nth-child(4),
.col:nth-child(5){
text-align: center;
}
}

here is your code I have correct it. you just work with little bit of css
.row{
display: -ms-flexbox;
display: flex;
-ms-flex-wrap: wrap;
flex-wrap: wrap;
justify-content: center;
}
.col {
position: relative;
width: 100%;
border:1px solid black;
}
#media (min-width:576px){
.col {
-ms-flex: 0 0 19%;
flex: 0 0 19%;
max-width: 19%;
}
}
#media (min-width: 768px){
.col {
-ms-flex: 0 0 33%;
flex: 0 0 33%;
max-width: 33%;
}
}
<div class="row">
<div class="col"><p>first</p> </div>
<div class="col"><p>second</p> </div>
<div class="col"><p>third</p> </div>
<div class="col"><p>fourth</p> </div>
<div class="col"><p>fifth</p> </div>
</div>

Related

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>

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>

Flex child wrapping when it shouldn't; flex-wrap: nowrap not working

No idea why, but flex-flow: row nowrap doesn't seem to be preventing the orange div from wrapping. The green and orange divs should be side by side - what am I doing wrong here? The orange div also seems to have no height.
Any help would be appreciated here, thank you.
.appShopSummaryContainer {
display: flex;
flex-flow: column wrap;
}
.appShopSummaryProductWrap {
flex-basis: 100%;
width: 100%;
background: pink;
flex-flow: row nowrap;
display: flex;
}
.appShopSummaryImg {
flex-basis: 40%; /* flex: 0 0 40% */
width: 40%;
height: auto;
padding-bottom: 26.667%;
background: green;
}
.appShopSummaryInfo {
flex-basis: 60%; /* flex: 0 0 60% */
width: 60%;
background: orange;
height: 100%;
}
<div class='appShopSummaryContainer'>
<div class='appShopSummaryProductWrap'>
<a href='#'><div class='appShopSummaryImg'></div>erg</a>
<div class='appShopSummaryInfo'>wgh</div>
</div>
<div class='appShopSummaryProductWrap'>
<a href='#'><div class='appShopSummaryImg'></div>jyu</a>
<div class='appShopSummaryInfo'>erge</div>
</div>
</div>
you can try out this code
<div class="appShopSummaryContainer">
<div class="appShopSummaryProductWrap">
<div class="appShopSummaryInfo"></div>
</div>
</div>
</br>
<div class='appShopSummaryContainer'>
<div class='appShopSummaryProductWrap'>
<a href='#'class='appShopSummaryImg' ><div>test</div></a>
<div class='appShopSummaryInfo'>test1</div>
</div>
</div>
css goes here
.appShopSummaryContainer {
display: flex;
flex-flow: column wrap;
}
.appShopSummaryProductWrap {
flex-basis: 100%;
background: pink;
height:100%;
width:100%;
display: flex;
flex-flow: row nowrap;
}
.appShopSummaryImg {
flex-basis: 40%; /* flex: 0 0 40% */
width: 40%;
height: auto;
padding-bottom: 26.667%;
background: green;
}
.appShopSummaryInfo {
flex-basis: 60%; /* flex: 0 0 60% */
width: 60%;
background: orange;
padding-bottom: 26.667%;
height: 100%;
}

Items order in flexbox container

I'm trying to build media query, which will be working like on the sketch. Any suggestion?
.container {
display: flex;
flex-direction: row;
justify-content: space-between;
background-color: lightblue;
width: 100%;
padding: .5rem;
box-sizing: border-box;
}
.name {
background-color: white;
padding: 1rem;
margin: .25rem;
flex-basis: 40px
}
.options {
display: flex;
flex-direction: row;
}
.option {
background-color: white;
padding: 1rem;
margin: .25rem;
flex-basis: 80px;
}
.action {
background-color: white;
padding: 1rem;
margin: .25rem;
}
#media (max-width: 350px){
.name {order: 1}
.action {order: 2}
.options {order: 3}
.container {
flex-direction: column;
flex-wrap: wrap;
}
}
<div class="container">
<div class="name">Lorem ipsum</div>
<div class="options">
<div class="option">2</div>
<div class="option">3</div>
<div class="option">4</div>
</div>
<div class="action">
5
</div>
</div>
I have already started, but I'm not really satisfied :). I need something more stable, as I will want to use it here later.
https://codepen.io/danzawadzki/pen/mwPYMz
At this moment I'm changing order and flex-direction in media query, but it's not good enough. Box number 1 will contain name of the segment, so it should have fixed width. There will be multiple items like that in one column, so I would prefer to keep it looks clean with same proportions.
Use this may be it will work for you
#media (max-width: 350px){
.options {order: 3; flex:0 0 100%;}
.container {
flex-wrap: wrap;
}
}
If you change your #media and remove flex-direction: column and add flex-basis: 100% to the option, it will flow as your image shows
#media (max-width: 350px){
.options {
order: 1; flex-basis: 100%;
}
.container {
flex-wrap: wrap;
}
}
Note, I also removed the .name and .action rules, as they are not necessary
Fiddle demo
Stack snippet
.container {
display: flex;
flex-direction: row;
justify-content: space-between;
background-color: lightblue;
width: 100%;
padding: .5rem;
box-sizing: border-box;
}
.name {
background-color: white;
padding: 1rem;
margin: .25rem;
flex-basis: 40px
}
.options {
display: flex;
flex-direction: row;
}
.option {
background-color: white;
padding: 1rem;
margin: .25rem;
flex-basis: 80px;
}
.action {
background-color: white;
padding: 1rem;
margin: .25rem;
}
#media (max-width: 350px){
.options {
order: 1; flex-basis: 100%;
}
.container {
flex-wrap: wrap;
}
}
<div class="container">
<div class="name">Lorem ipsum</div>
<div class="options">
<div class="option">2</div>
<div class="option">3</div>
<div class="option">4</div>
</div>
<div class="action">
5
</div>
</div>
Updated
By setting .options { flex-grow: 1; } and .option { flex: 1 1 80px; }, you can have the options/option elements to fill the remaining space on wider screens
Fiddle demo 2
Stack snippet 2
.container {
display: flex;
flex-direction: row;
justify-content: space-between;
background-color: lightblue;
width: 100%;
padding: .5rem;
box-sizing: border-box;
}
.name {
background-color: white;
padding: 1rem;
margin: .25rem;
flex-basis: 40px
}
.options {
flex-grow: 1;
display: flex;
}
.option {
background-color: white;
padding: 1rem;
margin: .25rem;
flex: 1 1 80px;
}
.action {
background-color: white;
padding: 1rem;
margin: .25rem;
}
#media (max-width: 350px){
.options {
order: 1; flex-basis: 100%;
}
.container {
flex-wrap: wrap;
}
}
<div class="container">
<div class="name">Lorem ipsum</div>
<div class="options">
<div class="option">2</div>
<div class="option">3</div>
<div class="option">4</div>
</div>
<div class="action">
5
</div>
</div>

Flex CSS with fixed width and equal height

I have three sections in a container. When I resize my browser to the max-width of 668px, I need to make the section 1 and section 3 in one row and the section 2 in the below row. The section 2 width should be proportional to the section 1 and section 3 width.
But now once I minimize the browser size to 668px and below, then section 3 is not visible.
This is what I tried.
#media (max-width: 668px) {
.container {
display: flex;
flex-wrap: wrap;
justify-content: center;
align-self: flex-start;
}
.container .section1 {
height: 300px;
}
.container .section1,
.container .section3 {
flex: 0 0 262px;
margin: 3px;
display: block;
flex-grow: 0;
flex-shrink: 0;
}
.container .section2 {
flex: 0 0 500px;
flex-grow: 0;
flex-shrink: 0;
order: 1;
min-height: 235px;
}
}
#media (max-width: 940px) {
.container {
display: flex;
flex-wrap: wrap;
justify-content: center;
align-self: flex-start;
}
.container .section1 {
height: 300px;
}
.container .section1,
.container .section3 {
flex: 0 0 262px;
margin: 3px;
display: block;
flex-grow: 0;
flex-shrink: 0;
}
.container .section2 {
flex: 0 0 500px;
flex-grow: 0;
flex-shrink: 0;
order: 1;
min-height: 235px;
}
}
<div class="container">
<div class="section1">Section 1</div>
<div class="section2">Section 2</div>
<div class="section3">Section 3</div>
</div>
You don't have any height specified on .section3.
On .section1 you have height: 300px.
On .section2 you have min-height: 235px.
But on .section3 you have nothing. Try this adjustment to your code:
.section1, .section3 {
height: 300px;
}
jsFiddle demo