Using flex-boxes and only four divs inside the container…
I cannot handle it…
I was stack on it…
.container {
display: flex;
flex-flow: column nowrap;
}
.x-1 {
background: yellow;
height: 200px;
width: 200px;
margin: 10px 10px 0 0;
}
.x-2 {
background: yellow;
height: 200px;
width: 200px;
margin: 10px 10px 0 0;
}
.x-3 {
background: yellow;
height: 200px;
width: 200px;
margin: 10px 10px 0 0;
}
.x-4 {
background: red;
height: 700px;
width: 800;
margin: 10px 10px 0 0;
}
<div class="container">
<div class="x-1"></div>
<div class="x-2"></div>
<div class="x-3"></div>
<div class="x-4"></div>
</div>
EDIT
I'll start from the beginning. I would like to get the same effect as in the image. I insert the code below.
.container {
display: flex;
padding: 0 100px;
}
.left {
display: flex;
flex-direction: column;
}
.box-1 {
background: yellow;
height: 200px;
width: 200px;
margin: 10px 0;
}
.box-2 {
background: yellow;
height: 200px;
width: 200px;
margin: 10px 0;
}
.box-3 {
background: yellow;
height: 200px;
width: 200px;
margin: 10px 0;
}
.box-4 {
width: 100%;
background: red;
height: 800px;
margin: 10px 10px;
}
<div class="container">
<div class="left">
<div class="box-1"></div>
<div class="box-2"></div>
<div class="box-3"></div>
</div>
<div class="box-4"></div>
</div>
I've rewritten styles to achieve adaptivity, flexibility and maintainability:
.container {
display: flex;
/* taking margins into account, so 90px */
padding: 0 90px;
}
.left {
display: flex;
flex-direction: column;
}
.box-1,
.box-2,
.box-3,
.box-4 {
margin: 10px;
}
.box-1,
.box-2,
.box-3 {
background: yellow;
width: 200px;
height: 200px;
}
.box-4 {
background: red;
flex: 1;
height: 800px;
}
#media only screen and (max-width: 900px) {
.container {
flex-direction: column-reverse;
}
.left {
flex-direction: row;
flex-wrap: wrap;
}
.box-1,
.box-2 {
width: auto;
flex: 1 0 0;
}
.box-3 {
width: auto;
flex: 0 1 100%;
}
}
#media only screen and (max-width: 700px) {
.container {
flex-direction: column-reverse;
}
.left {
flex-direction: column;
flex-wrap: nowrap;
}
.box-1,
.box-2,
.box-3,
.box-4 {
width: auto;
flex: 1 0 auto;
}
}
<div class="container">
<div class="left">
<div class="box-1"></div>
<div class="box-2"></div>
<div class="box-3"></div>
</div>
<div class="box-4"></div>
</div>
Here is jsFiddle to test resizing.
You can wrap the yellow boxes in their own container...
.container {
display: flex;
}
.left {
display: flex;
flex-direction: column;
}
.left div[class^='x'] {
background: yellow;
height: 200px;
width: 200px;
margin: 10px 10px 0 0;
}
.x-4 {
background: red;
height: 700px;
width: 800px;
margin: 10px 10px 0 0;
}
<div class="container">
<div class="left">
<div class="x-1"></div>
<div class="x-2"></div>
<div class="x-3"></div>
</div>
<div class="x-4"></div>
</div>
you just have to have divs to your code
.container {
display: flex;
}
.x-1 {
background: yellow;
height: 200px;
width: 200px;
margin: 10px 10px 0 0;
}
.x-2 {
background: yellow;
height: 200px;
width: 200px;
margin: 10px 10px 0 0;
}
.x-3 {
background: yellow;
height: 200px;
width: 200px;
margin: 10px 10px 0 0;
}
.x-4 {
background: red;
height: 700px;
width: 800px;
margin: 10px 10px 0 0;
}
<div class="container">
<div>
<div class="x-1"></div>
<div class="x-2"></div>
<div class="x-3"></div>
</div>
<div>
<div class="x-4"></div>
</div>
</div>
You can use CSS Grid layout here. Here is demo, I removed redundant .left block and rewrited styles. This will work even in IE10+:
.container {
/* taking margins into account, so 90px */
padding: 0 90px;
display: -ms-grid;
display: grid;
/* taking margins into account, so 220px */
-ms-grid-columns: 220px 1fr;
grid-template-columns: 220px 1fr;
-ms-grid-rows: auto auto 1fr;
grid-template-rows: auto auto 1fr;
}
.box-1,
.box-2,
.box-3,
.box-4 {
margin: 10px;
}
.box-1,
.box-2,
.box-3 {
background: yellow;
height: 200px;
}
.box-2 {
-ms-grid-row: 2;
grid-row: 2;
-ms-grid-column: 1;
grid-column: 1;
}
.box-3 {
-ms-grid-row: 3;
grid-row: 3;
-ms-grid-column: 1;
grid-column: 1;
}
.box-4 {
background: red;
height: 800px;
-ms-grid-row: 1;
-ms-grid-row-span: 3;
grid-row: 1 / span 3;
-ms-grid-column: 2;
grid-column: 2;
}
#media only screen and (max-width: 900px) {
.container {
-ms-grid-columns: 1fr 1fr;
grid-template-columns: 1fr 1fr;
-ms-grid-rows: 1fr auto auto;
grid-template-rows: 1fr auto auto;
}
.box-1 {
-ms-grid-row: 2;
grid-row: 2;
-ms-grid-column: 1;
grid-column: 1;
}
.box-2 {
-ms-grid-row: 2;
grid-row: 2;
-ms-grid-column: 2;
grid-column: 2;
}
.box-3 {
-ms-grid-row: 3;
grid-row: 3;
-ms-grid-column: 1;
-ms-grid-column-span: 2;
grid-column: 1 / span 2;
}
.box-4 {
-ms-grid-row: 1;
-ms-grid-row-span: 1;
grid-row: 1 / span 1;
-ms-grid-column: 1;
-ms-grid-column-span: 2;
grid-column: 1 / span 2;
}
}
#media only screen and (max-width: 700px) {
.container {
-ms-grid-columns: 1fr;
grid-template-columns: 1fr;
-ms-grid-rows: 1fr auto auto auto;
grid-template-rows: 1fr auto auto auto;
}
.box-1,
.box-2,
.box-3,
.box-4 {
-ms-grid-column: 1;
-ms-grid-column-span: 1;
grid-column: 1 / span 1;
}
.box-1 {
-ms-grid-row: 2;
grid-row: 2;
}
.box-2 {
-ms-grid-row: 3;
grid-row: 3;
}
.box-3 {
-ms-grid-row: 4;
grid-row: 4;
}
.box-4 {
-ms-grid-row: 1;
-ms-grid-row-span: 1;
grid-row: 1 / span 1;
}
}
<div class="container">
<div class="box-1"></div>
<div class="box-2"></div>
<div class="box-3"></div>
<div class="box-4"></div>
</div>
Here is jsFiddle to test resizing.
Related
[https://codepen.io/TA0011/pen/VwXKKgG]
When viewed in mobile version, the width of the card exceeds the Content hr (horizontal line). What can be done so that it does not exceed the horizontal line? Do not go with the 5 fr or 1fr unit. I can manage the 1 fr or 5fr scene. But only problem is with the card that exceeds the hr line which is blue in color.
.container-a {
margin-top: 10px;
width: calc(100% - 100px);
margin: 10px auto;
position: relative;
}
.container-a span {
background-color: #007bff;
padding: 5px 10px;
position: relative;
color: #fff;
z-index: -1;
background-blend-mode: multiply;
}
.container-a hr {
width: 100%;
color: #007bff;
margin-top: 4px;
height: 1px;
position: relative;
z-index: -2;
}
.container-a .wrapper {
display: grid;
height: 100%;
grid-template-columns: repeat(5, 1fr);
grid-template-areas: "blog-container blog-container blog-container blog-container blog-sidebar";
grid-gap: 10px;
}
.container-a .blog-container {
grid-area: blog-container;
display: grid;
grid-template-columns: repeat(auto-fill, minmax(270px, 1fr));
grid-gap: 10px;
}
.card {
min-width: 280px;
flex: 1;
-webkit-box-flex: 1;
-ms-flex: 1;
height: 180px;
width: 100%;
border: 1px solid #ccc;
background: #FF7F50;
font-weight: 500 !important;
position: relative;
color: #fff;
}
.container-a .blog-sidebar {
grid-area: blog-sidebar;
}
.blog-sidebar .widgets {
background: #fff;
border: 1px solid #ccc;
margin-bottom: 10px;
display: grid;
grid-template-columns: repeat(auto-fill, minmax(270px, 1fr));
grid-gap: 10px;
padding: 5px;
}
.widgets img {
width: 100%;
float: none;
display: block;
object-fit: fill;
}
.blog-sidebar h3 {
color: #007bff;
text-align: center;
margin-top: 2px 0 2px 0;
margin: -1px 0 -1px 0;
}
#media(max-width: 768px) {
.container-a {
width: calc(100% - 60px);
}
.container-a span {
font-size: 12px;
}
.container-a hr {
margin-top: 3px;
}
.container-a {
width: calc(100% - 60px);
margin-bottom: 100px;
}
.container-a .wrapper {
grid-template-rows: auto;
grid-template-columns: 1fr;
grid-template-areas: "blog-container" "blog-sidebar";
}
.card {
min-width: 244px;
}
}
<section class="container-a" aria-label="blog-content">
<span>Content</span>
<hr>
<div class="wrapper">
<div class="blog-container">
<div class="card">Card 1</div>
<div class="card">Card 2</div>
<div class="card">Card 3</div>
<div class="card">Card 4</div>
<div class="card">Card 5</div>
</div>
<div class="blog-sidebar">
<h3>Sidebar</h3>
<div class="widgets">
<div class="card">Card 1</div>
<div class="card">Card 2</div>
</div>
</div>
</div>
</section>
[I have done it , you can view it][1]
[1]: https://codepen.io/TA0011/pen/ZExJgmr
I have this code:
.container {
margin-right: auto;
margin-left: auto;
max-width: 1400px;
padding-left: 1rem;
padding-right: 1rem;
}
.grid { display: grid }
.vh-100 { min-height: 100vh;}
.grid-gap { grid-gap: 0.7rem; }
.flex { display: flex; -webkit-display: flex; -moz-display: flex; -ms-display: flex; -o-display: flex; }
.text-center { text-align: center; }
.flex-dir-col { flex-direction: column; }
.relative { position: relative; }
.absolute { position: absolute; }
.img-fluid { max-width: 100%; height: auto; }
.content-wrapper .container {
grid-auto-flow: row dense;
-ms-grid-columns: 3fr 1fr;
grid-template-columns: 3fr 1fr;
-ms-grid-rows: 1fr;
grid-template-rows: 1fr;
gap: 0px 35px;
}
.content-wrapper .container .main-content {
-ms-grid-row: 1;
-ms-grid-row-span: 1;
-ms-grid-column: 1;
-ms-grid-column-span: 2;
grid-area: 1 / 1 / 2 / 3;
}
.content-wrapper .container .main-content .post-content .post-content-top .post-sharing .icon {
width: 50px;
height: 50px;
background-color: #f1f1f1;
margin-bottom: 20px;
border-radius: 30%;
-webkit-border-radius: 30%;
-moz-border-radius: 30%;
-ms-border-radius: 30%;
-o-border-radius: 30%;
color: #3ab77d;
-webkit-box-shadow: 0px 5px 15px -5px #00000070;
box-shadow: 0px 5px 15px -5px #00000070;
}
.content-wrapper .container .main-content .post-content .post-content-top .post-sharing .icon i {
line-height: 50px;
font-size: 20px;
transition: all .4s;
-webkit-transition: all .4s;
-moz-transition: all .4s;
-ms-transition: all .4s;
-o-transition: all .4s;
}
.content-wrapper .container .main-content .post-content .post-content-top .post-sharing .icon:hover {
background-color: #3ab77d;
}
.content-wrapper .container .main-content .post-content .post-content-top .post-sharing .icon:hover i {
color: #f1f1f1;
}
.content-wrapper .container .main-content .post-content .post-content-top .post-featured-image {
overflow: hidden;
}
.content-wrapper .container .main-content .post-content .post-content-top .post-featured-image .post-date {
top: 1rem;
left: 1rem;
background-color: #fff;
width: 50px;
height: 50px;
line-height: 1.5;
}
.content-wrapper .container .main-content .post-content .post-content-top .post-featured-image .post-date .day {
font-size: 1rem;
padding-top: .2rem;
}
.content-wrapper .container .main-content .post-content .post-content-top .post-featured-image .post-date .month {
font-size: 0.75rem;
}
.content-wrapper .container .sidebar {
-ms-grid-row: 1;
-ms-grid-row-span: 1;
-ms-grid-column: 2;
-ms-grid-column-span: 1;
grid-area: 1 / 2 / 2 / 3;
position: relative;
background-color: rgba(255, 0, 0, 0.468);
}
<link href="https://cdn.jsdelivr.net/npm/bootstrap-icons#1.8.3/font/bootstrap-icons.css" rel="stylesheet"/>
<section class="content-wrapper mt-2 mb-2">
<div class="container grid vh-100 grid-gap">
<div class="main-content">
<div class="post-content">
<div class="post-content-top flex">
<div class="post-sharing flex flex-dir-col mr-1 text-center">
<i class="bi bi-facebook"></i>
<i class="bi bi-twitter"></i>
<i class="bi bi-reddit"></i>
<i class="bi bi-whatsapp"></i>
</div>
<div class="post-featured-image relative">
<img src="https://cdn.pixabay.com/photo/2022/06/14/17/30/beach-7262493_1280.jpg" class="img-fluid">
<div class="post-date absolute flex flex-dir-col text-center">
<span class="day">23</span>
<span class="month">JUL</span>
</div>
</div>
</div>
</div>
</div>
<div class="sidebar">
This is the sidebar
</div>
</div>
</section>
So this should be a blog like layout with a main content and a sidebar on the right. My problem is that the image is overflowing its div and it flows underneath the sidebar. I added a slightly transparent background color to the sidebar so it's more visible.
I tried to add max-width: 100% and height: auto; to the image as recommended here, but it didn't work. I also tried to add overflow: hidden to the parent div of the image, but no luck. But I wouldn't want to clip the image anyway.
I don't understand because max-width: 100% and height: auto; should make the image responsive, right?
Any idea what I'm doing wrong? Thanks
There are 2 ways to achieve this:
You can cut the image which is being overflowed.
overflow-x: hidden;
You can either give detailed stylings, like this
*{
margin: 0;
padding:0;
}
img{
width: 100%;
height: 100%;
}
.image_container{
max-width: 100vw;
max-height: 100vw;
}
<div class="image_container">
<img src="https://images-wixmp-ed30a86b8c4ca887773594c2.wixmp.com/f/80a9d98d-327f-4bb2-b173-4298d710e51c/derkflv-9f975f3d-791f-4e16-8d9d-fb0a9e5e0554.png?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ1cm46YXBwOjdlMGQxODg5ODIyNjQzNzNhNWYwZDQxNWVhMGQyNmUwIiwiaXNzIjoidXJuOmFwcDo3ZTBkMTg4OTgyMjY0MzczYTVmMGQ0MTVlYTBkMjZlMCIsIm9iaiI6W1t7InBhdGgiOiJcL2ZcLzgwYTlkOThkLTMyN2YtNGJiMi1iMTczLTQyOThkNzEwZTUxY1wvZGVya2Zsdi05Zjk3NWYzZC03OTFmLTRlMTYtOGQ5ZC1mYjBhOWU1ZTA1NTQucG5nIn1dXSwiYXVkIjpbInVybjpzZXJ2aWNlOmZpbGUuZG93bmxvYWQiXX0.eEDVAlJGBqXo6OeZEORXWk1veGSHFL-ZTUMz43Jtr3Q" alt="image">
<div>
I have a container with 3 elements that together share a width of 100% (25%,50%,25%). If the container has less than 800px the order of the elements should be changed. #box2 has a width of 100%. #box1 and #box3 should both have a width of 50% and be on the same column.
So as a final result I should have one column with #box2 at 100% and a second column with #box1 and #box2 at 50%. How do I get #box1 and #box3 to be in the same column in the code below?
JsFiddle (link)
#container {
display: flex;
flex-direction: row;
max-width: 100vw;
width: 100%;
}
#box1 {
background: yellow;
width: 25%;
}
#box2 {
background: orange;
width: 50%;
}
#box3 {
background: red;
width: 25%;
}
/* Large Ansicht */
#media only screen and (max-width: 800px) {
#container {
flex-direction: column;
}
#box1 {
background: yellow;
width: 50%;
order: 2;
}
#box2 {
background: orange;
width: 100%;
order: 1;
}
#box3 {
background: red;
width: 50%;
order: 3;
}
}
<div id="container">
<div id="box1">sidemenu</div>
<div id="box2">app</div>
<div id="box3">sidemenu 2</div>
</div>
You could replace flex-direction: column; with flex-wrap: wrap; :
#container {
display: flex;
flex-direction: row;
max-width: 100vw;
width: 100%;
}
#box1 {
background: yellow;
width: 25%;
}
#box2 {
background: orange;
width: 50%;
}
#box3 {
background: red;
width: 25%;
}
/* Large Ansicht */
#media only screen and (max-width: 800px) {
#container {
flex-wrap: wrap;
}
#box1 {
background: yellow;
width: 50%;
order: 2;
}
#box2 {
background: orange;
width: 100%;
order: 1;
}
#box3 {
background: red;
width: 50%;
order: 3;
}
}
<div id="container">
<div id="box1">sidemenu</div>
<div id="box2">app</div>
<div id="box3">sidemenu 2</div>
</div>
The divs inside mini_projects_11 are all attached to each other, but I am providing margin-bottom. I've tried attaching margin to 'a' and also assigning 'a' display:block, but still isn't working.
style.scss
#mini_projects {
background-color: rgba(0,0,0,.85);
grid-column: 1/4;
grid-row: 1/7;
height: 100vh;
width: 100vw;
&1{
display: grid;
height: 100vh;
width: 100vw;
grid-template-columns: 20% 60% 20%;
grid-template-rows: 20% 60% 20%;
background-color: $mainDOrange;
clip-path: polygon(20% 0%, 80% 0%, 100% 100%, 0% 100%);
&1{
grid-column: 2/3;
grid-row: 2/3;
overflow-x: hidden;
overflow-y: scroll;
display: grid;
grid-template-columns: 50% 50%;
grid-template-rows: 40% 40%;
grid-auto-rows: 40%;
div{
justify-self: center;
height: 100%;
width: 90%;
margin-bottom:5rem; (NOT WORKING)
text-align: center;
a{
text-decoration: none;
display: block;
img{
width: 100%;
height: 100%;
border-radius: 50%;
}
span{
z-index: 1;
display: block;
color: white;
font-size: 2.5rem;
transform: translate(0rem,5rem);
}
}
}
}
}
}
#mini_projects {
background-color: rgba(0, 0, 0, .85);
grid-column: 0.25;
grid-row: 0.1428571429;
height: 100vh;
width: 100vw;
}
#mini_projects1 {
display: grid;
height: 100vh;
width: 100vw;
grid-template-columns: 20% 60% 20%;
grid-template-rows: 20% 60% 20%;
background-color: orange;
clip-path: polygon(20% 0%, 80% 0%, 100% 100%, 0% 100%);
}
#mini_projects11 {
grid-column: 0.6666666667;
grid-row: 0.6666666667;
overflow-x: hidden;
overflow-y: scroll;
display: grid;
grid-template-columns: 50% 50%;
grid-template-rows: 40% 40%;
grid-auto-rows: 40%;
}
#mini_projects11 div {
justify-self: center;
height: 100%;
width: 90%;
margin-bottom: 5rem;
/* (NOT WORKING) */
text-align: center;
}
#mini_projects11 div a {
text-decoration: none;
display: block;
}
#mini_projects11 div a img {
width: 100%;
height: 100%;
border-radius: 50%;
}
#mini_projects11 div a span {
z-index: 1;
display: block;
color: white;
font-size: 2.5rem;
transform: translate(0rem, 5rem);
}
<div id="mini_projects">
<div id="mini_projects1">
<div id="mini_projects11">
<div id="omnifood"><span>Omnifood</span><img src="img/omnifood.png" alt=""></div>
<div id="natours"><span>Natours</span><img src="img/natours.png" alt=""></div>
<div id="nexter"><span>Nexter</span><img src="img/nexter.png" alt=""></div>
<div id="trillo"><span>Trillo</span><img src="img/trillo.png" alt=""></div>
</div>
</div>
nevermind, I just fixed it with row-gap in #main_projects_11 .
row-gap:2rem;
I'm trying to achieve the following responsive grid-layout using flexbox, but i can't manage to get the "desktop"-version right without a fixed height. I want #1 to be on its own to the left and #2 and #3 stacked on each other to the right (#1 does not have to have the same height as the right col)
HTML (simplified)
<div class="wrapper">
<div class="inner">
<div class="col1"></div>
<div class="col2"></div>
<div class="col3"></div>
</div>
</div>
CSS (simplified)
.wrapper {
max-width: 600px;
margin: 0 auto;
border: 1px solid #c4c4c4
}
.col1, .col2, .col3 {
background: #fe4c4c;
position: relative;
margin: 10px;
padding: 5px;
color: #FFF;
font-weight: bold;
font-size: 40px;
font-family: sans-serif;
min-height: 100px;
&:after {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
}
.col1 {
&:after {
content: '1';
}
width: 100px;
}
.col2 {
&:after {
content: '2';
}
width: calc(100% - (100px + 60px))
}
.col3 {
&:after {
content: '3';
}
width: calc(100% - 30px);
}
.inner {
width: 100%;
display: flex;
flex-wrap: wrap;
}
#media only screen and (min-width: 768px) {
.inner {
flex-direction: column;
}
.col1 {
width: 300px;
}
.col2 {
width: auto;
}
.col3 {
width: auto;
}
}
jsFiddle
https://jsfiddle.net/pcwudrmc/28053/
I've search around for similar problems but can't find an answer without fixed height.
Thanks in advance!
Solution using CSS grid:
https://codepen.io/seanstopnik/pen/bd511c728bb79d02d11ae04edbfe9a33
* {
box-sizing: border-box;
}
.wrapper {
max-width: 600px;
margin: 0 auto;
border: 1px solid #c4c4c4;
padding: 10px;
}
.col1,
.col2,
.col3 {
background: #fe4c4c;
position: relative;
color: #fff;
font: {
family: sans-serif;
size: 40px;
weight: 700;
}
min-height: 100px;
&:after {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
}
.col1:after {
content: '1';
}
.col2:after {
content: '2';
}
.col3:after {
content: '3';
}
// Inner
.inner {
display: grid;
grid: {
template-columns: 100px auto;
template-rows: 100px;
gap: 10px 10px;
}
#media (min-width: 768px) {
grid-template-columns: 300px auto;
}
}
.col1 {
#media (min-width: 768px) {
grid: {
column: 1 / span 1;
row: 1 / span 2;
}
}
}
.col3 {
grid: {
column: 1 / span 2;
row: 2 / span 2;
}
#media (min-width: 768px) {
grid: {
column: auto;
row: auto;
}
}
}
It can be done with Flexbox.
Based on the answer of Nenad Vracar to this question Flexbox/Float - 2 1 2 Layout I did this:
body,
html,
ul {
margin: 0;
padding: 0;
}
* {
box-sizing: border-box;
}
ul {
display: flex;
height: 100vh;
list-style-type: none;
flex-direction: column;
flex-wrap: wrap;
}
li {
flex: 0 0 50%;
border: 1px solid black;
display: flex;
align-items: center;
justify-content: center;
font-size: 50px;
}
li:nth-child(1) {
flex: 0 0 100%;
}
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
It gives you one item to the left with 100% height and two stacked one over the other on the right with 50% height each. This is the result: