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>
Related
How am I going to turn
into
I tried to use justify-content: space between; and seperated the blocks, but how am I going to align 3 and 4 to the bottom of the container
CSS Code
.container {
width: 240px;
height: 200px;
border: 1px solid gray;
display: flex;
flex-wrap: wrap;
justify-content: space-between;
}
.container > div {
width: 80px;
height: 50px;
border: 1px solid red;
}
Assuming all these sizes are hard coded, you could do something like this.
* {
box-sizing: border-box;
}
.container {
width: 240px;
height: 200px;
border: 1px solid gray;
display: flex;
flex-wrap: wrap;
justify-content: space-between;
align-content: space-between;
}
.container > div {
width: 80px;
height: 50px;
border: 1px solid red;
flex-shrink: 0;
}
.container > div:nth-child(1),
.container > div:nth-child(2),
.container > div:nth-child(5) {
order: -1;
}
.container > div:nth-child(5) {
margin-inline: 80px;
}
<div class="container">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
</div>
If only the size of your inside boxes are fixed you can use this solution.
.parent {
display: flex;
justify-content: space-between;
width: 100%;
height: 80vh;
border: 1px solid black;
position: relative;
resize: both;
}
div {
display: inline-block;
width: 100px;
height: 30px;
border: 1px solid red;
}
.three {
position: absolute;
bottom: 0%;
}
.four {
position: absolute;
bottom: 0%;
right: 0%;
}
.five {
position: absolute;
bottom: 50%;
right: 50%;
transform: translate(50%, 50%);
}
<div class="parent">
<div class="one">1</div>
<div class="two">2</div>
<div class="three">3</div>
<div class="four">4</div>
<div class="five">5</div>
</div>
This is how I want the page to split, so that I can have a fluid layout.
<div class="container">
<div class="top">Top</div>
<div class="image">image</div>
<div class="footer">footer</div>
</div>
.container{
height: 100vh;
width: 100vw;
display: flex;
flex-direction: column;
}
.top {
flex: 25%;
width: 100%;
background-color: red;
}
.image {
flex: 50%;
width: 100%;
background-color: green;
}
.footer{
flex: 25%;
width: 100%;
background-color:blue;
}
https://jsfiddle.net/9n7zmr60/
In the image section, I wanna insert an image and I want it to fit in the same ratio (25% 50% 25%) as above. However, when I try, its getting stretched like below:
.container{
height: 100vh;
width: 100vw;
display: flex;
flex-direction: column;
}
.top {
flex: 25%;
width: 100%;
background-color: red;
}
.image {
flex: 50%;
width: 100%;
background-color: green;
max-width: 100%;
object-fit: contain;
}
.footer{
flex: 25%;
width: 100%;
background-color:blue;
}
https://jsfiddle.net/qcnwu7fe/
Please help
Do this changes:
.image {
flex: 50%; /*delete this*/
height: 50%;
width: 100%;
background-color: green;
}
img{
height: 100%;
width: 100%;
}
.container {
height: 100vh;
width: 100vw;
display: flex;
flex-direction: column;
}
.top {
flex: 25%;
width: 100%;
background-color: red;
}
img {
height: 100%;
width: 100%;
}
.image {
width: 100%;
background-color: green;
height: 50%;
}
.footer {
flex: 25%;
width: 100%;
background-color: blue;
}
<div class="container">
<div class="top">Top</div>
<div class="image"><img src="https://static1.momsimage.com/wordpress/wp-content/uploads/2019/08/Kids-cartoons.jpg" alt=""></div>
<div class="footer">footer</div>
</div>
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:
I am trying to make a content slider with a chatbox to the side and a footer stuck to the bottom.
Here is a diagram of what I am trying to achieve:
The problem with below code is that the chatbox is the height of the page. I want the chat box to stop at the footer so that it is the height of the page -60px.
And here is what I have so far:
body {
margin: 0;
}
.wrapper {
background: #95a5a6;
display: table;
height: 100vh;
width: 100%;
}
.wrapper-inner {
display: table-cell;
padding-left: 300px;
padding-bottom: 60px;
vertical-align: middle;
text-align: center;
}
.chatbox {
background: #bdc3c7;
min-height: 100%;
position: absolute;
overflow-x: hidden;
overflow-y: auto;
width: 300px;
z-index: 2;
}
.footer {
background: #2c3e50;
bottom: 0px;
height: 60px;
position: absolute;
width: 100%;
z-index: 1;
}
<div class="wrapper">
<div class="chatbox"></div>
<div class="wrapper-inner">Content</div>
</div>
<div class="footer"></div>
https://jsfiddle.net/bjxsyve7/4/
Here's a simplified version using only flex and calc():
body {
display: flex;
flex-wrap: wrap;
margin: 0;
}
.chatbox {
flex: 0 0 300px;
height: calc(100vh - 60px);
overflow-y: auto;
background-color: #bdc3c7;
}
.content {
flex: 1;
background-color: #95a5a6;
}
.footer {
flex-basis: 100%;
height: 60px;
background: #2c3e50;
}
<div class="chatbox"></div>
<div class="content">Content</div>
<div class="footer"></div>
jsFiddle
You can use CSS calc() to achieve this. Add this min-height: calc(100% - 60px) to .chatbox. For more info about calc().
body {
margin: 0;
overflow-x:hidden;
}
.wrapper {
background: #95a5a6;
display: table;
height: 100vh;
width: 100%;
}
.wrapper-inner {
display: table-cell;
min-height: 100%;
padding-left:300px;
padding-bottom: 60px;
}
.chatbox {
background: #bdc3c7;
min-height: calc(100% - 60px);
position: absolute;
overflow-x: hidden;
overflow-y: auto;
top:0;
width: 300px;
z-index: 2;
}
.footer {
background: #2c3e50;
bottom: 0px;
height: 60px;
position: absolute;
width: 100%;
z-index: 1;
}
<div class="wrapper">
<div class="chatbox"></div>
<div class="wrapper-inner"></div>
</div>
<div class="footer"></div>
You need only adding this:
.wrapper{
position: relative;
}
In your code the chatbox div has height 100% of the body. But if you set position: relative; to it's parent(.wrapper) it will have height 100% of it's parent.
This is an easy way to do this with flex:
body {
display: flex;
flex-direction:column;
min-height: 100vh;
}
.wrapper {
background: #95a5a6;
display: flex;
flex: 1;
}
.chatbox {
background: #bdc3c7;
overflow-x: hidden;
overflow-y: auto;
width: 200px;
}
.footer {
background: #2c3e50;
height: 60px;
}
<div class="wrapper">
<div class="chatbox"></div>
<div class="wrapper-inner">Content</div>
</div>
<div class="footer"></div>
I am working on a page redesign that contains 3 divs and I want to make it responsive.
The problem I face is that the divs for large screen are arranged in the order 1,2,3. For responsive design however, I want to change the order to 1,3,2:
I tried different approaches like changing position to relative/absolute/static or changing the divs order with alternative CSS code but nothing proved to work so far.
Any ideas how I can achieve this?
.one {
float: left;
width: 150px;
border: solid 2px #eaeaea;
position: relative;
z-index: 0;
margin-bottom: 20px;
height: 100px;
}
.two {
float: left;
margin: 0 0 0 24px;
width: 150px;
border: solid 2px #eaeaea;
height: 100px;
}
.three {
float: left;
width: 900px;
border: solid 2px #eaeaea;
height: 100px;
}
#media only screen and (max-width: 500px) {
.one {
width: 93%;
padding: 3%;
}
.two {
width: 100%;
margin-left: 0px;
}
.three {
width: 100%;
margin: 0px;
}
}
<div class="one">Content1</div>
<div class="two">Content2</div>
<div class="three">Content3</div>
<div class="500markup">This box is 500px</div>
JSFIDDLE HERE
https://jsfiddle.net/fehrda1c/4/
<div class="container">
<div id="one">Content1</div><!--
!--><div id="three">Content3</div>
<div id="two">Content2</div>
</div>
.container {
padding: 5px;
position: relative;
}
#one, #two {
width: 50%;
display: inline-block;
box-sizing: border-box;
}
#two {
position: absolute;
top: 0;
right: 0;
}
#one {
position: absolute;
top: 0;
left: 0;
}
#three {
position: absolute;
top: 200px;
left: 0;
box-sizing: border-box;
}
#one, #two, #three {
margin: 0;
height: 200px;
border: 1px solid black;
}
#three {
width: 100%;
}
#media (max-width: 600px) {
#one, #two, #three {
width: 100%;
position: initial;
top: default;
}
}
This can be achieved using flexbox:
Contain the divs in a #container set to display: flex; this will tell the child divs to use the flexbox model
Add flex: 1; to .one and .two to tell them to grow if required
Add flex-basis: 100%; to .three to ensure it takes up the full width of the container
Add order: *n*; to .one, .two and .three to give them the desired order when they adapt to the smaller screen size
#container {
display: flex;
flex-wrap: wrap;
width: 100%;
}
.one {
border: solid 2px #eaeaea;
flex: 1;
height: 100px;
margin-bottom: 20px;
}
.two {
border: solid 2px #eaeaea;
flex: 1;
height: 100px;
margin-bottom: 20px;
}
.three {
border: solid 2px #eaeaea;
flex-basis: 100%;
height: 100px;
margin-bottom: 20px;
}
#media only screen and (max-width: 500px) {
.one {
flex-basis: 100%;
order: 1;
}
.two {
flex-basis: 100%;
order: 3;
}
.three {
flex-basis: 100%;
order: 2;
}
}
<div id="container">
<div class="one">Content1</div>
<div class="two">Content2</div>
<div class="three">Content3</div>
</div>
Flexbox can do this.
JSfiddle Demo
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
.container div {
height: 150px;
border: 1px solid red;
margin-bottom: 10px;
}
.container {
padding: 5px;
position: relative;
display: flex;
flex-wrap: wrap;
justify-content: space-between;
width: 500px;
border: 1px solid grey;
}
#one,
#two {
width: 220px;
}
#three {
width: 500px;
}
#media (max-width: 600px) {
#one {
order: 1;
width: 500px;
}
#two {
order: 3;
width: 500px;
}
#three {
order: 2;
}
<div class="container">
<div id="one">Content1</div>
<div id="two">Content2</div>
<div id="three">Content3</div>
</div>
You can do like following:
#media only screen and (max-width:500px)
{
.one{width: 93%; padding: 3%;}
.two{width: 100%; margin-left: 0px; margin-bottom: 10px; position:absolute; top:320px;}
.three{width: 100%; margin: 0px;}
}
Check Fiddle Here.