I tried switching one sandwich column and two columns by using flex. However, I could not do it without using two div tags and display: none;
Could you please give me any advice?
One column image
Two columns image
HTML
<div class="container">
<div class="category1">
<div class="timezone1">timezone1</div>
<div class="menu1">menu1</div>
<div class="timezone2">timezone2</div>
<div class="menu2">menu2</div>
<div class="timezone3">timezone3</div>
<div class="menu3">menu3</div>
</div>
</div>
<div class="pc-container">
<div class="pc-category1">
<div class="pc-timezone1">timezone1</div>
<div class="pc-timezone2">timezone2</div>
<div class="pc-timezone3">timezone3</div>
</div>
<div class="pc-category2">
<div class="pc-menu1">menu1</div>
<div class="pc-menu2">menu2</div>
<div class="pc-menu3">menu3</div>
</div>
</div>
CSS
.container {
display: flex;
flex-flow: column wrap;
}
.pc-container {
display: none;
}
#media screen and (min-width: 768px){
.container {
display: none;
}
.pc-container {
display: flex;
flex-flow: row wrap;
}
}
These images are added July 28.
Initial screen
Narrow to show smartphone screen
Re-large to show PC screen, but each column is not side by side.
Just switch from columns to wrapping rows....
.category1 {
display: inline-flex;
border: 1px solid grey;
padding: .5em;
flex-direction: column;
}
#media screen and (min-width: 500px) {
.category1 {
flex-direction: row;
flex-wrap: wrap;
}
.category1 > div {
white-space: nowrap;
flex: 0 1 50%;
}
.category1 > [class^="menu"] {
text-align: right;
}
}
<div class="container">
<div class="category1">
<div class="timezone1">timezone1</div>
<div class="menu1">menu1</div>
<div class="timezone2">timezone2</div>
<div class="menu2">menu2</div>
<div class="timezone3">timezone3</div>
<div class="menu3">menu3</div>
</div>
</div>
Codepen Demo
Here's another option: http://codepen.io/panchroma/pen/LkALXG/
HTML
<div class="container">
<div class="timezone">timezone1</div>
<div class="menu">menu1</div>
<div class="timezone">timezone2</div>
<div class="menu">menu2</div>
<div class="timezone">timezone3</div>
<div class="menu">menu3</div>
</div>
CSS
.timezone,
.menu{
width:50vw;
display:inline-block;
}
.menu{
float:right;
}
#media screen and (max-width: 767px){
.menu,
.timezone{
float:none;
display:block;
width:auto;
}
}
Good luck!
Related
I have issues with a layout where I want to center the last two elements on a row. First I tried with grid and couldn't find any proper solution so I found a way to solve it with "flex-wrap" and it looks pretty good.
The issue I'm facing now is with the gap/margin. I can't find any good solution while I'm setting the width to 33,33%.
Is there any other good way to solve this? I'm kind of stuck and have been looking at this for hours now :) (see layout attached image)
#container {
display: flex;
flex-wrap: wrap;
justify-content: center;
flex-direction: column;
}
#media only screen and (min-width: 640px) {
#container {
display: flex;
flex-wrap: wrap;
justify-content: center;
flex-direction: row
}
}
.item {
width: 100%;
background: teal;
color: white;
border-radius: 10px;
text-align: center;
}
#media only screen and (min-width: 640px) {
.item {
width: 33.33%;
}
}
<div id="container">
<div class="item">
<p>Item</p>
</div>
<div class="item">
<p>Item</p>
</div>
<div class="item">
<p>Item</p>
</div>
<div class="item">
<p>Item</p>
</div>
<div class="item">
<p>Item</p>
</div>
</div>;
You can use the gap parameter, see the example below.
Update:
Solution 1
Stretched Last element
#container {
display: flex;
flex-wrap: wrap;
align-items: center;
justify-content: center;
gap: 20px;
}
.item {
width: 100%;
background: teal;
color: white;
border-radius: 10px;
text-align: center;
}
/* Start Media Query*/
#media only screen and (min-width: 640px) {
#container {
display: flex;
justify-content: center;
flex-direction: row;
}
.item {
flex: 1 1 30%;
}
} /* End Media Query*/
<div id="container">
<div class="item">
<p>Item</p>
</div>
<div class="item">
<p>Item</p>
</div>
<div class="item">
<p>Item</p>
</div>
<div class="item">
<p>Item</p>
</div>
<div class="item">
<p>Item</p>
</div>
</div>
Solution 2
Not stretched last element
#container {
display: flex;
flex-wrap: wrap;
align-items: center;
justify-content: center;
gap: 20px;
}
.item {
width: 100%;
background: teal;
color: white;
border-radius: 10px;
text-align: center;
}
/* Start Media Query*/
#media only screen and (min-width: 640px) {
#container {
display: flex;
justify-content: center;
flex-direction: row;
}
.item {
flex: 0 0 calc(33.33% - 15px);
}
} /* End Media Query*/
<div id="container">
<div class="item">
<p>Item</p>
</div>
<div class="item">
<p>Item</p>
</div>
<div class="item">
<p>Item</p>
</div>
<div class="item">
<p>Item</p>
</div>
<div class="item">
<p>Item</p>
</div>
</div>
This is what my page looks like:
This is what happens when i resize the browser screen:
This is what happens when i resize it all the way down:
Essentially what I want is to have both left and right columns align side by side responsively with the same height and text in the middle until i reach a certain point and stack under each other.
This is my main code:
.left {
padding: 0;
}
.right {
background-color: #000;
color: #FFF;
}
.no-space {
padding: 0;
}
.row {
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
flex-wrap: wrap;
}
.section-content {
padding-top: 13.5%;
}
#media screen and (max-width: 1200px) {
.right {
height: 33.3%;
width: 100%;
align-content: center;
padding-top: 35px;
padding-bottom: 25px;
}
}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap#4.3.1/dist/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<div class="container-fluid no-space">
<div class="row imgfit">
<div class="col-sm-6 col-lg-6 left">
<img class="img-responsive imgfit" src="https://c4.wallpaperflare.com/wallpaper/860/864/1012/neon-aesthetic-wallpaper-preview.jpg">
</div>
<div class="col-sm-6 col-lg-6 right center-block text-center section-content">
<p class="pagination-centered futura imgfit">
TEXT
<br><br> TEXT
</p>
</div>
</div>
I have a container that has three images. Two on the left side and one on the right side on the mobile layout. When I hit a larger breakpoint, I want the image in the middle to be centered. I have been trying to do solve this using flexbox but I'm not able to center the image. I know it can be done using position: absolute but I was wondering if there's a way to do it with flexbox.
.container{
display:flex;
}
.left {
display: flex;
align-items: center;
flex-grow: 1;
}
.back {
height: 20px;
}
.logo {
height: 30x;
}
#media (min-width: 640px){
.left {
justify-content: space-between;
}
}
<div class="container">
<div class="left">
<img class="back" src="https://placeimg.com/20/20/animals"/>
<img class="logo" src="https://placeimg.com/200/50/animals"/>
</div>
<div class="right">
<img src="https://placeimg.com/100/20/animals"/>
</div>
</div>
Add margin:0 auto; to .logo in large screen sizes.
#media (min-width: 640px){
.logo{
margin:0 auto;
}
}
.container{
display:flex;
}
.left {
display: flex;
align-items: center;
flex-grow: 1;
}
.back {
height: 20px;
}
.logo {
height: 30x;
}
#media (min-width: 640px){
.logo{
margin:0 auto;
}
}
<div class="container">
<div class="left">
<img class="back" src="https://placeimg.com/20/20/animals"/>
<img class="logo" src="https://placeimg.com/200/50/animals"/>
</div>
<div class="right">
<img src="https://placeimg.com/100/20/animals"/>
</div>
</div>
justify-content: space-between; in flexbox make the first item is on the start line, last item on the end line so that not what you want.
You have two options :
1- you can use margin: 0 auto for the .logo and it will ok.
.container{
display:flex;
}
.left {
display: flex;
align-items: center;
flex-grow: 1;
}
.back {
height: 20px;
}
.logo {
height: 30x;
}
#media (min-width: 640px){
.logo{
margin:0 auto;
}
}
<div class="container">
<div class="left">
<img class="back" src="https://placeimg.com/20/20/animals"/>
<img class="logo" src="https://placeimg.com/200/50/animals"/>
</div>
<div class="right">
<img src="https://placeimg.com/100/20/animals"/>
</div>
</div>
2- you can give .right{ flex-grow: 1;} on the bigger screen so the image on left will be already centered and on smaller device just remove it.
.container{
display:flex;
}
.left {
display: flex;
align-items: center;
flex-grow: 1;
}
.back {
height: 20px;
}
.logo {
height: 30x;
}
#media (min-width: 640px){
.right {
flex-grow: 1;
text-align: right;
}
.left{
justify-content: space-between;
}
}
<div class="container">
<div class="left">
<img class="back" src="https://placeimg.com/20/20/animals"/>
<img class="logo" src="https://placeimg.com/200/50/animals"/>
</div>
<div class="right">
<img src="https://placeimg.com/100/20/animals"/>
</div>
</div>
I've built a row layout with Flexbox and within each row (.card), there are three columns, the last (.card-links) of which is also a Flex container. When these three columns stack as rows on mobile, the last doesn't grow with its content + padding and its height is only its padding.
I've found two workarounds where I can set it as margin instead, or display it as a block on mobile, but I'd really like to keep using padding as I have everywhere else and understand why this is happening.
HTML structure:
<div class="card">
<div class="card-img four-three-img">
<img src="" alt="">
</div>
<div class="card-info">
<div class="event-headings">
<h1>Heading</h1>
<h2>Subheading</h2>
</div>
<p>
</p>
<p>Text</p>
</div>
<div class="card-links">
<div class="button-cont">
<a class="pass-button button" href="" target="_blank" role="button">Button Text</a>
</div>
</div>
</div>
Relevant CSS:
.card {
display: flex;
flex-direction: column;
}
.card-img,
.card-info,
.card-links {
flex-basis: 33.3%;
position: relative;
padding: 15px;
}
.card-info {
flex-basis: 40%;
}
.card-links {
display: flex;
justify-content: center;
align-items: center;
flex-basis: 26.7%
}
#media screen and (max-width: 768px) {
.card-links {
padding: 15px 15px 45px;
}
}
I try to create this little gallery grid with flexbox, but I fail all time.
Is it possible with flexbox?
Here is my example and fiddle.
<div class="gallery">
<div class="box one">Box 1</div>
<div class="box two">Box 2</div>
<div class="box three">Box 3</div>
<div class="box four">Box 4</div>
<div class="box five">Box 5</div>
</div>
.gallery {
display:flex;
display: -webkit-flex;
-webkit-flex-wrap: wrap;
flex-wrap: wrap;}
.gallery .box {
background:#ccc;
height:250px;
width:33.33333%;}
.gallery .box.one,
.gallery .box.two {
-webkit-flex:1;
flex: 1;}
.gallery .box.three {
height:500px;}
.gallery .box.four,
.gallery .box.five {
-webkit-flex:1;
flex: 1;}
Ok, so have you tried using flex-direction: column? It requires a slight change in the way you think about flexbox. Try the following:
.gallery {
display: flex;
flex-direction: column;
flex-wrap: wrap;
max-height: 200px; // or however you want to do it, required for wrapping
}
.box {
height: 100px;
}
.three {
height: 200px;
}
You can try something like this:
create 3 parent divs .
create two child divs in each 1st and 3rd div with width 100% and height 50%
and flex-direction column.
#grid{
width:100%;
height:500px;
background:#eee;
display:flex;
}
#p1,#p2,#p3{
width:33%;
border:2px solid #fff;
display:flex;
flex-direction:column;
}
#pone,#ptwo,#p31,#p32{
flex-basis:100%;
height:50%;
border:3px solid white;
}
<div id="grid">
<div id="p1">
<div id="pone">
</div>
<div id="ptwo">
</div>
</div>
<div id="p2">
</div>
<div id="p3">
<div id="p31">
</div>
<div id="p32">
</div>
</div>
</div>