Why is my CSS grid layout not responsive? - html

I've been having trouble with my CSS grid. I used the repeat function, but it just messed up my layout and the webpage still isn't responsive. I want it to display an image when the max width is 600px, and show my periodic table design when the width is above that. I'm new to front-end development and could really use some help. Thanks for taking the time to read this!
.container { display: grid;
grid-template-columns: 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr;
grid-template-rows: 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr;
grid-gap: 0.4rem;
grid-auto-flow: row;
width: 100vw;
height: 100vh;
}
.noble-gas { display: grid;
grid-template-columns: 1fr;
grid-template-rows: 1fr 1fr 1fr 1fr 1fr 1fr 1fr;
grid-gap: 0.4rem;
grid-auto-flow: row;
grid-template-areas:
"he"
"ne"
"a"
"ky"
"xe"
"rn"
"og";
grid-area: 2 / 20 / 9 / 21;
}
.rn, .og, .xe, .ky, .a, .ne, .he, .h {
background-color: #dea4a2;
padding-left: 8%;
border-radius: 3px;
box-shadow: 0px 2.5px 3px 2px rgba(27, 1, 22, 0.247);
}
.rn { grid-area: rn; }
.og { grid-area: og; }
.xe { grid-area: xe; }
.ky { grid-area: ky; }
.a { grid-area: a; }
.ne { grid-area: ne; }
.he { grid-area: he; }
.halogen { display: grid;
grid-template-columns: 1fr;
grid-template-rows: 1fr 1fr 1fr 1fr 1fr 1fr;
grid-gap: 0.4rem;
grid-auto-flow: row;
grid-template-areas:
"f"
"cl"
"br"
"i"
"at"
"ts";
grid-area: 3 / 19 / 9 / 20;
}
.ts, .at, .i, .br, .cl, .f {
background-color: #c2a8a9;
padding-left: 8%;
border-radius: 3px;
box-shadow: 0px 2.5px 3px 2px rgba(27, 1, 22, 0.247);
}
.ts { grid-area: ts; }
.at { grid-area: at; }
.i { grid-area: i; }
.br { grid-area: br; }
.cl { grid-area: cl; }
.f { grid-area: f; }
.non-metal { display: grid;
grid-template-columns: 1fr 1fr 1fr;
grid-template-rows: 1fr 1fr 1fr;
grid-gap: 0.4rem;
grid-auto-flow: row;
grid-template-areas:
"c n o"
". p s"
". . se";
grid-area: 3 / 16 / 6 / 19;
}
.se, .s, .o, .n, .p, .c {
background-color: #d19595;
padding-left: 8%;
border-radius: 3px;
box-shadow: 0px 2.5px 3px 2px rgba(27, 1, 22, 0.247);
box-shadow: 0px 2.5px 3px 2px rgba(27, 1, 22, 0.247);
}
.se { grid-area: se; }
.s { grid-area: s; }
.o { grid-area: o; }
.n { grid-area: n; }
.p { grid-area: p; }
.c { grid-area: c; }
.semimetal { display: grid;
grid-template-columns: 1fr 1fr 1fr 1fr;
grid-template-rows: 1fr 1fr 1fr 1fr 1fr;
grid-gap: 0.4rem;
grid-auto-flow: row;
grid-template-areas:
"b . . ."
". si . ."
". ge as ."
". . sb te"
". . . po";
grid-area: 3 / 15 / 8 / 19;
}
.po, .te, .sb, .as, .ge, .si, .b {
background-color: #f4b8b0;
padding-left: 8%;
border-radius: 3px;
box-shadow: 0px 2.5px 3px 2px rgba(27, 1, 22, 0.247);
}
.po { grid-area: po; }
.te { grid-area: te; }
.sb { grid-area: sb; }
.as { grid-area: as; }
.ge { grid-area: ge; }
.si { grid-area: si; }
.b { grid-area: b; }
.basic-metal { display: grid;
grid-template-columns: 1fr 1fr 1fr 1fr;
grid-template-rows: 1fr 1fr 1fr 1fr 1fr;
grid-gap: 0.4rem;
grid-auto-flow: row;
grid-template-areas:
"al . . ."
"ga . . ."
"in sn . ."
"tl pb bi ."
"nh fi mc lv";
grid-area: 4 / 15 / 9 / 19;
}
.lv, .mc, .fi, .nh, .bi, .pb, .tl, .sn, .in, .ga, .al {
background-color: #bd8c88;
padding-left: 8%;
border-radius: 3px;
box-shadow: 0px 2.5px 3px 2px rgba(27, 1, 22, 0.247);
}
.lv { grid-area: lv; }
.mc { grid-area: mc; }
.fi { grid-area: fi; }
.nh { grid-area: nh; }
.bi { grid-area: bi; }
.pb { grid-area: pb; }
.tl { grid-area: tl; }
.sn { grid-area: sn; }
.in { grid-area: in; }
.ga { grid-area: ga; }
.al { grid-area: al; }
.lanthonide { grid-area: 7 / 5 / 8 / 6;
background-color: #ffe0e1;
padding-left: 8%;
border-radius: 3px;
box-shadow: 0px 2.5px 3px 2px rgba(27, 1, 22, 0.247);}
.actinide { grid-area: 8 / 5 / 9 / 6;
background-color: #c8807d;
padding-left: 8%;
border-radius: 3px;
box-shadow: 0px 2.5px 3px 2px rgba(27, 1, 22, 0.247); }
.transition-metal { display: grid;
grid-template-columns: 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr;
grid-template-rows: 1fr 1fr 1fr 1fr;
grid-gap: 0.4rem 0.4rem;
grid-auto-flow: row;
grid-template-areas:
"sc ti v cr mn fe co ni cu zn"
"y zr nb mo tc ru rh pd ag cd"
". hf ta w re os ir pt au hg"
". rf db sg bh hs mt ds rg cn";
grid-area: 5 / 5 / 9 / 15;
}
.zn, .cd, .hg, .cn, .rg, .au, .ag, .cu, .ni, .pd, .pt, .ds, .co, .rh, .ir, .mt, .fe, .ru, .os, .hs, .mn, .tc, .re, .bh, .cr, .mo, .w, .sg, .db, .ta, .nb, .v, .rf, .hf, .zr, .ti, .y, .sc {
background-color: #e7cace;
padding-left: 8%;
border-radius: 3px;
box-shadow: 0px 2.5px 3px 2px rgba(27, 1, 22, 0.247);
}
.zn { grid-area: zn; }
.cd { grid-area: cd; }
.hg { grid-area: hg; }
.cn { grid-area: cn; }
.rg { grid-area: rg; }
.au { grid-area: au; }
.ag { grid-area: ag; }
.cu { grid-area: cu; }
.ni { grid-area: ni; }
.pd { grid-area: pd; }
.pt { grid-area: pt; }
.ds { grid-area: ds; }
.co { grid-area: co; }
.rh { grid-area: rh; }
.ir { grid-area: ir; }
.mt { grid-area: mt; }
.fe { grid-area: fe; }
.ru { grid-area: ru; }
.os { grid-area: os; }
.hs { grid-area: hs; }
.mn { grid-area: mn; }
.tc { grid-area: tc; }
.re { grid-area: re; }
.bh { grid-area: bh; }
.cr { grid-area: cr; }
.mo { grid-area: mo; }
.w { grid-area: w; }
.sg { grid-area: sg; }
.db { grid-area: db; }
.ta { grid-area: ta; }
.nb { grid-area: nb; }
.v { grid-area: v; }
.rf { grid-area: rf; }
.hf { grid-area: hf; }
.zr { grid-area: zr; }
.ti { grid-area: ti; }
.y { grid-area: y; }
.sc { grid-area: sc; }
.alkaline-earth { display: grid;
grid-template-columns: 1fr;
grid-template-rows: 1fr 1fr 1fr 1fr 1fr 1fr;
grid-gap: 0.4rem;
grid-auto-flow: row;
grid-template-areas:
"be"
"mg"
"ca"
"sr"
"ba"
"ra";
grid-area: 3 / 4 / 9 / 5;
}
.ra,.ba,.sr,.ca,.mg,.be {
background-color: #c8a0a1;
padding-left: 8%;
border-radius: 3px;
box-shadow: 0px 2.5px 3px 2px rgba(27, 1, 22, 0.247);
}
.ra { grid-area: ra; }
.ba { grid-area: ba; }
.sr { grid-area: sr; }
.ca { grid-area: ca; }
.mg { grid-area: mg; }
.be { grid-area: be; }
.alkali-metal { display: grid;
grid-template-columns: 1fr;
grid-template-rows: 1fr 1fr 1fr 1fr 1fr 1fr;
grid-gap: 0.4rem 0.4rem;
grid-auto-flow: row;
grid-template-areas:
"li"
"na"
"k"
"rb"
"cs"
"fr";
grid-area: 3 / 3 / 9 / 4;
}
.fr,.cs,.rb,.k,.na,.li {
background-color: #ffe0e1;
padding-left: 8%;
border-radius: 3px;
box-shadow: 0px 2.5px 3px 2px rgba(27, 1, 22, 0.247);
}
.fr { grid-area: fr; }
.cs { grid-area: cs; }
.rb { grid-area: rb; }
.k { grid-area: k; }
.na { grid-area: na; }
.li { grid-area: li; }
.non-metal2 { display: grid;
grid-template-columns: 1fr;
grid-template-rows: 1fr;
grid-gap: 0.4rem;
grid-auto-flow: row;
grid-template-areas:
"h";
grid-area: 2 / 3 / 3 / 4;
}
.h { grid-area: h; }
.lanthonide2 { display: grid;
grid-template-columns: 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr;
grid-template-rows: 1fr;
grid-gap: 0.4rem;
grid-auto-flow: row;
grid-template-areas:
"la ce pr nd pm sm eu gd tb dy ho er tm yb lu";
grid-area: 10 / 5 / 11 / 20;
}
.la,
.ce,
.pr,
.nd,
.pm,
.sm,
.eu,
.gd,
.tb,
.dy,
.ho,
.er,
.tm,
.yb,
.lu,
.la {
background-color: #ffe0e1;
padding-left: 8%;
border-radius: 3px;
box-shadow: 0px 2.5px 3px 2px rgba(27, 1, 22, 0.247);
}
.la { grid-area: la; }
.ce { grid-area: ce; }
.pr { grid-area: pr; }
.nd { grid-area: nd; }
.pm { grid-area: pm; }
.sm { grid-area: sm; }
.eu { grid-area: eu; }
.gd { grid-area: gd; }
.tb { grid-area: tb; }
.dy { grid-area: dy; }
.ho { grid-area: ho; }
.er { grid-area: er; }
.tm { grid-area: tm; }
.yb { grid-area: yb; }
.lu { grid-area: lu; }
.actinide2 { display: grid;
grid-template-columns: 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr;
grid-template-rows: 1fr;
grid-gap: 0.4rem;
grid-auto-flow: row;
grid-template-areas:
"ac th pa u np pu am cm bk cf es fm md no lr";
grid-area: 11 / 5 / 12 / 20;
}
.ac,
.th,
.pa,
.u,
.np,
.pu,
.am,
.cm,
.bk,
.cf,
.es,
.fm,
.md,
.no,
.lr {
background-color: #c8807d;
padding-left: 8%;
border-radius: 3px;
box-shadow: 0px 2.5px 3px 2px rgba(27, 1, 22, 0.247);
}
.ac { grid-area: ac; }
.th { grid-area: th; }
.pa { grid-area: pa; }
.u { grid-area: u; }
.np { grid-area: np; }
.pu { grid-area: pu; }
.am { grid-area: am; }
.cm { grid-area: cm; }
.bk { grid-area: bk; }
.cf { grid-area: cf; }
.es { grid-area: es; }
.fm { grid-area: fm; }
.md { grid-area: md; }
.no { grid-area: no; }
.lr { grid-area: lr; }
number {
color: #1a030371;
font-size: 7px;
font-family: Barlow, sans-serif;
font-style: thin;
text-transform: uppercase;
justify-self: center;
align-self: center;
text-shadow: 2px 2px 5px rgba(52, 2, 25, 0);
}
element {
color: #2c2c2c;
font-size: 15px;
font-family: Barlow, sans-serif;
font-style: bold;
text-transform: uppercase;
padding-left: 20px;
justify-self: center;
align-self: center;
text-shadow: 2px 2px 5px rgba(52, 2, 25, 0.527);
}
element_name {
color: #1a030382;
font-size: 7px;
font-family: Barlow, sans-serif;
font-style: bold;
text-transform: uppercase;
justify-self: center;
align-self: center;
text-shadow: 2px 2px 5px rgba(52, 2, 25, 0.402);
}
atomic_number {
color: #1a030361;
font-size: 8px;
font-family: Barlow, sans-serif;
text-transform: uppercase;
justify-self: center;
align-self: center;
text-shadow: 2px 2px 5px rgba(52, 2, 25, 0.192);
}

