How divide HTML Page vertically into 4 sections - html

I want divide my HTML Page into 4 different vertical sections .
I want each section to have a different background color, for that I used div but it each background color does not cover the sides of each section.
** My aspire end result:
I don't want to see the color red of the body background color in the html.
body {
background-color: red;
}
.intro {
background-color: #674AB3;
}
.edu {
background-color: #A348A6;
}
.Skills {
background-color: #9F63C4;
}
.end {
background-color: #9075D8;
}
<div class="intro">
<hr>
</div>
<div class="edu">
<hr>
</div>
<div class="Skills">
<hr>
</div>
<div class="end">
<hr>
</div>

.flex-container {
display: flex;
justify-content: center;
align-items: center;
}
.flex-container>div {
margin:30px;
}
.flex-container hr {
width: 100px;
height: 100px;
padding: 5px;
margin: 10px;
border-color: #FFF;
box-shadow: none;
border-width: 5px;
}
.intro {
background-color: #674AB3;
}
.edu {
background-color: #A348A6;
}
.Skills {
background-color: #9F63C4;
}
.end {
background-color: #9075D8;
}
<div class="flex-container">
<div class="intro"><hr></div>
<div class="edu"><hr></div>
<div class="Skills"><hr></div>
<div class="end"><hr></div>
</div>

Set margin: 0 for body, it has a defualt margin.
Set <hr>'s margin to 0.
Set height for each div to be 25vh (vertical height).
body {
background-color: red;
margin: 0;
}
.intro {
background-color: #674AB3;
height: 25vh;
}
.edu {
background-color: #A348A6;
height: 25vh;
}
.Skills {
background-color: #9F63C4;
height: 25vh;
}
.end {
background-color: #9075D8;
height: 25vh;
}
hr {
margin: 0;
}
<div class="intro">
<hr/>
</div>
<div class="edu">
<hr/>
</div>
<div class="Skills">
<hr/>
</div>
<div class="end">
<hr/>
</div>

You could try using grid! might as well make it responsive :D
This is to have 4 sections laying one next to another, to make them stack in the vertical direction, change:
grid-template-columns: repeat(4, 1fr);
to:
grid-template-rows: repeat(4, 1fr);
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
background-color: #00000000; /* transparent color */
display: grid;
grid-template-columns: repeat(4, 1fr); /* 4 vertical sections */
height: 100vh;
margin: auto;
}
.intro {
background-color: #674AB3;
}
.edu {
background-color: #A348A6;
}
.Skills {
background-color: #9F63C4;
}
.end {
background-color: #9075D8;
}
<div class="intro">
<p>intro</p>
</div>
<div class="edu">
<p>edu</p>
</div>
<div class="Skills">
<p>Skills</p>
</div>
<div class="end">
<p>end</p>
</div>

Something like this?
body {
background-color: red;
}
.intro {
height:200px;
background-color: #674AB3 !important;
}
.edu {
height:200px;
background-color: #A348A6 !important;;
}
.Skills {
height:200px;
background-color: #9F63C4 !important;;
}
.end {
height:200px;
background-color: #9075D8 !important;;
}
<div class="intro">
<hr>
</div>
<div class="edu">
<hr>
</div>
<div class="Skills">
<hr>
</div>
<div class="end">
<hr>
</div>

You can try this approach as well.
body {background-color:transparent;}
.intro {
background-color: #674AB3;
height:100vh;
width:100%;
}
.edu {
background-color: #A348A6;
height:100vh;
width:100%;
}
.Skills {
background-color: #9F63C4;
height:100vh;
width:100%;
}
.end {
background-color: #9075D8;
height:100vh;
width:100%;
}
.wrapper {
display:grid;
height:100vh;
width:100%;
}
<div class="wrapper">
<div class="intro">
Hello
</div>
<div class="edu">
Hello
</div>
<div class="Skills">
Hello
</div>
<div class="end">
Hello
</div>
</div>

