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>
Related
I want to have the ability to move the .secDiv down when resizing the browser. Currently the coloured squares in the .boxes overlap the .secDiv when scaling the browser down.
Please assist.
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
.boxes {
position: relative;
width: 100%;
height: 300px;
border: 2px solid pink;
display: flex;
flex-wrap: wrap;
justify-content: space-around;
align-items: center;
}
.red {
width: 300px;
background-color: red;
height: 150px;
margin: 15px;
}
.green {
width: 300px;
background-color: green;
height: 150px;
margin: 15px;
}
.blue {
width: 300px;
background-color: blue;
height: 150px;
margin: 15px;
}
.secDiv {
position: relative;
width: 100%;
height: 300px;
border: 2px solid yellow;
display: flex;
flex-wrap: wrap;
justify-content: space-around;
align-items: center;
}
.red2 {
width: 300px;
background-color: red;
height: 150px;
margin: 15px;
}
.green2 {
width: 300px;
background-color: green;
height: 150px;
margin: 15px;
}
.blue2 {
width: 300px;
background-color: blue;
height: 150px;
margin: 15px;
}
<div class="boxes">
<div class="red"></div>
<div class="green"></div>
<div class="blue"></div>
</div>
<div class="secDiv">
<div class="red2"></div>
<div class="green2"></div>
<div class="blue2"></div>
</div>
I would suggest using dynamic heights such as % or vh. Because you have a fixed height of 300px. It will try to keep that height when resizing, and simply your content doesn't fit in a 300px height when you resize. You can use something simple like overflow-y: scroll if you want to use a fixed height, but I don't think that is what you're going for. I added width: 50% on your boxes and secDiv classes. You can use either 50%, 25% or whatever you desire for your end result. But I would stay away from using fixed units when looking for a responsive design.
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
.boxes {
position: relative;
width: 100%;
height: 50%;
border: 2px solid pink;
display: flex;
flex-wrap: wrap;
justify-content: space-around;
align-items: center;
}
.red {
width: 300px;
background-color: red;
height: 150px;
margin: 15px;
}
.green {
width: 300px;
background-color: green;
height: 150px;
margin: 15px;
}
.blue {
width: 300px;
background-color: blue;
height: 150px;
margin: 15px;
}
.secDiv {
position: relative;
width: 100%;
height: 50%;
border: 2px solid yellow;
display: flex;
flex-wrap: wrap;
justify-content: space-around;
align-items: center;
}
.red2 {
width: 300px;
background-color: red;
height: 150px;
margin: 15px;
}
.green2 {
width: 300px;
background-color: green;
height: 150px;
margin: 15px;
}
.blue2 {
width: 300px;
background-color: blue;
height: 150px;
margin: 15px;
}
<div class="boxes">
<div class="red"></div>
<div class="green"></div>
<div class="blue"></div>
</div>
<div class="secDiv">
<div class="red2"></div>
<div class="green2"></div>
<div class="blue2"></div>
</div>
I've added the #media query, so it changes responsively when the browser resizes, and the <div>'s break nicely under each other.
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
height: 100vh;
}
.boxes {
position: relative;
width: 100%;
height: 300px;
border: 2px solid pink;
display: flex;
flex-wrap: wrap;
justify-content: space-around;
align-items: center;
}
.red {
width: 300px;
background-color: red;
height: 150px;
margin: 15px;
}
.green {
width: 300px;
background-color: green;
height: 150px;
margin: 15px;
}
.blue {
width: 300px;
background-color: blue;
height: 150px;
margin: 15px;
}
.secDiv {
position: relative;
width: 100%;
height: 300px;
border: 2px solid yellow;
display: flex;
flex-wrap: wrap;
justify-content: space-around;
align-items: center;
}
.red2 {
width: 300px;
background-color: red;
height: 150px;
margin: 15px;
}
.green2 {
width: 300px;
background-color: green;
height: 150px;
margin: 15px;
}
.blue2 {
width: 300px;
background-color: blue;
height: 150px;
margin: 15px;
}
#media(max-width: 994px) {
.secDiv, .boxes {
height: 600px;
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<title>Document</title>
</head>
<body>
<div class="boxes">
<div class="red"></div>
<div class="green"></div>
<div class="blue"></div>
</div>
<div class="secDiv">
<div class="red2"></div>
<div class="green2"></div>
<div class="blue2"></div>
</div>
</body>
</html>
I am trying to build a layout with a menubar and a main container that includes a searchbar, left sidebar, and a results table.
I want the main container to always be as tall as possible for the window and the left sidebar and results table to also be as tall as possible within the main container.
This is how this would look with fixed heights on everything:
https://jsfiddle.net/m45cakne/1/
<div class="menubar"></div>
<div class="main-section">
<div class="searchbar">
</div>
<div class="section-content">
<div class="sidebar"></div>
<div class="results-table"></div>
</div>
</div>
* {
box-sizing: border-box;
}
html, body {
height: 100%;
}
.menubar {
height: 50px;
border: 1px solid black;
}
.main-section {
border: 1px solid black;
margin-top: 20px;
height: 600px;
}
.searchbar {
border: 1px solid black;
margin: 20px;
height: 50px;
}
.section-content {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-ms-flex-wrap: wrap;
flex-wrap: wrap;
padding-right: 25px;
padding-left: 25px;
flex: 1;
}
.sidebar {
-webkit-box-flex: 0;
-ms-flex: 0 0 25%;
flex: 0 0 25%;
max-width: 25%;
border: 1px solid black;
position: relative;
width: 100%;
min-height: 1px;
padding-right: 15px;
padding-left: 15px;
height: 490px;
}
.results-table {
-webkit-box-flex: 0;
-ms-flex: 0 0 75%;
flex: 0 0 75%;
max-width: 75%;
position: relative;
width: 100%;
min-height: 1px;
border: 1px solid black;
height: 490px;
padding: 0px;
}
The menubar height can change as the page is viewed on different devices, and the searchbar height can also change as it is filled with search terms.
What would be the right method to build this responsive layout with CSS?
Just use flex properties all the way through:
body {
display: flex;
flex-direction: column;
height: 100vh;
margin: 0;
}
.menubar {
flex: 0 0 50px;
border: 1px solid black;
}
.main-section {
flex: 1;
display: flex;
flex-direction: column;
border: 1px solid black;
margin-top: 20px;
padding: 25px;
}
.searchbar {
flex: 0 0 50px;
margin-bottom: 20px;
border: 1px solid black;
}
.section-content {
flex: 1;
display: flex;
flex-wrap: wrap;
}
.sidebar {
flex: 0 0 25%;
border: 1px solid black;
}
.results-table {
flex: 1;
border: 1px solid black;
}
* {
box-sizing: border-box;
}
<div class="menubar">menu bar</div>
<div class="main-section">main container
<div class="searchbar">search bar</div>
<div class="section-content">
<div class="sidebar">side bar</div>
<div class="results-table">results table</div>
</div>
</div>
jsFiddle demo
I have a block with several items. When I click on the menu item it is highlighting and extending to the left border of the window.
I did this with the help of an absolutely positioned element, and set the width to 1000px, but this option does not work. This red bar should rest against the edge of the window at any resolution.
html
<div class="flex-menu-area">
<div class="container">
<div class="flex-menu">
<div class="left-flex-column">
<div class="flex-menu-select"><span>item 1</span></div>
<div class="flex-menu-select"><span>item 2</span></div>
<div class="flex-menu-select"><span>item 3</span></div>
</div>
<div class="right-left-column">
<div class="object"><span>item1content</span></div>
<div class="object"><span>item1content</span></div>
<div class="object"><span>item1content</span></div>
<div class="object"><span>item1content</span></div>
<div class="object"><span>item1content</span></div>
</div>
</div>
</div>
</div>
css
.flex-menu-area {
background: #fff;
width: 100%;
height: 512px;
.flex-menu {
display: flex;
flex-direction: row;
flex-wrap: nowrap;
height: 100%;
.left-flex-column {
max-width: 256px;
width: 100%;
outline: 1px solid gray;
height: 512px;
.flex-menu-select {
font-size: 20px;
line-height: 26px;
font-weight: 600;
text-align: left;
color: $text-color;
padding-top: 43px;
padding-bottom: 43px;
max-width: 100%;
border-bottom: 1px solid gray;
position: relative;
&:hover {
background: #ccc;
&:before {
position: absolute;
width: 1000px;
height: 100%;
content: "";
display: block;
top: 0px;
left: -1000px;
background: red;
}
}
}
}
.right-left-column {
outline: 1px solid gray;
height: 512px;
width: 884px;
background: #fff;
display: -webkit-flex;
display: -moz-flex;
display: -ms-flex;
display: -o-flex;
display: flex;
flex-direction: row;
align-items: center;
justify-content: space-between;
background: #eee;
.object {
outline: 1px solid red;
font-size: 20px;
line-height: 40px;
}
}
}
}
Solution:
&:before {
position: absolute;
width: 100vw;
right: 0;
height: 100%;
content: "";
display: block;
top: 0px;
background: #fff;
z-index: -1;
}
Set the width and left values of your &:before to:
width: calc((100vw - 100%) /2);
left: calc(0 - ((100vw - 100%) /2));
I think that should help, but I don't have access to a screen large enough to test right now so apologies if my calcs are slightly off.
Is it possible without table tag or display: table?
https://monosnap.com/file/MoxMr7WehKJD4RyKWPTJ7Dyqg8dsez
<div class="wrapper">
<div class="title">Some title</div>
<div class="content">Content</div>
</div>
.wrapper {
border: 3px solid yellow;
width: 250px;
height: 350px;
position: fixed;
top: 10px;
left: 10px;
background: green;
}
.title {
min-height: 30px;
max-height: 80px;
background: blue;
}
.content {
background: red;
height: 100%;
}
http://jsfiddle.net/wqozs28y/
Ill try it with position absolute, but i donw know what will be the height on TITLE div :(
Yes, you can use flexbox depending on what level of browser support you want.
.wrapper {
border: 3px solid yellow;
width: 250px;
height: 350px;
position: fixed;
top: 10px;
left: 10px;
background: green;
display: flex;
flex-direction: column;
}
.title {
flex-grow: 0;
flex-shrink: 1;
flex-basis: auto;
min-height: 30px;
max-height: 80px;
background: blue;
}
.content {
background: red;
flex-grow: 1;
}
<div class="wrapper">
<div class="title">Some title</div>
<div class="content">Content</div>
</div>
JSFiddle Demo
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.