Related

HTML button displays very differently on 2 pages with same CSS

I have two web pages - one is a Product Listings Page:
/*BUTTON STYLE*/
button {
background-color: #132257;
color: white;
border: 0.25em solid white;
cursor: pointer;
width: 100%;
height: 90%;
}
/*PRODUCT LISTINGS PAGE*/
.shoplayout {
grid-area: MC;
display: grid;
grid-template-columns: 1fr 4fr;
grid-template-areas: "filters listing";
}
.filters {
grid-area: filters;
}
.listing {
grid-area: listing;
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 0.75em;
}
.product {
width: 100%;
display: grid;
grid-template-columns: 1fr;
grid-template-rows: repeat(6, auto);
border: 0.125em solid #1b2493;
}
.product>* {
height: 8vh;
line-height: 8vh;
border-bottom: solid 0.02em #1b2493;
padding-left: 0.25em;
}
.product div:nth-child(even) {
/*Every second div within a product has the same background*/
background-color: lightsteelblue;
}
.productbuy {
display: grid;
grid-template-columns: repeat(4, 1fr);
text-align: center;
border: none;
}
.productpic {
grid-row: 1;
padding: 0;
border: none;
display: flex;
justify-items: center;
height: 20vh;
}
.productpic>img {
/*https: //stackoverflow.com/questions/3029422/how-do-i-auto-resize-an-image-to-fit-a-div-container*/
max-width: 100%;
max-height: 100%;
}
/*PRODUCT DETAILS PAGE*/
.productdetails {
grid-area: MC;
display: grid;
grid-template-columns: repeat(2, 1fr);
grid-template-rows: repeat(6, auto);
gap: 1em;
}
.productdetails>* {
border-bottom: solid 0.02em #1b2493;
padding-left: 0.25em;
}
.picdetail {
grid-column: 1 / span 2;
grid-row: 1;
background-color: cornsilk;
justify-items: center;
border: none;
}
.picdetail img {
max-width: 100%;
max-height: 100%;
}
.productnumbers {
display: grid;
grid-template-columns: repeat(4, 1fr);
text-align: center;
}
<!-- CONTENT -->
<div class="content" id="elemToSlide">
<div class="shoplayout">
<div class="listing">
<!--ALL PRODUCTS IN HERE-->
<div class="product">
<div>Ch. BonPlonque, 2019</div>
<div>Bordeaux, France</div>
<div>Crisp fresh white</div>
<div>3.21/5, 3 reviews</div>
<!--DISPLAY AS A 4 COL INNER GRID-->
<div class="productbuy">
<div>€15.95</div>
<div>Stock: 5</div>
<div><button>Buy</button></div>
<div>Qty</div>
</div>
</div>
<!--END LISTING COLUMNS-->
</div>
<!--END SHOP CONTENT-->
</div>
One is a Products Details Page:
/*BUTTON STYLE*/
button {
background-color: #132257;
color: white;
border: 0.25em solid white;
cursor: pointer;
width: 100%;
height: 90%;
}
/*PRODUCT LISTINGS PAGE*/
.shoplayout {
grid-area: MC;
display: grid;
grid-template-columns: 1fr 4fr;
grid-template-areas: "filters listing";
}
.filters {
grid-area: filters;
}
.listing {
grid-area: listing;
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 0.75em;
}
.product {
width: 100%;
display: grid;
grid-template-columns: 1fr;
grid-template-rows: repeat(6, auto);
border: 0.125em solid #1b2493;
}
.product>* {
height: 8vh;
line-height: 8vh;
border-bottom: solid 0.02em #1b2493;
padding-left: 0.25em;
}
.product div:nth-child(even) {
/*Every second div within a product has the same background*/
background-color: lightsteelblue;
}
.productbuy {
display: grid;
grid-template-columns: repeat(4, 1fr);
text-align: center;
border: none;
}
.productpic {
grid-row: 1;
padding: 0;
border: none;
display: flex;
justify-items: center;
height: 20vh;
}
.productpic>img {
/*https: //stackoverflow.com/questions/3029422/how-do-i-auto-resize-an-image-to-fit-a-div-container*/
max-width: 100%;
max-height: 100%;
}
/*PRODUCT DETAILS PAGE*/
.productdetails {
grid-area: MC;
display: grid;
grid-template-columns: repeat(2, 1fr);
grid-template-rows: repeat(6, auto);
gap: 1em;
}
.productdetails>* {
border-bottom: solid 0.02em #1b2493;
padding-left: 0.25em;
}
.picdetail {
grid-column: 1 / span 2;
grid-row: 1;
background-color: cornsilk;
justify-items: center;
border: none;
}
.picdetail img {
max-width: 100%;
max-height: 100%;
}
<!--,GRID CELL 5, 2 - DISPLAYS AS A 4 COL INNER GRID-->
<div class="productbuy">
<div>€15.95</div>
<div>Stock: 5</div>
<div><button>Buy</button></div>
<div>Qty</div>
</div>
Pretty much everything displays as I want with the exception of the button captioned BUY:
The HTML for the .productbuy element is identical in both cases, the CSS is identical in both cases 9with the exception of the nth child background-color), the button style applies across the entire website, so I am puzzled as to why this is happening.
Any and all suggestions gratefully received.
Thanks to both MD Code and Andrea who spotted that there must have been a problem with the parent containers. In the second case, the product details page, there was a div missing from the page structure, and the CSS for both pages was somewhat messy.
So I tidied things up and it works perfectly now.
Product Details Page HTML is now:
<!-- CONTENT -->
<div class="content" id="elemToSlide">
<!--DETAIL LAYOUT-->
<div class="detailslayout">
<!--A WINE-->
<div class="detailspic">
<img src="images/products/winehorizontallarge.png" />
</div>
<div>Name and vintage</div>
<div>Origin</div>
<div>Description</div>
<div>Reviews</div>
<div>Grapes</div>
<div>Producer</div>
<div class="detailsnumbers">
<div>TA</div>
<div>pH</div>
<div>RS</div>
<div>ABV</div>
</div>
<div class="productbuy">
<div>€15.95</div>
<div>Stock: 5</div>
<div><button>Buy</button></div>
<div>Qty</div>
</div>
<!--END WINE-->
</div>
<!--END DETAIL LAYOUT-->
</div>
<!--END CONTENT-->
Putting in the div "DetailsLayout" meant the page structure was correct; I then tidied up the CSS:
/*LAYOUT FOR THE PLP*/
.shoplayout {
grid-area: MC;
display: grid;
grid-template-columns: 1fr 4fr;
grid-template-areas: "filters listing";
}
/*LAYOUT FOR THE PDP - THIS WAS MISSING PREVIOUSLY*/
.detailslayout {
grid-area: MC;
display: grid;
grid-template-columns: repeat(2, 1fr);
grid-template-rows: repeat(6, auto);
gap: 1em;
}
.detailslayout > * {
height: 8vh;
line-height: 8vh;
}
.products {
width: 100%;
display: grid;
grid-template-columns: 1fr;
grid-template-rows: repeat(6, auto);
border: 0.125em solid #1b2493;
}
.products > * {
height: 8vh;
line-height: 8vh;
border-bottom: solid 0.02em #1b2493;
padding-left: 0.25em;
}
.products div:nth-child(even) {
background-color: lightsteelblue;
}
.productbuy {
display: grid;
grid-template-columns: repeat(4, 1fr);
text-align: center;
border: none;
background-color: lightsteelblue;
}
.productpic {
grid-row: 1;
padding: 0;
border: none;
display: flex;
justify-items: center;
height: 20vh;
}
.productpic img, .detailspic img {
max-width: 100%;
max-height: 100%;
}
.detailspic {
grid-column: 1 / span 2;
grid-row: 1 ;
background-color: cornsilk;
justify-items: center;
border: none;
height: 20vh;
}
.detailsnumbers {
display: grid;
grid-template-columns: repeat(4, 1fr);
text-align: center;
}
Now, on both pages the buttons render in the same way on the PLP:
And on the PDP:
Thanks to all for your help,
Dermot
The problem is not your style, button is working good but on details page your button's styles is according to the div parent change div parent style