You can simmply use display: flex to the parent container which is flex-container
like
<div style="display: flex;">
<div class="intro"><hr></div>
<div class="edu"><hr></div>
<div class="Skills"><hr></div>
<div class="end"><hr></div>
</div>

<div class="intro">
</div>
<div class="edu">
</div>
<div class="Skills">
</div>
<div class="end">
</div>
</div>
body {
background-color: red;
}
.main{
display: flex;
grid-template-columns: repeat(4,1fr);
width:100vw;
}
.intro {
background-color: #674AB3;
width: 25%;
height: 75vh;
}
.edu {
background-color: #A348A6;
width: 25%;
height: 75vh;
}
.Skills {
background-color: #9F63C4;
width: 25%;
height: 75vh;
}
.end {
background-color: #9075D8;
width: 25%;
height: vh;
}
grid

<div class="main">
<div class="intro">
</div>
<div class="edu">
</div>
<div class="Skills">
</div>
<div class="end">
</div>
.main{
display: flex;
grid-template-columns: repeat(4,1fr);
width:100vw;
}
.intro {
background-color: #674AB3;
width: 25%;
height: 75vh;
}
.edu {
background-color: #A348A6;
width: 25%;
height: 75vh;
}
.Skills {
background-color: #9F63C4;
width: 25%;
height: 75vh;
}
.end {
background-color: #9075D8;
width: 25%;
height:75vh;
}

Related

Sticky footer when using flex

