I've been struggling to arrange this layout of images.
I've been thinking of using flexbox and i'm pretty sure it's doable with it but I can't manage to find the right way to do it.
If anyone is able to help me i'll be glad.
Here is the layout with each square being an img in a link tag :
img layout
The space between each img must be the same, that's why I've been thinking of using flexbox.
Thanks in advance,
j
Edit: I uploaded what I've been working on :
http://163.172.185.65/flexboxuse.html
Flexbox example with percentage:
* {
box-sizing: border-box;
padding: 0;
margin: 0;
}
html,
body {
width: 100%;
height: 100%;
}
.container {
height: 100%;
width: 100%;
display: flex;
flex-flow: row wrap;
}
.sidebar {
background: limegreen;
height: 70%;
width: 30%;
}
.rightCont {
height: 70%;
width: 70%;
display: flex;
flex-flow: row wrap;
}
.red {
background: red;
height: calc(50% - 10px);
width: calc(50% - 10px);
margin-left: 10px;
margin-bottom: 10px;
}
.blue {
background: blue;
height: calc(50% - 10px);
width: calc(50% - 10px);
margin-left: 10px;
margin-bottom: 10px;
}
.yellow {
background: yellow;
height: 50%;
width: calc(50% - 10px);
margin-left: 10px;
}
.sky {
background: cyan;
height: 50%;
width: calc(50% - 10px);
margin-left: 10px;
}
.bottom {
background: violet;
height: calc(30% - 10px);
width: 100%;
margin-top: 10px;
}
<div class="container">
<div class="sidebar"></div>
<div class="rightCont">
<div class="red"></div>
<div class="blue"></div>
<div class="yellow"></div>
<div class="sky"></div>
</div>
<div class="bottom"></div>
</div>
You could approach this with flexbox, but even just float:left will do the trick.
Working Example:
section {
width: 312px;
}
div {
float: left;
width: 100px;
height: 100px;
margin: 0 6px 6px 0;
background-color: gray;
}
div:nth-of-type(1) {
height: 206px;
}
div:nth-of-type(6) {
width: 312px;
margin-bottom: 0;
}
div:nth-of-type(3),
div:nth-of-type(5),
div:nth-of-type(6) {
margin-right: 0;
}
div:nth-of-type(1) {
background-color: lime;
}
div:nth-of-type(2) {
background-color: red;
}
div:nth-of-type(3) {
background-color: blue;
}
div:nth-of-type(4) {
background-color: yellow;
}
div:nth-of-type(5) {
background-color: cyan;
}
div:nth-of-type(6) {
background-color: magenta;
}
<section>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</section>
Second approach (this time with element heights relative to the height of the viewport)
section {
width: calc(100vh + 12px);
}
div {
float: left;
width: 33vh;
height: 33vh;
margin: 0 6px 6px 0;
background-color: gray;
}
div:nth-of-type(1) {
height: calc(66vh + 6px);
}
div:nth-of-type(6) {
width: calc(100vh + 12px);
margin-bottom: 0;
}
div:nth-of-type(3),
div:nth-of-type(5),
div:nth-of-type(6) {
margin-right: 0;
}
div:nth-of-type(1) {
background-color: lime;
}
div:nth-of-type(2) {
background-color: red;
}
div:nth-of-type(3) {
background-color: blue;
}
div:nth-of-type(4) {
background-color: yellow;
}
div:nth-of-type(5) {
background-color: cyan;
}
div:nth-of-type(6) {
background-color: magenta;
}
<section>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</section>
Here is a working example using flex and percentage width:
html,body{
border:0;
margin:0;
height:100%;
}
#wrap
{
width:300px;
display: flex;
flex-direction: column;
justify-content: space-between;
height: 100%;
}
#container
{
display:flex;
flex-direction:row;
flex-wrap:wrap;
justify-content:space-between;
height:70%;
}
#green
{
background-color:chartreuse;
width:30%;
order:0;
}
#red
{
background-color:red;
height:48%;
}
#blue
{
background-color:blue;
height:48%;
}
#yellow
{
background-color:yellow;
height:49%;
}
#aquamarine
{
background-color:aqua;
height:49%;
}
#purple
{
background-color:purple;
height:28%;
}
.col
{
width:32%;
display:flex;
flex-direction:column;
justify-content:space-between;
}
<div id="wrap">
<div id="container">
<div id="green"></div>
<div class="col">
<div id="red"></div>
<div id="yellow"></div>
</div>
<div class="col">
<div id="blue"></div>
<div id="aquamarine"></div>
</div>
</div>
<div id="purple"></div>
</div>
Related
Imagine a code like this:
.div {
width: 100%;
height: 100px;
background-color: green;
}
.div1 {
width: 100px;
height: 100px;
background-color: red;
}
.div2 {
width: 100px;
height: 100px;
background-color: blue;
}
.main {
background-color: yellow;
}
<div class="main">
<div>
<div class="div"></div>
<div class="div1"></div>
</div>
<div class="div2"></div>
</div>
It will render something like this:
I want that the blue div comes up and stay on the right of the red div. Imagine that I can´t change the divs from where they are, so I need to do it in css. How can I do it?
Without changing the markup, if you set float: left to the red <div> then you could put the blue <div> to its right side
.div {
width: 100%;
height: 100px;
background-color: green;
}
.div1 {
width: 100px;
height: 100px;
background-color: red;
float: left;
}
.div2 {
width: 100px;
height: 100px;
background-color: blue;
display: inline-block;
vertical-align: top;
}
.main {
background-color: yellow;
}
<div class="main">
<div>
<div class="div"></div>
<div class="div1"></div>
</div>
<div class="div2"></div>
</div>
The previous solution which uses float on the red div works well, but here is another possible solution:
Apply position: relative; to the blue div (to be able to move it in relation to its default position) and add top: -100px; left: 100px; to move it up next to the red div:
.div {
width: 100%;
height: 100px;
background-color: green;
}
.div1 {
width: 100px;
height: 100px;
background-color: red;
}
.div2 {
width: 100px;
height: 100px;
background-color: blue;
position: relative;
top: -100px;
left: 100px;
}
.main {
background-color: yellow;
}
<div class="main">
<div>
<div class="div"></div>
<div class="div1"></div>
</div>
<div class="div2"></div>
</div>
This can also be done with the grid CSS. Here I used a named template box and then in the "chatty verbose" CSS I put the positional related for each "block". I added classes to the CSS just for clarity but you could update to your classes.
I added some color and things just for clarity and visual references but kept the "position relate" in separate CSS chunks.
.main {
font-size: 2rem;
display: grid;
grid-template: "box";
background-color: yellow;
}
.main *,
.main::before {
grid-area: box;
}
.green-block {
place-self: start;
}
.red-block {
width: 50%;
place-self: end start;
}
.blue-block {
width: 50%;
place-self: end end;
}
.green-block {
height: 3rem;
background-color: green;
}
.red-block {
height: 3rem;
background-color: red;
}
.blue-block {
background-color: blue;
}
.blue-block,
.green-block,
.red-block {
/* color for clarity and just to super center the text in the blocks */
display: grid;
color: cyan;
font-family: sans-serif;
text-transform: uppercase;
place-items: center;
}
<div class="main">
<div>
<div class="div green-block">green</div>
<div class="div1 red-block">red</div>
</div>
<div class="div2 blue-block">blue</div>
</div>
How can I place the blue box next to red box at the top. The yellow box should be below the blue box.
I cannot change the HTML structure. And also I cannot use margin-top for the blue box because the height of the boxes will change dynamically.
Please help me! Thanks!
Here is my code:
.wrapper {
width: 80%;
margin: 0 auto;
padding: 0;
border: 0;
font-size: 100%;
vertical-align: baseline;
}
.item {
float: left;
padding: 0 8px;
box-sizing: border-box;
}
.item1 {
width: 60%;
height: 400px;
background-color: red;
}
.item2 {
width: 60%;
height: 100px;
background-color: green;
}
.item3 {
width: 30%;
height: 200px;
background-color: blue;
}
.item4 {
width: 30%;
height: 500px;
background-color: yellow;
}
<div class="wrapper">
<div class="item item1"></div>
<div class="item item2"></div>
<div class="item item3"></div>
<div class="item item4"></div>
</div>
there is a typo error in your CSS file.
in your second item selector, you wrote itme2, and here is the problem.
❌.itme2 {...}
✅.item2 {...}
the logic is fine! is just a typo
.wrapper {
width: 80%;
margin: 0 auto;
padding: 0;
border: 0;
font-size: 100%;
vertical-align: baseline;
}
.item {
float: left;
padding: 0 8px;
box-sizing: border-box;
}
.item1 {
width: 60%;
height: 400px;
background-color: red;
}
.item2 {
width: 60%;
height: 100px;
background-color: green;
}
.item3 {
width: 30%;
height: 200px;
background-color: blue;
}
.item4 {
width: 30%;
height: 500px;
background-color: yellow;
}
<div class="wrapper">
<div class="item item1"></div>
<div class="item itme2"></div>
<div class="item item3"></div>
<div class="item item4"></div>
</div>
I am building a carousel and I want the container to have overflow-x hidden and overflow-y visible.When the items are hovered
a transform: scale() is applied and I want them to overflow from the container but only on the y axis.
I tried to set the container to overflow-x: hidden and the child to overflow-y: visible but that didn't work.
:root {
--item-margin: 5px;
}
body {
display: flex;
justify-content: center;
align-items: center;
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.container {
position: relative;
width: 80vw;
margin-top: 60px;
overflow-x: hidden;
}
.wrapper {
height: 90%;
display: flex;
position: relative;
left: calc(-20% - 5px);
transition: .5s ease all;
overflow-y: visible;
}
.left-arrow,
.right-arrow {
display: flex;
cursor: pointer;
justify-content: center;
align-items: center;
font-size: 2rem;
color: white;
width: 50px;
height: 50px;
background: #ED4956;
border-radius: 50%;
transform: translate(-50%, -50%);
position: absolute;
}
.left-arrow {
left: 2%;
top: 50%;
}
.right-arrow {
left: 98%;
top: 50%;
}
.child {
display: inline-flex;
justify-content: center;
align-items: center;
flex-shrink: 0;
height: 250px;
width: 20%;
font-weight: bold;
font-size: 4rem;
transition: .4s ease transform;
}
.child {
margin-right: var(--item-margin);
}
.child:hover {
transform: scale(1.5);
}
.child1 {
background: green;
}
.child2 {
background: blue;
}
.child3 {
background: yellow;
}
.child4 {
background: navy;
}
.child5 {
background: red;
}
.child6 {
background: pink;
}
.child7 {
background: grey;
}
.child8 {
background: brown;
}
.child9 {
background: #ff80ed;
}
#media (max-width: 1400px) {
.child {
width: calc(25% - var(--item-margin));
}
.wrapper {
left: -25%;
}
}
<div class="container">
<div class="wrapper">
<div class="child child1">1</div>
<div class="child child2">2</div>
<div class="child child3">3</div>
<div class="child child4">4</div>
<div class="child child5">5</div>
<div class="child child6">6</div>
<div class="child child7">7</div>
<div class="child child8">8</div>
<div class="child child9">9</div>
</div>
<div class="left-arrow"> ← </div>
<div class="right-arrow"> → </div>
</div>
You just have to give the .container a height, for example: 100vh.
Because the tops of the children are cut off i recommend to use padding-top: 60px instead of margin-top: 60px.
Because of the changed height the arrows are wrong positioned. To adjust this i used a different top value: half of the childrens height plus the containers padding-top (125px + 60px).
Because the .wrapper doesn't need position: relative in this example i removed it. Maybe in your project it is necessary for the arrow function(s)...
Working example: (watch it in "Full page" mode to see it without scrollbars)
:root {
--item-margin: 5px;
}
body {
display: flex;
justify-content: center;
align-items: center;
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.container {
position: relative;
width: 80vw;
height: 100vh;
padding-top: 60px;
overflow-x: hidden;
}
.wrapper {
height: 90%;
display: flex;
left: calc(-20% - 5px);
transition: .5s ease all;
overflow-y: visible;
}
.left-arrow,
.right-arrow {
display: flex;
cursor: pointer;
justify-content: center;
align-items: center;
font-size: 2rem;
color: white;
width: 50px;
height: 50px;
background: #ED4956;
border-radius: 50%;
transform: translate(-50%, -50%);
position: absolute;
top: 185px;
}
.left-arrow {
left: 2%;
}
.right-arrow {
left: 98%;
}
.child {
display: inline-flex;
justify-content: center;
align-items: center;
flex-shrink: 0;
height: 250px;
width: 20%;
font-weight: bold;
font-size: 4rem;
transition: .4s ease transform;
}
.child {
margin-right: var(--item-margin);
}
.child:hover {
transform: scale(1.5);
}
.child1 {
background: green;
}
.child2 {
background: blue;
}
.child3 {
background: yellow;
}
.child4 {
background: navy;
}
.child5 {
background: red;
}
.child6 {
background: pink;
}
.child7 {
background: grey;
}
.child8 {
background: brown;
}
.child9 {
background: #ff80ed;
}
#media (max-width: 1400px) {
.child {
width: calc(25% - var(--item-margin));
}
.wrapper {
left: -25%;
}
}
<div class="container">
<div class="wrapper">
<div class="child child1">1</div>
<div class="child child2">2</div>
<div class="child child3">3</div>
<div class="child child4">4</div>
<div class="child child5">5</div>
<div class="child child6">6</div>
<div class="child child7">7</div>
<div class="child child8">8</div>
<div class="child child9">9</div>
</div>
<div class="left-arrow"> ← </div>
<div class="right-arrow"> → </div>
</div>
Because the arrows are still cut of you could add an extra container (here .parent) and add height, padding-top and overflow-x to it (instead of to the container).
Working example: (watch in "Full page" mode to see it without scrollbars)
:root {
--item-margin: 5px;
}
body {
display: flex;
justify-content: center;
align-items: center;
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.container {
position: relative;
width: 80vw;
}
.parent {
height: 100vh;
padding-top: 60px;
overflow-x: hidden;
}
.wrapper {
height: 90%;
display: flex;
left: calc(-20% - 5px);
transition: .5s ease all;
overflow: visible;
}
.left-arrow,
.right-arrow {
display: flex;
cursor: pointer;
justify-content: center;
align-items: center;
font-size: 2rem;
color: white;
width: 50px;
height: 50px;
background: #ED4956;
border-radius: 50%;
transform: translate(-50%, -50%);
position: absolute;
top: 185px;
}
.left-arrow {
left: 2%;
}
.right-arrow {
left: 98%;
}
.child {
display: inline-flex;
justify-content: center;
align-items: center;
flex-shrink: 0;
height: 250px;
width: 20%;
font-weight: bold;
font-size: 4rem;
transition: .4s ease transform;
}
.child {
margin-right: var(--item-margin);
}
.child:hover {
transform: scale(1.5);
}
.child1 {
background: green;
}
.child2 {
background: blue;
}
.child3 {
background: yellow;
}
.child4 {
background: navy;
}
.child5 {
background: red;
}
.child6 {
background: pink;
}
.child7 {
background: grey;
}
.child8 {
background: brown;
}
.child9 {
background: #ff80ed;
}
#media (max-width: 1400px) {
.child {
width: calc(25% - var(--item-margin));
}
.wrapper {
left: -25%;
}
}
<div class="container">
<div class="parent">
<div class="wrapper">
<div class="child child1">1</div>
<div class="child child2">2</div>
<div class="child child3">3</div>
<div class="child child4">4</div>
<div class="child child5">5</div>
<div class="child child6">6</div>
<div class="child child7">7</div>
<div class="child child8">8</div>
<div class="child child9">9</div>
</div>
</div>
<div class="left-arrow"> ← </div>
<div class="right-arrow"> → </div>
</div>
I have a container div.
Inside it I have all the child divs each with float left property.
How do space the child div evenly across the row ?
The code pen is as follows:
https://codepen.io/pranavbhagwat81/pen/NWNgaVW
<div class='box'>
<div class='box1'></div>
<div class='box2'></div>
<div class='box3'></div>
</div>
CSS
.box{
}
.box1{
height:50px;
width:50px;
background-color:violet;
float:left;
}
.box2{
height:50px;
width:50px;
background-color:indigo;
float:left;
}
.box3{
height:50px;
width:50px;
background-color:green;float:left;
}
Use flex instead, with space-between (or space-evenly):
.box {
display: flex;
justify-content: space-between;
}
.box > * {
height: 50px;
width: 50px;
}
.box1 {
background-color: violet;
}
.box2 {
background-color: indigo;
}
.box3 {
background-color: green;
}
.box4 {
background-color: orange;
}
.box5 {
background-color: yellow;
}
.box6 {
background-color: red;
}
.box7 {
background-color: black;
}
.box8 {
background-color: pink;
}
<div class='box'>
<div class='box1'></div>
<div class='box2'></div>
<div class='box3'></div>
<div class='box4'></div>
<div class='box5'></div>
<div class='box6'></div>
<div class='box7'></div>
<div class='box8'></div>
</div>
If you couldn't use flex, you can calc to determine how much the margin should be:
.box > * {
height: 50px;
width: 50px;
float: left;
}
.box > *:not(:first-child) {
margin-left: calc((100vw - 50px * 8) / 8)
}
.box1 {
background-color: violet;
}
.box2 {
background-color: indigo;
}
.box3 {
background-color: green;
}
.box4 {
background-color: orange;
}
.box5 {
background-color: yellow;
}
.box6 {
background-color: red;
}
.box7 {
background-color: black;
}
.box8 {
background-color: pink;
}
<div class='box'>
<div class='box1'></div>
<div class='box2'></div>
<div class='box3'></div>
<div class='box4'></div>
<div class='box5'></div>
<div class='box6'></div>
<div class='box7'></div>
<div class='box8'></div>
</div>
http://codepen.io/anon/pen/OMLLwB
#news {
width: 85%;
margin: 0 auto;
}
#news ul {
list-style: none;
margin: 0;
padding: 0;
}
#worldMap img {
width: 100%;
}
.newspiece {
margin-bottom: 2.5%;
padding: 20px;
background-color: #90C3D4;
height: 130px;
}
.newspiece h3 {
border-bottom: 3px solid black;
margin-bottom: 10px;
}
#media(min-width: 600px) {
.newspiece {
width: 25%;
margin-left: 5%;
display: inline-block;
vertical-align: top;
overflow: hidden;
}
.newspiece:first-child {
margin-left:0;
}
}
Am i missing something here? the width the total container (#news) is 85%, the width of each item is 25%, and two of them have a 5% left margin, total sums to 85%, then why do i resize it, the rightmost column goes down?
i have changed your html/css. this is a cleaner solution and is suported among all browsers
html:
<div class="flex">
<div class="box">
<h3>Title</h3>
<p>Content</p>
</div>
<div class="box">
<h3>Title</h3>
<img src="http://www.placecage.com/400/300" alt="">
</div>
<div class="box">
<h3>Title</h3>
<p>Content</p>
</div>
</div>
css:
.flex {
display: flex;
justify-content: space-around;
flex-wrap: wrap;
}
.box {
width: 300px;
margin: 10px;
padding: 20px;
background: #90C3D4;
}
.box h3 {
padding-bottom: 5px;
border-bottom: 3px solid black;
}
.box img {
max-width: 100%;
}
* { box-sizing: border-box; }
The padding adds to the total width of the element if box-sizing: border-box is not used.