CSS "grid-template-areas" not working as desired

I thought that when I type . (blank field) inside grid-template-areas property, .item5 will show inside under field. However it showing inside . field. Why does it works like that?
body {
margin: 0;
}
.item1 {
grid-area: header;
}
.item2 {
grid-area: content;
}
.item3 {
grid-area: left;
}
.item4 {
grid-area: footer;
}
.grid-container {
display: grid;
grid-template-columns: 300px 1fr;
grid-template-areas: "header header"
"content ."
"left ."
"footer footer"
"under under";
grid-gap: 10px;
background-color: #2196F3;
padding: 10px;
}
.grid-container > div {
background-color: rgba(255, 255, 255, 0.8);
text-align: center;
padding: 20px 0;
font-size: 30px;
}
<div class="grid-container">
<div class="item1">1</div>
<div class="item2">2</div>
<div class="item3">3</div>
<div class="item4">4</div>
<div class="item5">5</div>
</div>
From the specification:
A sequence of one or more "." (U+002E FULL STOP), representing a null cell token.
and
A null cell token represents an unnamed area in the grid container.
unnamed area doesn't mean that an item cannot be placed there. It simply mean unnamed and the automatic placement algorithm will start from the top to the bottom so the first empty area for item5 is an unnamed area.
You can follow the full algorithm here: https://www.w3.org/TR/css-grid-1/#auto-placement-algo where you will find no restriction about unnamed area or the named one. If your item5 was alone it will get placed into the first row/column (the one named "header")
body {
margin: 0;
}
.item1 {
grid-area: header;
}
.item2 {
grid-area: content;
}
.item3 {
grid-area: left;
}
.item4 {
grid-area: footer;
}
.grid-container {
display: grid;
grid-template-columns: 300px 1fr;
grid-template-areas: "header header"
"content ."
"left ."
"footer footer"
"under under";
grid-gap: 10px;
background-color: #2196F3;
padding: 10px;
}
.grid-container > div {
background-color: rgba(255, 255, 255, 0.8);
text-align: center;
padding: 20px 0;
font-size: 30px;
}
<div class="grid-container">
<div class="item5">5</div>
</div>
Related question where I am giving a more detailed explanation about the placement algorithm:
CSS Grid : How Auto placement algorithm works
Based on your last comment, I think that you are expecting to have grid-auto-flow: column;, but this is not the default style.
I added it to the snippet:
body {
margin: 0;
}
.item1 {
grid-area: header;
}
.item2 {
grid-area: content;
}
.item3 {
grid-area: left;
}
.item4 {
grid-area: footer;
}
.grid-container {
grid-auto-flow: column;
display: grid;
grid-template-columns: 300px 1fr;
grid-template-areas: "header header"
"content ."
"left ."
"footer footer"
"under under";
grid-gap: 10px;
background-color: #2196F3;
padding: 10px;
}
.grid-container > div {
background-color: rgba(255, 255, 255, 0.8);
text-align: center;
padding: 20px 0;
font-size: 30px;
}
<div class="grid-container">
<div class="item1">1</div>
<div class="item2">2</div>
<div class="item3">3</div>
<div class="item4">4</div>
<div class="item5">5</div>
</div>