I am trying to make a responsive website with a sticky footer. So far I have been able to achieve what I want with the following HTML and CSS up until the point if anything inside content expands the div, for example if there are several more products, the footer will float over the top of the content div. What I am trying to achieve is for the footer to keep getting pushed down by the content above it.
I've tried using margin 0 auto on the wrapper div which uses flex instead, however it doesn't seem to achieve the results I am looking for.
Can someone help me unblock myself, or advice what I should actually be doing?
Thank you
/* Desktop */
#media (max-width: 1480px) {
.content,
.header,
.footer {
width: 100%;
margin: 0 20px;
}
.border {
display: none;
}
.product {
width: 33.33% !important;
}
}
/* Tablet */
#media (max-width: 768px) {
.header,
.footer {
margin: 0 auto;
}
.product {
width: 100% !important;
}
}
/* Mobile */
#media (max-width: 360px) {}
/* Main */
.push {
height: 100%;
}
.wrapper {
display: flex;
justify-content: center;
}
.header,
.content,
.footer {
width: 1380px;
}
.header {
height: 100px;
background: green;
}
.content {
background: blue;
height: 100%;
}
.border {
width: 130px;
height: 100px;
float: left;
background: rebeccapurple;
}
.product {
width: 280px;
height: 519px;
float: left;
background: fuchsia;
}
.footer {
height: 200px;
margin-top: -200px;
background: greenyellow;
}
<body>
<div class="push">
<div class="wrapper">
<div class="header">
<div class="border"></div>
</div>
</div>
<div class="wrapper">
<div class="content">
<div class="border"></div>
<div class="product"></div>
<div class="product"></div>
<div class="product"></div>
<div class="product"></div>
<div class="border"></div>
</div>
</div>
</div>
<div class="wrapper">
<div class="footer">
<div class="border"></div>
</div>
</div>
</body>
Updated:
position: sticky; won't do, since you need to have footer always on the bot part of the page no matter the height of the content. So, we get to plan B which is position: fixed; with bottom: 0; and width: 100%;, which makes footer be full-width fixed on bottom but without taking any actual space.
In order to make this work, you need to add a margin-bottom on the content div with the height of your footer. In that way, you "fake" the existence of footer, therefore displaying the whole content of content:
body {
margin: 0;
position: relative;
}
header {
min-height: 60px;
background: #222;
color: #fff;
display: flex;
align-items: center;
}
.content {
display: flex;
flex-wrap: wrap;
/* Insert the height of your footer element (mine is 60px) */
margin-bottom: 60px;
}
.product {
min-width: 200px;
min-height: 200px;
background: linear-gradient(45deg, #ccc, #ccc5);
}
footer {
position: fixed;
width: 100%;
bottom: 0;
height: 60px;
background: #252525;
color: #fff;
display: flex;
align-items: center;
gap: 10px;
}
<body>
<header>
<h2>This is the header.</h2>
</header>
<section class="content">
<div class="product"></div>
<div class="product"></div>
<div class="product"></div>
<div class="product"></div>
<div class="product"></div>
<div class="product"></div>
<div class="product"></div>
<div class="product"></div>
<div class="product"></div>
<div class="product"></div>
<div class="product"></div>
<div class="product"></div>
<div class="product"></div>
</section>
<footer>
<h2>This is the footer</h2>
<div class="info">Lorem Ipsum</div>
<div class="info">Lorem Ipsum</div>
<div class="info">Lorem Ipsum</div>
</footer>
</body>
try this approach:
style {
.flex-body {
display:flex;
width:100%;
height:100%; /* or use 100vh */
}
.flex-body .body {
flex-grow:1;
position:relative;
}
.flex-body .body .container {
position:absolute;
width:100%;
height:100%;
overflow-y:auto;
}
.flex-body .footer {
/* add footer styles here */
}
}
and inside your <body> tag:
<div class="flex-body">
<div class="body">
<div class="container"><!-- contents of container --></div>
</div>
<div class="footer"><!-- contents of footer --></div>
</div>
<head>
<style>
/* Desktop */
#media (max-width: 1480px) {
.main-wrapper, .content, .header, .footer {
width: 100% !important;
}
.border {
display: none;
}
.product {
width: 33.33% !important;
}
}
/* Tablet */
#media (max-width: 768px) {
.product {
width: 100% !important;
}
}
/* Mobile */
#media (max-width: 360px) {
}
/* Main */
body {
position: relative;
}
.main-wrapper, .header, .content, .footer {
width: 1380px;
}
.main-wrapper {
height: 100%;
margin: 0 auto;
}
.footer-wrapper {
clear: both;
height: 180px;
margin-top: -180px;
}
.header {
height: 100px;
background: green;
}
.content {
background: blue;
}
.footer {
height: 100%;
margin: 0 auto;
background: greenyellow;
}
.border {
width: 130px;
height: 100px;
float: left;
background: rebeccapurple;
}
.product {
width: 280px;
height: 519px;
float: left;
background: fuchsia;
}
</style>
</head>
<body>
<div class="main-wrapper">
<div class="header">
<div class="border"></div>
<div class="border"></div>
</div>
<div class="content">
<div class="border"></div>
<div class="product">
</div>
<div class="product">
</div>
<div class="product">
</div>
<div class="product">
</div>
<div class="border"></div>
</div>
</div>
<div class="footer-wrapper">
<div class="footer">
<div class="border"></div>
<div class="border"></div>
</div>
</div>
</body>

How to make a proportional 4 column layout with html and css

