How to divide a webpage into four parts using flexbox? - html

How to divide a webpage into four equal parts using flex box? Something like this website- www.rileyjshaw.com
The columns should be able to stack upon one another on smaller view ports.
Edit/ Update:
So far I've tried this-
Should I change the line height? How can I achieve distinct blocks?
/* Grid */
.column {
flex-basis: 100%;
width: 50%;
height: 50%;
line-height: 200px;
}
#media screen and (min-width: 800px) {
.row {
display: flex;
flex-direction: row;
flex-wrap: nowrap;
}
.column {
flex: 1;
}
._25 {
flex: 2.5;
}
._5 {
flex: 5;
}
}
/* Style */
body {
font-family: 'Lato', sans-serif;
font-size: 1.3em;
color: #ccc;
background: #000;
/*margin-bottom: 70px;*/
}
.column {
/* padding: 15px;
border: 1px solid #666;
margin: 5px 0;*/
background: #343436;
}
main {
max-width: 1200px;
margin: 0 auto;
}
h1,
h2 {
text-align: center;
}
<div class="row">
<div class="column">
50%
</div>
<div class="column">
50%
</div>
</div>
<div class="row">
<div class="column">
50%
</div>
<div class="column">
50%
</div>
</div>

You can simplify the code and gain the wanted output. Here I removed the row's and used a container. The main benefit with this structure is that you can alter the order of the column's if you find that necessary.
I also choose to use flex-basis instead of flex-grow to make them stay 50% wide no matter their content size.
On wider screens, using the media query, set the column's 50% wide and the container to display: flex; flex-wrap: wrap;.
On narrower screens, and being block elements, they stack on top of each other by default
html, body {
height: 100%;
margin: 0;
}
.container {
height: 100%;
}
.column {
height: 25%;
}
#media screen and (min-width: 600px) {
.container {
display: flex;
flex-wrap: wrap;
}
.column {
flex-basis: 50%;
height: 50%;
}
}
/* general styles */
body {
font-family: 'Lato', sans-serif;
font-size: 1.3em;
color: #ccc;
background: #000;
/*margin-bottom: 70px;*/
}
.column {
padding: 15px;
/*border: 1px solid #666;*/
box-sizing: border-box;
}
.column:nth-child(1) {
background: #5c9;
}
.column:nth-child(2) {
background: #fb0;
}
.column:nth-child(3) {
background: #39f;
}
.column:nth-child(4) {
background: #f33;
}
main {
max-width: 1200px;
margin: 0 auto;
}
h1,
h2 {
text-align: center;
}
<div class="container">
<div class="column">
50%
</div>
<div class="column">
50%
</div>
<div class="column">
50%
</div>
<div class="column">
50%
</div>
</div>
If you still need the original markup structure, here is a sample with that too
html, body {
height: 100%;
margin: 0;
}
.row {
height: 50%;
}
.column {
height: 50%;
}
#media screen and (min-width: 600px) {
.row {
display: flex;
}
.column {
flex-basis: 50%;
height: 100%;
}
}
/* general styles */
body {
font-family: 'Lato', sans-serif;
font-size: 1.3em;
color: #ccc;
background: #000;
/*margin-bottom: 70px;*/
}
.column {
padding: 15px;
/*border: 1px solid #666;*/
box-sizing: border-box;
}
.row:nth-child(1) .column:nth-child(1) {
background: #5c9;
}
.row:nth-child(1) .column:nth-child(2) {
background: #fb0;
}
.row:nth-child(2) .column:nth-child(1) {
background: #39f;
}
.row:nth-child(2) .column:nth-child(2) {
background: #f33;
}
main {
max-width: 1200px;
margin: 0 auto;
}
h1,
h2 {
text-align: center;
}
<div class="row">
<div class="column">
50%
</div>
<div class="column">
50%
</div>
</div>
<div class="row">
<div class="column">
50%
</div>
<div class="column">
50%
</div>
</div>
Updated based on a comment
Centering the column's content can be done with i.e.:
Flexbox - https://jsfiddle.net/0ns6ofcp/
.column {
height: 25%;
display: flex; /* added */
justify-content: center; /* hor. center */
align-items: center; /* ver. center */
}
Transform - https://jsfiddle.net/0ns6ofcp/1/
<div class="column">
<div>50%</div>
</div>
.column {
position: relative; /* added property */
height: 25%;
}
.column > div { /* added rule */
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%,-50%);
}