CSS Grid Items not filling cell

When I say it doesnt fill the cell this is what I mean I am having trouble with a grid I created. The items placed in the grid don't fill the cell. All the items seem to be positioned correctly and it is responsive but they won't actually fill the cell. Sorry if this is so obvious, I'm a newbie to HTML and CSS. I think that my code is not completely optimized and could a lot shorter but that's the way I found :').
Here the HTML code
.about-grid {
display: grid;
height: 100vh;
grid-template-columns: 1fr 1fr 1fr 1fr;
grid-template-rows: 0.2fr 1.5fr 1.2fr 0.8fr;
grid-template-areas: "nav nav nav nav" "sidebar main main main" "sidebar content1 content2 content3" "sidebar footer footer footer";
grid-gap: 0.2rem;
font-weight: 800;
text-transform: uppercase;
font-size: 12px;
color: #004d40;
text-align: center;
}
.about-grid nav {
background: #a7ffeb;
grid-area: nav;
border-radius: var(--main-radius);
padding-top: var(--main-padding);
}
.about-grid main {
background: #84ffff;
grid-area: main;
border-radius: var(--main-radius);
padding-top: var(--main-padding);
}
.about-grid #sidebar {
background: #18ffff;
grid-area: sidebar;
border-radius: var(--main-radius);
padding-top: var(--main-padding);
}
.about-grid #content1 {
background: #6fffd2;
grid-area: content1;
border-radius: var(--main-radius);
padding-top: var(--main-padding);
}
.about-grid #content2 {
background: #64ffda;
grid-area: content2;
border-radius: var(--main-radius);
padding-top: var(--main-padding);
}
.about-grid #content3 {
background: #73ffba;
grid-area: content3;
border-radius: var(--main-radius);
padding-top: var(--main-padding);
}
.about-grid footer {
background: #1de9b6;
grid-area: footer;
border-radius: var(--main-radius);
padding-top: var(--main-padding);
}
.about-grid a {
text-align: center;
display: block;
font-family: inherit;
text-decoration: none;
font-weight: bold;
margin: 1rem;
}
#media only screen and (max-width: 550px) {
.about-grid {
grid-template-columns: 1fr;
grid-template-rows: 0.4fr 0.4fr 2.2fr 1.2fr 1.2fr 1.2fr 1fr;
grid-template-areas: "nav" "sidebar" "main" "content1" "content2" "content3" "footer";
}
}
<main class="about-bg">
<div class="about-grid">
<nav>Navbar</nav>
<main>Main</main>
<div id="sidebar">Sidebar</div>
<div id="content1">Content1</div>
<div id="content2">Content2</div>
<div id="content3">Content3</div>
<footer>Footer</footer>
</div>
</main>
I didn't quite understand your problem, but I saw a different problem in your code. When you fill all the cells with large text, then your grid will go beyond the screen. Perhaps you meant this problem. You need to specify the overflow-wrap: anywhere rule for the .about-grid selector. Now the contents of the cells will depend on the width of the cells themselves.
To check how overflow-wrap: anywhere works. Run this snippet and remove the overflow-wrap: anywhere rule from the .about-grid selector and you should see what I'm talking about.
body {
box-sizing: border-box;
margin: 0;
padding: 0;
}
.about-grid {
display: grid;
height: 100vh;
grid-template-columns: 1fr 1fr 1fr 1fr;
grid-template-rows: 0.2fr 1.5fr 1.2fr 0.8fr;
grid-template-areas: "nav nav nav nav" "sidebar main main main" "sidebar content1 content2 content3" "sidebar footer footer footer";
grid-gap: 0.2rem;
font-weight: 800;
text-transform: uppercase;
font-size: 12px;
color: #004d40;
text-align: center;
overflow-wrap: anywhere; /*add this it*/
}
.about-grid nav {
background: #a7ffeb;
grid-area: nav;
border-radius: var(--main-radius);
padding-top: var(--main-padding);
}
.about-grid main {
background: #84ffff;
grid-area: main;
border-radius: var(--main-radius);
padding-top: var(--main-padding);
}
.about-grid #sidebar {
background: #18ffff;
grid-area: sidebar;
border-radius: var(--main-radius);
padding-top: var(--main-padding);
}
.about-grid #content1 {
background: #6fffd2;
grid-area: content1;
border-radius: var(--main-radius);
padding-top: var(--main-padding);
}
.about-grid #content2 {
background: #64ffda;
grid-area: content2;
border-radius: var(--main-radius);
padding-top: var(--main-padding);
}
.about-grid #content3 {
background: #73ffba;
grid-area: content3;
border-radius: var(--main-radius);
padding-top: var(--main-padding);
}
.about-grid footer {
background: #1de9b6;
grid-area: footer;
border-radius: var(--main-radius);
padding-top: var(--main-padding);
}
.about-grid a {
text-align: center;
display: block;
font-family: inherit;
text-decoration: none;
font-weight: bold;
margin: 1rem;
}
#media only screen and (max-width: 550px) {
.about-grid {
grid-template-columns: 1fr;
grid-template-rows: 0.4fr 0.4fr 2.2fr 1.2fr 1.2fr 1.2fr 1fr;
grid-template-areas: "nav" "sidebar" "main" "content1" "content2" "content3" "footer";
}
}
<main class="about-bg">
<div class="about-grid">
<nav>Navbar</nav>
<main>Main</main>
<div id="sidebar">SidebarSidebarSidebarSidebarSidebarSidebarSidebarSidebarSidebarSidebarSidebarSidebarSidebarSidebarSidebarSidebarSidebarSidebarSidebarSidebarSidebarSidebarSidebarSidebarSidebarSidebarSidebarSidebarSidebarSidebarSidebarSidebarSidebarSidebarSidebarSidebarSidebarSidebarSidebarSidebarSidebarSidebarSidebarSidebarSidebarSidebarSidebarSidebarSidebar</div>
<div id="content1">Content1Content1Content1Content1Content1Content1Content1Content1Content1Content1Content1Content1Content1Content1Content1Content1Content1Content1Content1Content1Content1Content1Content1Content1Content1Content1Content1Content1Content1Content1Content1Content1Content1Content1Content1Content1Content1Content1Content1Content1Content1Content1Content1</div>
<div id="content2">Content2Content2Content2Content2Content2Content2Content2Content2Content2Content2Content2Content2Content2Content2Content2Content2Content2Content2Content2Content2Content2Content2Content2Content2Content2Content2Content2Content2Content2Content2Content2Content2Content2Content2Content2Content2Content2Content2Content2Content2Content2Content2Content2</div>
<div id="content3">Content3Content3Content3Content3Content3Content3Content3Content3Content3Content3Content3Content3Content3Content3Content3Content3Content3Content3Content3Content3Content3Content3Content3Content3Content3Content3Content3Content3Content3Content3Content3Content3Content3Content3Content3Content3Content3Content3Content3Content3Content3Content3</div>
<footer>Footer</footer>
</div>
</main>
Do you mean to remove the little margins between cells? If so, then setting grid-gap: 0 will do it:
Demo:
.about-grid
{
display: grid;
height: 100vh;
grid-template-columns: 1fr 1fr 1fr 1fr;
grid-template-rows: 0.2fr 1.5fr 1.2fr 0.8fr;
grid-template-areas:
"nav nav nav nav"
"sidebar main main main"
"sidebar content1 content2 content3"
"sidebar footer footer footer";
/*The only thing I changed, grid-gap*/
grid-gap: 0;
font-weight: 800;
text-transform: uppercase;
font-size: 12px;
color: #004d40;
text-align: center;
}
.about-grid nav {
background: #a7ffeb;
grid-area: nav;
border-radius: var(--main-radius);
padding-top: var(--main-padding);
}
.about-grid main {
background: #84ffff;
grid-area: main;
border-radius: var(--main-radius);
padding-top: var(--main-padding);
}
.about-grid #sidebar {
background: #18ffff;
grid-area: sidebar;
border-radius: var(--main-radius);
padding-top: var(--main-padding);
}
.about-grid #content1 {
background: #6fffd2;
grid-area: content1;
border-radius: var(--main-radius);
padding-top: var(--main-padding);
}
.about-grid #content2 {
background: #64ffda;
grid-area: content2;
border-radius: var(--main-radius);
padding-top: var(--main-padding);
}
.about-grid #content3 {
background: #73ffba;
grid-area: content3;
border-radius: var(--main-radius);
padding-top: var(--main-padding);
}
.about-grid footer {
background: #1de9b6;
grid-area: footer;
border-radius: var(--main-radius);
padding-top: var(--main-padding);
}
.about-grid a {
text-align: center;
display: block;
font-family: inherit;
text-decoration: none;
font-weight: bold;
margin: 1rem;
}
#media only screen and (max-width: 550px) {
.about-grid {
grid-template-columns: 1fr;
grid-template-rows: 0.4fr 0.4fr 2.2fr 1.2fr 1.2fr 1.2fr 1fr;
grid-template-areas:
"nav"
"sidebar"
"main"
"content1"
"content2"
"content3"
"footer";
}
}
<main class="about-bg">
<div class="about-grid">
<nav>Navbar</nav>
<main>Main</main>
<div id="sidebar">Sidebar</div>
<div id="content1">Content1</div>
<div id="content2">Content2</div>
<div id="content3">Content3</div>
<footer>Footer</footer>
</div>
</main>