How do I make a 4 column design for html with css? I want to have 1 column that's 5% of the screen, one that's 65%, another that's 5%, and the fourth that's 25%, all in that order. I've tried tons of online tutorials, but none of them work, each one gives a random problem.
I have provided simple examples using multiple display types. If you need to set the height to full width, then set the rule height: 100vh for the container.
#1 table:
body {
box-sizing: border-box;
margin: 0;
padding: 0;
}
.container {
display: table;
width: 100%;
}
.container_box {
display: table-cell;
color: white;
}
.container_box:nth-child(1) {
width: 5%;
background-color: yellow;
}
.container_box:nth-child(2) {
width: 65%;
background-color: green;
}
.container_box:nth-child(3) {
width: 25%;
background-color: red;
}
.container_box:nth-child(4) {
width: 5%;
background-color: grey;
}
<div class="container">
<div class="container_box">
1
</div>
<div class="container_box">
2
</div>
<div class="container_box">
3
</div>
<div class="container_box">
4
</div>
</div>
#2 flex:
body {
box-sizing: border-box;
margin: 0;
padding: 0;
}
.container {
display: flex;
width: 100%;
}
.container_box {
color: white;
}
.container_box:nth-child(1) {
width: 5%;
background-color: yellow;
}
.container_box:nth-child(2) {
width: 65%;
background-color: green;
}
.container_box:nth-child(3) {
width: 25%;
background-color: red;
}
.container_box:nth-child(4) {
width: 5%;
background-color: grey;
}
<div class="container">
<div class="container_box">
1
</div>
<div class="container_box">
2
</div>
<div class="container_box">
3
</div>
<div class="container_box">
4
</div>
</div>
#3 grid:
body {
box-sizing: border-box;
margin: 0;
padding: 0;
}
.container {
display: grid;
grid-template-columns: 5% 65% 25% 5%;
width: 100%;
}
.container_box {
color: white;
}
.container_box:nth-child(1) {
background-color: yellow;
}
.container_box:nth-child(2) {
background-color: green;
}
.container_box:nth-child(3) {
background-color: red;
}
.container_box:nth-child(4) {
background-color: grey;
}
<div class="container">
<div class="container_box">
1
</div>
<div class="container_box">
2
</div>
<div class="container_box">
3
</div>
<div class="container_box">
4
</div>
</div>
Please use flexbox.
.parent{
display:flex;
justify-content: space-around;
flex-direction: row;
}
.child{
border:1px solid black;
height:50px;
background-color: lightblue;
}
.child:nth-child(0){
flex-basis: 5%;
}
.child:nth-child(1){
flex-basis: 65%;
}
.child:nth-child(2){
flex-basis: 5%;
}
.child:nth-child(3){
flex-basis: 25%;
}
<div class="parent">
<div class="child">content</div>
<div class="child">content</div>
<div class="child">content</div>
<div class="child">content</div>
</div>

How can I put two <div> that has the same width and height next to each other without having to scroll?

I'm currently making a simple html page with two sections with content inside of each of them but the last content of the second div .right is going on the bottom of the page and make the page scrollable.
I tried making another div and put a flex-direction: column but it doesn't work:
body {
margin: 0;
}
.main-container {
display: flex;
flex-direction: column;
}
.left {
background: #ecece9;
width: 50%;
height: 100vh;
}
.right {
background: #ffffff;
width: 50%;
height: 100vh;
}
<div class="main-container">
<div class="left">
<h2>content</h2>
</div>
<div class="right">
<h2>content should be on top</h2>
</div>
</div>
How can I put two <div> that has the same width and height next to each other without having to scroll?
You need to use flex-direction: row and not flex-direction: column.
To avoid repeating width: 50%; height: 100vh; for both .left and .right, I would also create another class, such as .box, which is applied to both and contains these properties.
body {
margin: 0;
}
.main-container {
display: flex;
flex-direction: row;
}
.box {
width: 50%;
height: 100vh;
}
.left {
background: #ecece9;
}
.right {
background: #ffffff;
}
<div class="main-container">
<div class="left box">
<h2>content</h2>
</div>
<div class="right box">
<h2>content should be on top</h2>
</div>
</div>
Change the flex direction from column to row
body {
margin: 0;
}
.main-container {
display: flex;
flex-direction: row;
}
.left {
background: #ecece9;
width: 50%;
height: 100vh;
}
.right {
background: #ffffff;
width: 50%;
height: 100vh;
}
<div class="main-container">
<div class="left">
<h2>content</h2>
</div>
<div class="right">
<h2>content should be on top</h2>
</div>
</div>
Use CSS Grid to build Layouts it is very powerful. See I changed only two lines and the layout is ready.
.main-container {
display: grid;
grid-template-columns: 50% 50%
}
body {
margin: 0;
}
.main-container {
display: grid;
grid-template-columns: 50% 50%
}
.left {
background: #ecece9;
height: 100vh;
}
.right {
background: #ffffff;
height: 100vh;
}
<div class="main-container">
<div class="left">
<h2>content</h2>
</div>
<div class="right">
<h2>content should be on top</h2>
</div>
</div>
Try only display flex on large screen and and block on mobile
body {
margin: 0;
}
.main-container {
display: flex;
}
.left {
background: #ecece9;
width: 50%;
height: 100vh;
padding:20px;
}
.right {
background: #ddd;
width: 50%;
height: 100vh;
padding:20px;
}
/* For mobile screen
#media only screen and (max-width: 768px) {
.main-container{
display: block;
}
.left, .right{
width: 100%;
}
}
*/
<div class="main-container">
<div class="left">
<h2>content</h2>
</div>
<div class="right">
<h2>content should be on top</h2>
</div>
</div>