Here is something you can start with:
/* Grid */
.column {
flex-basis: 100%;
height: 50vh;
}
.row {
display: flex;
}
.row:first-child .column:first-child {
background: red;
}
.row:first-child .column:last-child {
background: blue;
}
.row:last-child .column:first-child {
background: green;
}
.row:last-child .column:last-child {
background: black;
}
/* Style */
body {
font-family: 'Lato', sans-serif;
font-size: 1.3em;
color: #ccc;
background: #000;
}
.column {
background: #343436;
}
<div>
<div class="row">
<div class="column">
50%
</div>
<div class="column">
50%
</div>
</div>
<div class="row">
<div class="column">
50%
</div>
<div class="column">
50%
</div>
</div>
</div>

Related

How to make items stack on mobile view

I have made 3 images side by side using display flex in desktop screen but now i want to make them responsive and make the items stack in mobile mode. Can someone tell me how to do it?
Code:
.row {
display: flex;
}
.column {
padding: 200px;
text-align: center;
flex: 10%;
}
.column img {
border-radius: 50%;
width: 300px;
border: 2px solid white;
}
.column p {
color: white;
padding-top: 50px;
font-size: 24px;
}
.partners-h1 p {
font-family: "BatmanForeverAlternate";
font-size-adjust: 10px;
color: white;
}
.partners-h1 {
text-align: center;
font-size: 30px;
}
/* added for demonstration purpose by editor */
body {
background-color: black;
}
<div class="row">
<div class="column">
<img src="images/sneak.jpg">
<p>Sneak Energy</p>
</div>
<div class="column">
<img src="images/kontrol.png">
<p>Kontrol Freek</p>
</div>
<div class="column">
<img src="images/astro.jpg">
<p>Astro Gaming</p>
</div>
</div>
There are 2 ways I can think of, towards solving this.
Method 1: Using Media query
.row {
display: flex;
flex-direction:column; /* Initially, mobile first */
}
/* If minimum screen width is 320, switch to side by side. Replace 320 with whatever screen size you want to swap between horizontal and stacked view */
#media (min-width:320px)
{
.row {
flex-direction:row;
}
}
.column {
padding: 200px;
text-align: center;
flex: 10%;
}
.column img {
border-radius: 50%;
width: 300px;
border: 2px solid white;
}
.column p {
color: white;
padding-top: 50px;
font-size: 24px;
}
.partners-h1 p {
font-family: "BatmanForeverAlternate";
font-size-adjust: 10px;
color: white;
}
.partners-h1 {
text-align: center;
font-size: 30px;
}
/* added for demonstration purpose by editor */
body {
background-color: black;
}
<div class="row">
<div class="column">
<img src="images/sneak.jpg">
<p>Sneak Energy</p>
</div>
<div class="column">
<img src="images/kontrol.png">
<p>Kontrol Freek</p>
</div>
<div class="column">
<img src="images/astro.jpg">
<p>Astro Gaming</p>
</div>
</div>
Method 2: Using flex-wrap to automatically stack when 2 images don't fit side to side
.row {
display: flex;
flex-wrap:wrap;
}
.column {
padding: 200px;
text-align: center;
flex: 10%;
}
.column img {
border-radius: 50%;
width: 300px;
border: 2px solid white;
}
.column p {
color: white;
padding-top: 50px;
font-size: 24px;
}
.partners-h1 p {
font-family: "BatmanForeverAlternate";
font-size-adjust: 10px;
color: white;
}
.partners-h1 {
text-align: center;
font-size: 30px;
}
/* added for demonstration purpose by editor */
body {
background-color: black;
}
<div class="row">
<div class="column">
<img src="images/sneak.jpg">
<p>Sneak Energy</p>
</div>
<div class="column">
<img src="images/kontrol.png">
<p>Kontrol Freek</p>
</div>
<div class="column">
<img src="images/astro.jpg">
<p>Astro Gaming</p>
</div>
</div>
Add a media query to address screens up to a certain width like:
#media only screen
and (max-width: 800px) {
selector {
property: value;
}
}
then change the flex-direction property from the default row to column:
flex-direction: column;
Reference to media queries: MDN Web Docs
.row {
display: flex;
}
#media only screen
and (max-width: 800px) {
.row {
flex-direction: column;
}
}
.column {
padding: 200px;
text-align: center;
flex: 10%;
}
.column img {
border-radius: 50%;
width: 300px;
border: 2px solid white;
}
.column p {
color: white;
padding-top: 50px;
font-size: 24px;
}
.partners-h1 p {
font-family: "BatmanForeverAlternate";
font-size-adjust: 10px;
color: white;
}
.partners-h1 {
text-align: center;
font-size: 30px;
}
/* added for demonstration purpose by editor */
body {
background-color: black;
}
<div class="row">
<div class="column">
<img src="images/sneak.jpg">
<p>Sneak Energy</p>
</div>
<div class="column">
<img src="images/kontrol.png">
<p>Kontrol Freek</p>
</div>
<div class="column">
<img src="images/astro.jpg">
<p>Astro Gaming</p>
</div>
</div>