Grid Layout failing to align

I am trying to achieve this, I want the nav and header tags to fill the left side.
html {
width: 100%;
overflow: hidden;
}
body {
display: grid;
grid-template-rows: 1fr 5fr 1fr;
grid-template-columns: 2fr 5fr 3fr;
grid-template-areas:
"header header"
"nav main"
"footer footer";
}
header {
background: yellowgreen;
}
nav {
background: lightblue;
}
main {
background: aliceblue;
}
footer {
background: lightgoldenrodyellow;
}
<header>Banner</header>
<nav>Navigation</nav>
<main>Main content</main>
<footer>Footer</footer>
You can try like this
.item1 { grid-area: header; }
.item2 { grid-area: menu; }
.item3 { grid-area: main; }
.item5 { grid-area: footer; }
.grid-container {
display: grid;
grid-template-areas:
'header main main main '
'menu main main main '
'menu footer footer footer ';
grid-gap: 10px;
background-color: #2196F3;
padding: 10px;
}
.grid-container > div {
background-color: rgba(255, 255, 255, 0.8);
text-align: center;
padding: 20px 0;
font-size: 30px;
}
<div class="grid-container">
<div class="item1">Header</div>
<div class="item2">Menu</div>
<div class="item3">Main</div>
<div class="item5">Footer</div>
</div>
Here's an example of how you could define grid-areas
html {
overflow: hidden;
}
body {
display: grid;
grid-template-rows: 25px 100px;
grid-template-columns: 80px 1fr;
grid-template-areas: "header main" "nav main" "nav footer";
}
body>header {
grid-area: header;
background-color: yellowgreen;
}
body>nav {
grid-area: nav;
background-color: lightblue;
}
body>main {
grid-area: main;
background-color: aliceblue;
}
body>footer {
grid-area: footer;
background-color: lightgoldenrodyellow;
}
<header>Banner</header>
<nav>Navigation</nav>
<main>Main content</main>
<footer>Footer</footer>