How to create CSS box layouts?

I'm quite new to web development and I would like to create this in CSS and HTML:
I am unsure how to do this as I am only 13 and still learning.
What I have tried but failed miserably with:
.grey{
height:300px;
width:700px;
background-color: #e5e5e5;
z-index: 0;
}
.pink{
height:150px;
width:100px;
background-color:#ff8a8a;
padding-top: 0px;
padding-right:0px;
z-index: 1;
}
<div class="grey">
<div class="pink"> </div>
</div>
Use CSS-grid which is built-in within CSS. See code snippet.
.wrapper {
display: grid;
grid-template-columns:
40%
1fr
1fr
;
grid-template-rows:
100px
100px
;
grid-template-areas:
"box-1 box-2 box-4"
"box-1 box-3 box-5"
;
}
.box-1 {
grid-area: box-1;
background-color: grey;
}
.box-2 {
grid-area: box-2;
background-color: orange;
}
.box-3 {
grid-area: box-3;
background-color: lightblue;
}
.box-4 {
grid-area: box-4;
background-color: red;
}
.box-5 {
grid-area: box-5;
background-color: lightgreen;
}
<div class="wrapper">
<div class="box-1"></div>
<div class="box-2"></div>
<div class="box-3"></div>
<div class="box-4"></div>
<div class="box-5"></div>
</div>
Using flex:
.container {
display: flex;
justify-content: flex-start;
background-color: #e5e5e5;
}
.box {
height:150px;
width: 50%;
display: flex;
flex-wrap: wrap;
}
.square {
height: 50%;
width: 50%;
}
.square--pink {
background-color: #fb7378;
}
.square--orange {
background-color: #fcbd8b;
}
.square--blue {
background-color: #8ce0fd;
}
.square--green {
background-color: #7cff83;
}
<div class="container">
<div class="box"></div>
<div class="box">
<div class='square square--pink'></div>
<div class='square square--orange'></div>
<div class='square square--blue'></div>
<div class='square square--green'></div>
</div>
</div>
You should look over the css box model btw, it would help you better understand how to structure your HTML for your css :).
.grey{
height:300px;
width:600px;
background-color: #e5e5e5;
z-index: 0;
}
.pink{
height:150px;
width:150px;
background-color:#ff8a8a;
padding-top: 0px;
padding-right:0px;
z-index: 1;
float:right;
}
.green{
height:150px;
width:150px;
background-color:green;
padding-top: 150px;
padding-right:0px;
z-index: 1;
float:right;
}
.skyblue{
height:150px;
width:150px;
background-color:skyblue;
padding-top: 0px;
padding-right:0px;
z-index: 1;
float:right;
}
.orange{
height:150px;
width:150px;
background-color:orange;
padding-top: 150px;
padding-right:0px;
z-index: 1;
float:right;
}
<div class="grey">
<div class="green">
<div class="pink">
</div>
</div>
<div class="orange">
<div class="skyblue">
</div>
</div>
</div>
Here's a quick example:
<div class="container">
<nav class="nav left">left</nav>
<nav class="nav right">right</nav>
<nav class="nav right1">right</nav>
</div>
CSS:
.container {
display: flex;
flex-flow: row;
min-height: 50vh;
}
.left {
flex: 5;
background-color: grey;
width: 70%;
}
.right {
flex:2;
background-color: green;
}
.right1 {
flex:2;
background-color: red;
}
here is your solution.
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
div { height: 102px; float:right; width: 150px;z-index: 2; }
.box{ width:600px;height:206px;background:grey;border:1px grey;z-index:0;float:left;}
.red { background: red; }
.green { background: green; }
.blue { background: blue; clear: right; }
.orange { background: orange; }
</style>
</head>
<body>
<div class="box">
<div class="red"></div>
<div class="green"></div>
<div class="blue"></div>
<div class="orange"></div>
</div>
</body>
</html>
With Flexbox, you can do something like this:
.grey {
display: flex; /* displays flex-items (children) inline */
justify-content: flex-end; /* places the flex-item (.innerFlex) to the end of the horizontal line */
height: 300px;
width: 700px;
max-width: 100%;
background: #e5e5e5;
}
.innerFlex {
display: flex;
flex-wrap: wrap; /* enables wrapping */
flex-basis: 200px; /* initial width set to 200px since its flex-items are 100px wide and you want them to wrap */
}
.pink {
flex-basis: 100px; /* initial width set to 100px */
height: 150px;
background: orange;
padding-top: 0;
padding-right: 0;
}
.pink:nth-child(2) {background: red}
.pink:nth-child(3) {background: blue}
.pink:nth-child(4) {background: green}
<div class="grey">
<div class="innerFlex"> <!-- additional wrapper -->
<div class="pink"></div>
<div class="pink"></div>
<div class="pink"></div>
<div class="pink"></div>
</div>
</div>
With the Grid:
.grey {
display: grid;
grid-template: 150px 150px / auto 100px 100px;
width: 700px;
max-width: 100%;
background: #e5e5e5;
}
.pink:nth-child(1) {grid-column: 2; background: orange}
.pink:nth-child(2) {background: red}
.pink:nth-child(3) {grid-column: 2; background: blue}
.pink:nth-child(4) {background: green}
<div class="grey">
<div class="pink"></div>
<div class="pink"></div>
<div class="pink"></div>
<div class="pink"></div>
</div>