Add responsive image to a website split into 4 responsive boxes

I want to make a website that would consist of 4 boxes each 50% height and width. I found code that does that, but now I struggle to add images into each box. I want each of the four divs to have a different image, and they should scale according to window size. Any help is appreciated.
Here's my codepen: https://codepen.io/alanvkarlik/pen/OJRdyRR
Here's what I would like to achieve: https://i.imgur.com/7CR7sW8.jpg
HTML:
<div class="container">
<div class="column"><img src="https://f4.bcbits.com/img/a0846297992_16.jpg"></div>
<div class="column">IMG 2</div>
<div class="column">IMG 3</div>
<div class="column">IMG 4</div>
</div>
CSS:
html, body {
height: 100%;
margin: 0;
}
.container {
height: 100%;
}
.column {
height: 25%;
display: flex;
justify-content: center;
align-items: center;
}
#media screen and (min-width: 600px) {
.container {
display: flex;
flex-wrap: wrap;
}
.column {
flex-basis: 50%;
height: 50%;
}
}
/* general styles */
body {
font-family: 'Lato', sans-serif;
font-size: 1.3em;
color: #ccc;
background: #000;
/*margin-bottom: 70px;*/
}
.column {
padding: 15px;
/*border: 1px solid #666;*/
box-sizing: border-box;
}
.column:nth-child(1) {
background: #5c9;
}
.column:nth-child(2) {
background: #fb0;
}
.column:nth-child(3) {
background: #39f;
}
.column:nth-child(4) {
background: #f33;
}
main {
max-width: 1200px;
margin: 0 auto;
}
h1,
h2 {
text-align: center;
}
Not sure if this is what you're trying to achieve but I'd do it with by setting object-fit: contain on images. I also changed a bit the way (css) you're defining the divs.
body {
margin: 0;
}
.container {
display: flex;
flex-wrap: wrap;
}
.column {
height: 50vh;
width: 50vw;
display: flex;
justify-content: center;
align-items: center;
padding: 15px;
box-sizing: border-box;
}
.column img {
height: 100%;
width: 100%;
object-fit: contain;
}
.column:nth-child(1) {
background: #5c9;
}
.column:nth-child(2) {
background: #fb0;
}
.column:nth-child(3) {
background: #39f;
}
.column:nth-child(4) {
background: #f33;
}
<div class="container">
<div class="column"><img src="https://f4.bcbits.com/img/a0846297992_16.jpg"></div>
<div class="column">IMG 2</div>
<div class="column">IMG 3</div>
<div class="column">IMG 4</div>
</div>
I think this is what you are looking for.
Your column img class is set to 100% width and height. I set the height to 50% and the width to auto so it detects the image size and displays it noramlly.
And i simply removed the "object-fit: cover;".
If you change your .colum img {} to the following it should be exactly what you want.
.column img {
height: 50%;
width: auto;
}
I added a snippet so you can see it working.
body {
margin: 0;
}
.container {
display: flex;
flex-wrap: wrap;
}
.column {
height: 50vh;
width: 50vw;
display: flex;
justify-content: center;
align-items: center;
padding: 15px;
box-sizing: border-box;
}
.column img {
height: 50%;
width: auto;
}
.column:nth-child(1) {
background: #5c9;
}
.column:nth-child(2) {
background: #fb0;
}
.column:nth-child(3) {
background: #39f;
}
.column:nth-child(4) {
background: #f33;
}
<div class="container">
<div class="column"><img src="https://f4.bcbits.com/img/a0846297992_16.jpg"></div>
<div class="column">IMG 2</div>
<div class="column">IMG 3</div>
<div class="column">IMG 4</div>
</div>

