The objective is to insert side margins for wider screens, while keeping the header span the entire width.
Normally we'd write
.inner {
margin: 0 5%;
}
to get such margins, but it turns out that HTML grids are so flexible that they make side margins possible through dead grid DIVs.
But somehow using dead DIVs does not seem quite right. Is there a way to obtain side margins within a grid. I see how this can be done with a blend of flex and grid. Here I'm wondering if it can be done with grids alone.
body {
height: 100vh; margin: 0; display: flex;
}
.outer{
margin: 5px; border: 5px; padding: 5px;
display: flex;
flex-grow: 1;
}
.inner {
flex-grow: 1;
margin: 5px; border: 5px; padding: 5px; grid-gap: 5px;
display: grid;
grid-template-rows: 100px 5fr 100px;
grid-template-columns: 1fr;
grid-template-areas: "header" "content" "side";
}
#media screen and (min-width: 600px) {
.inner {
grid-template-rows: 100px 6fr;
grid-template-columns: 5fr 100px;
grid-template-areas:
"header header"
"content side";
}
}
#media screen and (min-width: 800px) {
.inner {
grid-template-rows: 100px 6fr;
grid-template-columns: 1fr 5fr 100px 1fr;
grid-template-areas:
"header header header header"
"leftmargin content side rightmargin";
}
}
.box {
padding: 10px; margin: 5px;
border: 5px solid #444;
background-color: #eee;
font-size: 150%;
position: relative;
}
.header { grid-area: header; }
.content { grid-area: content; }
.side { grid-area: side; }
.leftmargin { grid-area: leftmargin; }
.rightmargin { grid-area: rightmargin; }
<div class="outer">
<div class="inner">
<div class="box header">Header</div>
<div class="box content">Content</div>
<div class="box side">Side</div>
</div>
</div>
Use dots (.) to declare empty grid areas:
grid-template-areas:
"header header header header"
". content side .";
Example:
body {
height: 100vh;
margin: 10px;
}
.inner {
display: grid;
grid-template-rows: 100px 5fr 100px;
grid-template-columns: 1fr;
grid-template-areas: "header" "content" "side";
grid-gap: 5px;
}
#media screen and (min-width: 600px) {
.inner {
grid-template-rows: 100px 6fr;
grid-template-columns: 5fr 100px;
grid-template-areas:
"header header"
"content side";
}
}
#media screen and (min-width: 800px) {
.inner {
grid-template-rows: 100px 6fr;
grid-template-columns: 5% 5fr 100px 5%;
grid-template-areas:
"header header header header"
". content side .";
}
}
.box {
padding: 10px; margin: 5px;
border: 5px solid #444;
background-color: #eee;
font-size: 150%;
position: relative;
}
.header { grid-area: header; }
.content { grid-area: content; }
.side { grid-area: side; }
<div class="inner">
<div class="box header">Header</div>
<div class="box content">Content</div>
<div class="box side">Side</div>
</div>
Related
here's my html code
<div>
<div id="navbar" class="box">Navbar</div>
<div id="sidenav " class="box">Side Navbar</div>
<div id="main " class="box">Main</div>
<div id="footer " class="box">Footer</div>
</div>
and here's my scss code
div{
display: grid;
width:100%;
height: 100%;
grid-template-columns: 25% 75% 25%;
grid-gap: 15px;
grid-template-rows:25% 50% 25% ;
// grid-gap: 15px;
grid-template-areas:
"hd hd hd "
"sd ma ma "
"ft ft ft ";
.box{
display: flex;
// text-align: center;
border: 3px solid red;
/* width:150px;
height: 150px; */
margin: auto;
align-items: center;
justify-content: center;
}
#navbar{
grid-area: hd;
}
#sidenav{
grid-area: sd;
}
#main{
grid-area: ma;
}
#footer{
grid-area: ft;
}
}
the problem is the footer div doesn't display in the bottom here's a screenshot
what i want is to make the footer display at the bottom so what seems to be the problem here
There are some issues that I found in your code which makes the layout little wonky.
grid-template-columns: 25% 75% 25%;
The column total is more than 100%, so it will not work perfectly.
I would highly recommend you to use a CSS grid generator online like https://grid.layoutit.com/
For your layout, I would also not recommend structure 3x3 (columns and rows) - As from the image you shared above it looks like the following
1 row - For "Navbar" (this doesnt need any sub columns)
1 row - For Content -> this has 2 columns 1 for "SideNav" and 1 for "Main"
1 row - For "Footer" (again you dont need sub columns)
Based on this your HTML structure will end up looking like
<div class="container">
<div class="navbar">Navbar</div>
<div class="Content">
<div class="SideNav">Side Nav</div>
<div class="Main">Main</div>
</div>
<div class="Footer">Footer</div>
</div>
And your CSS will look like this
body{
padding: 0;
margin: 0;
}
.container {
width: 100vw;
height: 100vh;
display: grid;
grid-template-columns: 1fr;
grid-template-rows: 25% 50% 25%;
gap: 0px 0px;
grid-template-areas:
"navbar"
"Content"
"Footer";
}
.navbar {
grid-area: navbar;
background-color: #f5f5f5;
padding: 16px;
text-align: center;
}
.Content {
display: grid;
grid-template-columns: 360px 1fr;
grid-template-rows: 1fr;
gap: 0px 0px;
grid-template-areas:
"SideNav Main";
grid-area: Content;
}
.SideNav {
grid-area: SideNav;
background-color: #e5e5e5;
padding: 16px;
}
.Main {
grid-area: Main;
background-color: salmon;
padding: 16px;
}
.Footer {
grid-area: Footer;
background-color: #d5d5d5;
padding: 16px;
text-align: center;
}
Here, if you check the code well, the container has 3 rows (25% - navbar, 50% - content, 25% - footer)
And then content has 2 columns (360px - Sidenav, 1fr - Main)
Hope this helps :)
You can also see the code live on my codepen : https://codepen.io/raunaqpatel/pen/WNyQqmm
Or here:
body{
padding: 0;
margin: 0;
}
.container {
width: 100vw;
height: 100vh;
display: grid;
grid-template-columns: 1fr;
grid-template-rows: 25% 50% 25%;
gap: 0px 0px;
grid-template-areas:
"navbar"
"Content"
"Footer";
}
.navbar {
grid-area: navbar;
background-color: #f5f5f5;
padding: 16px;
text-align: center;
}
.Content {
display: grid;
grid-template-columns: 360px 1fr;
grid-template-rows: 1fr;
gap: 0px 0px;
grid-template-areas:
"SideNav Main";
grid-area: Content;
}
.SideNav {
grid-area: SideNav;
background-color: #e5e5e5;
padding: 16px;
}
.Main {
grid-area: Main;
background-color: salmon;
padding: 16px;
}
.Footer {
grid-area: Footer;
background-color: #d5d5d5;
padding: 16px;
text-align: center;
}
<div class="container">
<div class="navbar">Navbar</div>
<div class="Content">
<div class="SideNav">Side Nav</div>
<div class="Main">Main</div>
</div>
<div class="Footer">Footer</div>
</div>
The grid-template-areas is being used here. However, the main area is not taking up the rest of the area, and I dont need aside for my project.
How can I make the main area take up the rest of the area?
.container {
display: grid;
grid-template-areas: "header header header" "nav content side" "footer footer footer";
grid-template-columns: 200px 1fr 200px;
grid-template-rows: auto 1fr auto;
grid-gap: 10px;
height: 100vh;
}
header {
grid-area: header;
border: 1px solid #61dafb;
}
nav {
grid-area: nav;
margin-left: 0.5rem;
border: 1px solid red;
}
main {
grid-area: content;
border: 1px solid blue;
}
footer {
grid-area: footer;
border: 1px solid black;
}
#media (max-width: 768px) {
.container {
grid-template-areas: "header" "nav" "content" "side" "footer";
grid-template-columns: 1fr;
grid-template-rows: auto/* Header */
minmax(75px, auto)/* Nav */
1fr/* Content */
minmax(75px, auto)/* Sidebar */
auto;
/* Footer */
}
nav,
aside {
margin: 0;
}
}
header {
grid-area: header;
display: flex;
justify-content: space-between;
align-items: center;
}
<div class="container">
<header>
<div>Header</div>
</header>
<nav>
<div>nav</div>
</nav>
<main>
</main>
<footer>
<div>Footer</div>
</footer>
</div>
Attaching JSfiddle https://jsfiddle.net/fgmwe281/2/
I am trying to use grid-template-areas for my boilerplate code for layout. However, unable to get the main content to take up the space on right(the area for aside).
I would skip grid-template-areas completely. It is nice for beginners as it visually displays the areas but overall it increases the size of necessary code. On top of that, it is easier to just skip it and letting the header and the footer span the entire with by using grid-column: 1 / -1;.
If you change the grid-template-columns to min-content auto min-content then the sidebar and the navigation will only consume as much space as needed. In this case, I sued the width on the containing div. If it exist then it will consume 200px width, if it doesn't, then it will consume no space:
body {
margin: 0;
}
.container {
display: grid;
grid-template-columns: min-content auto min-content;
grid-gap: 10px;
min-height: 100vh;
}
header,
footer {
grid-column: 1 / -1;
}
nav > div,
aside > div {
width: 200px;
}
header {
display: flex;
justify-content: space-between;
align-items: center;
}
#media only screen
and (max-width: 768px) {
.container {
grid-template-columns: 1fr;
grid-template-rows: auto auto 1fr auto auto;
}
}
#media only screen
and (min-width: 769px) {
.container {
grid-template-rows: auto 1fr auto;
}
}
header {
border: 1px solid #61dafb;
}
nav {
border: 1px solid red;
}
main {
border: 1px solid blue;
}
footer {
border: 1px solid black;
}
<div class="container">
<header>
<div>Header</div>
</header>
<nav>
<div>nav</div>
</nav>
<main>
<div>main</div>
</main>
<aside></aside>
<footer>
<div>Footer</div>
</footer>
</div>
I have a grid layout which can be simplified like this.
What I want to achieve is the .side should take whatever space the content needs to by default using min-content, but user can enlarge it by increasing the --size custom css property.
I tried calc(min-content + var(--size)) but it doesn't work. I can't assign a specific value like calc(100px + var(--size)) since the original size should be determined by its content.
What is the best approach to achieve such a feature?
* {
box-sizing: border-box;
}
body {
overflow: hidden;
margin: 0;
}
.container {
--size: 10px;
width: 100vw;
height: 100vh;
display: grid;
grid-template-areas:
"l t"
"l b";
/* doesn't work */
/* grid-template-columns: calc(min-content + var(--size)) 1fr; */
grid-template-columns: min-content 1fr;
grid-template-rows: 1fr 1fr;
}
.container > * {
border: solid 1px black;
}
.side {
grid-area: l;
}
.top {
grid-area: t;
}
.bottom {
grid-area: b;
}
<section class="container">
<div class="side">This should have a 10px gutter on the right</div>
<div class="top">Top</div>
<div class="bottom">Bottom</div>
</section>
Use width:calc(100% + var(--size)) for the grid item. This will create an overflow that you can rectify by adding a gap:
* {
box-sizing: border-box;
}
body {
overflow: hidden;
margin: 0;
}
.container {
--size: 20px;
height: 100vh;
display: grid;
grid-template-areas:
"l t"
"l b";
/* doesn't work */
/* grid-template-columns: calc(min-content + var(--size)) 1fr; */
grid-template-columns: min-content 1fr;
grid-template-rows: 1fr 1fr;
column-gap:var(--size);
}
.container > * {
border: solid 1px black;
}
.side {
grid-area: l;
width:calc(100% + var(--size));
}
.top {
grid-area: t;
}
.bottom {
grid-area: b;
}
<section class="container">
<div class="side">This should have a 10px gutter on the right</div>
<div class="top">Top</div>
<div class="bottom">Bottom</div>
</section>
I want the area in red to always fit the content so that the area below (the commenting section) is always right after and not down below.
In chrome, it works, but not in Firefox (see picture).
I thought that by adding grid-template-rows: max-content; it will make it happen, but apparently not.
So, how to make the red area always fit the content so the commenting area is right after.
https://jsfiddle.net/mjb7cehy/
HTML and CSS
.gridAB {
display: grid;
grid-template-columns: 1fr;
grid-template-rows: auto;
grid-template-areas: "body" "aside" "comment";
grid-gap: 40px;
}
#media screen and (min-width: 768px) {
.gridAB {
grid-template-columns: 2fr 1fr;
grid-template-rows: max-content;
grid-template-areas: "body aside" "comment aside";
}
}
.gridAB .aside {
grid-area: aside;
display: grid;
grid-template-columns: 1fr;
grid-template-rows: auto;
grid-gap: 20px;
align-content: flex-start;
}
.gridAB .body {
grid-area: body;
background-color: red;
}
.gridAB .album {
background-color: pink;
}
.gridAB .credit {
background-color: green;
}
.gridAB .version {
background-color: yellow;
}
.gridAB .comment {
grid-area: comment;
background-color: #eee;
}
<div class="gridAB">
<div class="body">body goes here</div>
<div class="aside">
<div class="album">album</div>
<div class="credit">credits</div>
<div class="version">version</div>
</div>
<div class="comment">comments go here</div>
</div>
Try
grid-template-rows: 0fr 1fr;
will work: Please find updated example link: jsfiddle
I'm starting to use CSS grid. So far, so good. I want some grid areas NOT to expand when other areas do.
This is what i have now
I'm designing mobile first, then desktop. The grid on the desktop, if you notice, the 'album' area expands when the body expands. I don't want that. I want the areas 'album', 'credits', 'version' to retain the height even if the 'body' or the 'comment' area expand. In other words, when a grid area expands, the other areas height remain intact.
https://jsfiddle.net/e9n4ac5d/
.grid {
display: grid;
grid-template-columns: 1fr;
grid-template-rows: auto;
grid-template-areas: "body" "album" "credit" "version" "comment";
}
#media screen and (min-width: 400px) {
.grid {
grid-template-columns: 2fr 1fr;
grid-template-rows: auto;
grid-template-areas: "body album" "comment credit" "comment version";
}
}
.body {
grid-area: body;
background-color: red;
}
.album {
grid-area: album;
background-color: pink;
}
.credit {
grid-area: credit;
background-color: green;
}
.version {
grid-area: version;
background-color: yellow;
}
.comment {
grid-area: comment;
background-color: #eee;
}
<div class="grid">
<div class="body">body
<br><br><br><br><br><br>
</div>
<div class="album">album</div>
<div class="credit">credits</div>
<div class="version">version</div>
<div class="comment">comments</div>
</div>
You can change your HTML structure like this:
.grid {
display: grid;
grid-template-columns: 1fr;
grid-template-rows: auto;
grid-template-areas: "body" "album" "credit" "version" "comment";
}
#media screen and (min-width: 400px) {
.right {
grid-area: right;
}
.grid {
grid-template-columns: 2fr 1fr;
grid-template-rows: auto;
grid-template-areas: "body right" "comment right";
}
}
.body {
grid-area: body;
background-color: red;
}
.album {
grid-area: album;
background-color: pink;
height: 50px;
}
.credit {
grid-area: credit;
background-color: green;
height: 50px;
}
.version {
grid-area: version;
background-color: yellow;
height: 50px;
}
.comment {
grid-area: comment;
background-color: #eee;
}
<div class="grid">
<div class="body">body
<br><br><br><br><br><br>
</div>
<div class="right">
<div class="album">album</div>
<div class="credit">credits</div>
<div class="version">version</div>
</div>
<div class="comment">comments</div>
</div>
Just set fixed height and width... To make it non expandable and non compressible...