some issue with margin in flex container

need some help. How to fix bug with .half-img2{ margin-top: 10px; }
http://prntscr.com/94uqok
These 2 imgs height must be equal to main-img
http://plnkr.co/edit/Dvj5HfG6hJqvYPxr0ljJ?p=preview
Html:
<style type="text/css">
.test{
display: flex;
}
.test>div{
flex: 1;
}
.test .main-img{
flex-grow: 2;
}
img{
width: 100%;
}
.half-img{
margin-left: 10px;
}
.half-img2{
margin-top: 10px;
}
</style>
<div class="test">
<div class="main-img">
<img src="http://fakeimg.pl/350x200/00CED1/FFF/?text=img+placeholder">
</div>
<div class="half-img">
<div class="half-img1">
<img src="http://fakeimg.pl/350x200/00CED1/FFF/?text=img+placeholder">
</div>
<div class="half-img2">
<img src="http://fakeimg.pl/350x200/00CED1/FFF/?text=img+placeholder">
</div>
</div>
</div>
I'll ignore the images sizes as these are not really relevant to the div layout issue.
A judicious use of margins and flex-column div layout seems to be required.
Layout would be something like this.
* {
box-sizing: border-box;
}
.test {
display: flex;
width: 80%;
margin: 1em auto;
border:1px solid green;
}
img {
display: block;
}
.test div {
}
.main-img {
flex:2;
margin-right: 10px;
background: lightblue;
}
.half-img {
display: flex;
flex-direction: column;
height: 250px;
}
.half-img {
flex:1;
display: flex;
flex-direction: column;
}
.half-img div {
flex:1;
background: lightblue;
}
.half-img1 {
margin-bottom: 5px;
}
.half-img2 {
margin-top: 5px;
}
<div class="test">
<div class="main-img">
</div>
<div class="half-img">
<div class="half-img1">
</div>
<div class="half-img2">
</div>
</div>
</div>