Flexbox: how to put another object here [duplicate]

This question already has answers here:
Make a div span two rows in a grid
(2 answers)
Closed 2 years ago.
I'm using display: flex; to create tiles view.
I'm trying to put the third picture under the second picture.
But, whenever I do it, the third picture went under the first picture and won't come under second picture,
#media screen and (min-width: 1000px) {
.main {
height:1800px;
width: 100%;
margin: 0px 0px 0px 0px;
position: relative;
z-index: 0;
}
.parallax {
/* The image used */
background-image: url("https://i.ibb.co/r272XPt/2019-2020.png");
/* Set a specific height */
min-height: 400px;
opacity: 60%;
filter: blur(2px);
-webkit-filter: blur(2px);
/* Create the parallax scrolling effect */
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
}
.tiles{
position:relative;
top:100px;
width:90vw;
margin-left:5vw;
display: flex;
flex-wrap: wrap;
}
.chromebookHelpdesk img{
margin-left:5vw;
width:50vw;
display:block;
}
.subhelp{
height:25vw;
}
.subhelp img{
margin-left:5vw;
width:25vw;
display:block;
}
.studentsTour img{
margin-left:5vw;
width:20vw;
margin-top:5vw;
display:block;
}
#projects img {
text-align: center;
display: block;
width: 25vw;
margin: 20px;
}
.mission_logo {
width: 200px;
display: inline-block;
float: left;
text-align: center;
}
.mission {
text-align: center;
margin-top: 100px;
font-size: 20px;
}
.ingenuity {
color: #3cba54;
font-size: 60px !important;
}
.creativity {
color: #f4c20d;
font-size: 60px !important;
}
.innovation {
color: #db3236;
font-size: 60px !important;
}
}
#media only screen and (max-width: 1000px) {
.main {
height:2500px;
width: 100%;
margin: 0px 0px 0px 0px;
position: relative;
z-index: 0;
}
.parallax {
display: none;
}
.mission_logo {
width: 60vw;
text-align: center;
}
.mission {
text-align: center;
margin-top: 100px;
font-size: 15px;
}
.ingenuity {
color: #3cba54;
font-size: 40px !important;
}
.creativity {
color: #f4c20d;
font-size: 40px !important;
}
.innovation {
color: #db3236;
font-size: 40px !important;
}
}
.tiles h1 {
text-align: center;
font-size: 50px;
color: black;
}
.follow{
position:relative;
top:100px;
text-align:center;
border-radius: 50%;
background-color: #84e3ca;
width: 50vw;
height: 50vw;
margin-left: 25vw;
opacity:70%;
}
.follow h1{
font-size:35px;
padding-top: 20vw;
}
.follow h2{
font-size:30px;
}
<div class="main">
<div class="tiles">
<div class="chromebookHelpdesk"><a href="https://sledteam.github.io/sled/chromebook"><img
src="https://github.com/sledteam/sled/blob/master/Chromebook%20Helpdesk.png?raw=true" alt="Chromebook-Helpdesk"></a></div>
<div class="subhelp"><a href="https://sledteam.github.io/sled/chromebook"><img
src="https://github.com/sledteam/sled/blob/master/Sub%20Help.png?raw=true" alt="Sub Help"></a>
</div>
<div class="studentsTour"><a href="https://sledteam.github.io/sled/chromebook"><img
src="https://github.com/sledteam/sled/blob/master/New%20Students%20Tour.png?raw=true" alt="New Students Tour"></a></div>
</div>
I'm stuck with this for a week.
I would appreciate it if anyone knows a solution for this.
Complete guide to css flexbox
.tiles {
display: flex;
flex-wrap: wrap;
flex-direction: row;
flex: 1;
}
.section {
display: flex;
flex: 1;
flex-direction: column;
}
.item {
display: flex;
flex: 1;
padding: 1rem;
margin: 4px;
background: green;
color: white;
}
<div class="tiles">
<div class="section">
<div class="item">
<p>Chromebook Helpdesk</p>
</div>
</div>
<div class="section">
<div class="item">
<p>Sub Help</p>
</div>
<div class="item">
<p>Student Tour</p>
</div>
</div>
</div>
It is simple if you understand the concept here is the example:
As per your requirement you need 2 columns with single row so you will be creating flex property, now you need 2 columns hence you make it flex:50% like 2. Now coming to your image section where you need 2 images to be underneath so you will provide the height:50%(right images) and you will give height:100%(left image).
You can keep changing the sizes as you desire. You can also add responsive design for the same. Hope it helps.
* {
box-sizing: border-box;
}
.row {
display: flex;
}
.column {
flex: 50%;
padding: 5px;
}
<div class="row">
<div class="column">
<img src="http://i183.photobucket.com/albums/x312/Tiefnuker/office_02_zpsdz0zixcd.jpg" style="width:100%" />
</div>
<div class="column">
<div class="row">
<img src="http://i183.photobucket.com/albums/x312/Tiefnuker/office_02_zpsdz0zixcd.jpg" style="width:50%" />
</div>
<div class="row">
<img src="http://i183.photobucket.com/albums/x312/Tiefnuker/office_02_zpsdz0zixcd.jpg" style="width:50%" />
</div>
</div>
</div>