Sass mixin #content not letting grids working properly

I'm having an odd issue, I'm new to using the #mixin and #content tag, and sass in general for that matter, but for some reason, my grid-template-areas don't accurately represent what I want them to.
HTML
<header>
<div class="text"></div>
<nav>
<div class="about"></div>
<div class="qualities"></div>
<div class="portfolio"></div>
<div class="contact"></div>
</nav>
</header>
SCSS
$colors: (
red: red,
blue: blue
);
#function border($color) {
#return 3px map-get($colors, $color) solid;
}
#mixin desktop {
#media only screen and (min-width: 768px) {
#content;
}
}
header {
grid-area: header;
border: border(red);
display: grid;
grid-template-columns: 3fr 1fr;
grid-template-areas: "header-title nav";
.text {
grid-area: header-title;
border: 3px green solid;
}
nav {
grid-area: nav;
border: border(blue);
}
#include desktop {
grid-template-columns: 4fr 1fr 1fr 1fr 1fr;
grid-template-rows: 1fr;
grid-template-areas: "header-title header-about header-q header-port header-contact";
.about {
grid-area: header-about;
border: border(blue);
}
.qualities {
grid-area: header-q;
border: border(blue);
}
.portfolio {
grid-area: header-port;
border: border(blue);
}
.contact {
grid-area: header-contact;
border: border(blue);
}
}
}
To see this issue... jsfiddle
It should go from splitting the header into 2 columns to splitting the header into 5 columns;