Background does not fill whitespace at the bottom of page when using min-height

The last media query (Resolution: 529x266) makes each of my blocks ("Work, About, Hire") have a min-height. But my background does not fill in the extra space. How do I make my background fill the extra space?
To see the white space make the codepen 529x266 or smaller.
https://codepen.io/anon/pen/QpVBGZ
html {height: 100%; font-size: 100%;}
body {
height: 100%;
font-size: 1rem;
}
* {
margin: 0;
border: 0;
box-sizing: border-box;
}
section {
display: flex;
height: 100%;
width: 100%;
background: url("https://images.unsplash.com/photo-1483914764278-6f2b1e39bba5?dpr=1&auto=format&fit=crop&w=1500&h=1377&q=80&cs=tinysrgb")no-repeat;
background-size: 100% 100%;
}
.work, .about, .hire {
display: flex;
height: 100%;
width: 33.3%;
opacity: 0.7;
justify-content: center;
align-items: center;
}
.opacity {
height: 100%;
width: 100%;
background: #000000;
z-index: 100;
}
span {
position:absolute;
height:100%;
width: 33.3%;
}
/* Text */
p {
position: absolute;
font-size: 5em;
font-weight: 900;
font-family: 'Teko', sans-serif;
text-transform: uppercase;
text-align: center;
}
a {
color: #fff;
text-decoration: none;
}
/* Hover */
.opacity:hover {
opacity: 0;
}
.work:hover, .about:hover, .hire:hover {
opacity: 1;
}
/* Media Queries */
/* 529px at 16px */
#media only screen and (max-width: 33.0625em) {
section {
display: flex;
flex-direction: column;
}
.work, .about, .hire {
height: 33.3%;
width: 100%;
}
span {
height: 33.3%;
width: 100%;
}
}
/* 266px at 16px */
#media only screen and (max-height: 16.625em) and (max-width: 33.0625em) {
section {
display: flex;
flex-direction: column;
}
.work, .about, .hire {
min-height: 38.3%;
width: 100%;
}
span {
min-height: 38.3%;
width: 100%;
}
}
<section>
<div class="work">
<div class="opacity">
<span></span>
</div>
<p>Work</p>
</div>
<div class="about">
<div class="opacity">
<span></span>
</div>
<p>About</p>
</div>
<div class="hire">
<div class="opacity">
<span></span>
</div>
<p>Hire</p>
</div>
</section>
The whitespace is because the elements inside the body are overflowing and by default overflow is visible.
So why is the content overflowing? It's because .work .about .hire each have min-height of 38.3%. Since there are three of them, the total min-height is > 100% therefore the elements overflow and you get the white band.
Change .work .about .hire's min-height to 33% and adjust the font size or other properties of the item to fit within the height constraints.

Design a 3-columns layout using flex

I have to design a 3 columns layout with these conditions:
I don't want to use percentages
I don't care if left or right columns are made with pixels
center column has to take the remaining width
elements have to be aligned vertically
I need the spans to be 100% height of their parent, to make the hover work with a full background-color
I tried using display:flex on the main container, it works well but I can't align the elements vertically. I tried using display-table: cell and vertical-align: middle but it doesn't seem to work with flex.
I developed a jsfiddle to show you what I tried: http://jsfiddle.net/v13yy2v3/4/
html, body {
height:100%;
}
#mainPercent {
background-color: red;
width: 100%;
height: 20%;
color: white;
}
#leftPercent {
background-color: green;
float: left;
width: 5%;
height:100%;
}
#centerPercent {
background-color: blue;
text-align: center;
float: left;
width: 90%;
/* percent isn't wanted */
height:100%;
display:table;
}
#centerPercent span {
display:table-cell;
vertical-align : middle;
}
#rightPercent {
background-color: purple;
float: right;
height:100%;
width: 5%;
}
#mainFlex {
background-color: red;
width: 100%;
height: 20%;
color: white;
display:flex;
/* align-items: center;
justify-content: center; items are not 100% height */
}
#leftFlex {
background-color: green;
}
#centerFlex {
background-color: blue;
text-align: center;
flex:1;
/*display:table;*/
}
#rightFlex {
background-color: purple;
}
#mainPx {
background-color: red;
width: 100%;
height: 20%;
color: white;
}
#leftPx {
width:128px;
float:left;
background-color: green;
}
#centerPx {
background-color: blue;
text-align: center;
width:100%;
}
#rightPx {
float:right;
width : 128px;
background-color: purple;
}
<br/>
<div id="mainPercent">
<div id="leftPercent"><span>left</span>
</div>
<div id="centerPercent"><span>center</span>
</div>
<div id="rightPercent"><span>right</span>
</div>
</div>
<br/>
<br/>
<div id="mainFlex">
<div id="leftFlex"><span>left</span>
</div>
<div id="centerFlex"><span>center</span>
</div>
<div id="rightFlex"><span>right</span>
</div>
</div>
<br/>
<br/>
<div id="mainPx">
<div id="leftPx"><span>left</span>
</div>
<div id="centerPx"><span>center</span>
</div>
<div id="rightPx"><span>right</span>
</div>
</div>
You'd have to keep extending the flexbox to the child items and the spans.
* {
margin: 0;
padding: 0;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
html,
body {
height: 100%;
}
#mainFlex {
background-color: red;
width: 100%;
height: 20%;
color: white;
display: flex;
justify-content: center;
}
.left {
background-color: green;
}
.center {
background-color: blue;
text-align: center;
flex: 1;
}
.right {
background-color: purple;
}
.child {
display: flex;
flex-direction: column;
}
span {
display: flex;
flex-direction: column;
justify-content: center;
align-content: center;
padding: 0.25em;
flex: 1;
}
span:hover {
background: #bada55;
}
<div id="mainFlex">
<div class=" left child"><span>left</span>
</div>
<div class="center child"><span>center</span>
</div>
<div class="right child"><span>right</span>
</div>
</div>