惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

N
News and Events Feed by Topic
GbyAI
GbyAI
博客园 - Franky
宝玉的分享
宝玉的分享
Blog — PlanetScale
Blog — PlanetScale
Google DeepMind News
Google DeepMind News
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
人人都是产品经理
人人都是产品经理
Microsoft Azure Blog
Microsoft Azure Blog
The Register - Security
The Register - Security
腾讯CDC
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
I
InfoQ
The Cloudflare Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Engineering at Meta
Engineering at Meta
MongoDB | Blog
MongoDB | Blog
有赞技术团队
有赞技术团队
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
F
Full Disclosure
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Vercel News
Vercel News
博客园 - 【当耐特】
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
S
Schneier on Security
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Project Zero
Project Zero
量子位
M
MIT News - Artificial intelligence
Stack Overflow Blog
Stack Overflow Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
美团技术团队
Attack and Defense Labs
Attack and Defense Labs
C
Cybersecurity and Infrastructure Security Agency CISA
T
The Blog of Author Tim Ferriss
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
T
Troy Hunt's Blog
Google Online Security Blog
Google Online Security Blog
罗磊的独立博客
P
Proofpoint News Feed
Schneier on Security
Schneier on Security
Spread Privacy
Spread Privacy
S
SegmentFault 最新的问题
L
LINUX DO - 最新话题
Simon Willison's Weblog
Simon Willison's Weblog
爱范儿
爱范儿
博客园 - 聂微东
A
About on SuperTechFans
PCI Perspectives
PCI Perspectives
D
Docker

CSS Articles by Temani Afif

Get Ready For the Powerful CSS border-shape Property! | CSS-Tricks Let’s Play With Gap Decorations! How to Control Infinite CSS Animations (Part 2 of 2) How to Control Infinite CSS Animations (Part 1 of 2) Two Circles, One Arrow, and Anchor Positioning Making a Responsive Pyramidal Grid With Modern CSS | CSS-Tricks How to Create a CSS-only Elastic Text Effect Making Complex CSS Shapes Using shape() | CSS-Tricks Responsive Hexagon Grid Using Modern CSS | CSS-Tricks Responsive List of Avatars Using Modern CSS (Part 2) | CSS-Tricks Responsive List of Avatars Using Modern CSS (Part 1) | CSS-Tricks Perfectly Pointed Tooltips: To The Corners Perfectly Pointed Tooltips: All Four Sides Perfectly Pointed Tooltips: A Foundation Sequential linear() Animation With N Elements | CSS-Tricks Infinite Marquee Animation using Modern CSS Better CSS Shapes Using shape() — Part 4: Close and Move | CSS-Tricks Drawing CSS Shapes using corner-shape Better CSS Shapes Using shape() — Part 3: Curves | CSS-Tricks Better CSS Shapes Using shape() — Part 2: More on Arcs | CSS-Tricks Better CSS Shapes Using shape() — Part 1: Lines and Arcs | CSS-Tricks Creating Blob Shapes using clip-path: shape() Creating Flower Shapes using clip-path: shape() Custom progress element using the attr() function A CSS-Only Star Rating Component and More! (Part 2) | CSS-Tricks A CSS-Only Star Rating Component and More! (Part 1) | CSS-Tricks How to Create Wavy Boxes Using CSS Full-Bleed Layout with Modern CSS Fancy Menu Navigation Using Anchor Positioning | CSS-Tricks How to Create a Zig-Zag Box Using CSS How to Create Zig-Zag CSS Loaders Using One Element Custom Progress Element Using Anchor Positioning & Scroll-Driven Animations How to Create Filling CSS Loaders Using One Element How to Create Curved-Edge and Rounded-Edge Shapes Using CSS CSS Tricks That Use Only One Gradient | CSS-Tricks How to create Shapes with Inner Curves using CSS Mask Custom Range Slider Using Anchor Positioning & Scroll-Driven Animations How to Get the Width/Height of Any Element in Only CSS How Keyboard Navigation Works in a CSS Game How To Create Cut-Out Shapes using The clip-path property The Modern Guide For Making CSS Shapes — Smashing Magazine css-shape.com: The Ultimate Collection of CSS-only Shapes ⚡️ Sliding 3D Image Frames In CSS — Smashing Magazine CSS Tricks To Master The clip-path Property Creating Wavy Circles with Fancy Animations in CSS Modern CSS Tooltips And Speech Bubbles (Part 2) — Smashing Magazine Do you need a Tooltip or a Speech Bubble? I have created 100 using CSS 😲 Modern CSS Tooltips And Speech Bubbles (Part 1) — Smashing Magazine Creating Flower Shapes using CSS Mask & Trigonometric Functions CSS Shapes: The Triangle The Complex But Awesome CSS border-image Property — Smashing Magazine CSS Tricks to add 3D Effects to your Text CSS Responsive Multi-Line Ribbon Shapes (Part 2) — Smashing Magazine CSS Responsive Multi-Line Ribbon Shapes (Part 1) — Smashing Magazine I have made 100+ CSS-only Ribbon Shapes | The Perfect Collection 🎀 How to create a CSS-only infinite scroll animation Re-Creating The Pop-Out Hover Effect With Modern CSS (Part 2) — Smashing Magazine Re-Creating The Pop-Out Hover Effect With Modern CSS (Part 1) — Smashing Magazine How to Create CSS Ribbon Shapes with a Single Element CSS Shapes: The Ribbon Revealing Images With CSS Mask Animations — Smashing Magazine css-loaders.com: The Biggest Collection of Loading Animations (more than 500 🤯) How To Define An Array Of Colors With CSS — Smashing Magazine CSS effects on images II Shines, Perspective, And Rotations: Fancy CSS 3D Effects For Images — Smashing Magazine What's your Lucky CSS Pattern? Check It Now! How to Add a CSS Reveal Animation to Your Images I created 100+ unique CSS patterns | The best collection 🤩 How to Create a Custom Range Slider Using CSS CSS Shapes: The Heart CSS Shapes: Polygon & Starburst A Text Reveal Animation using CSS CSS Tip: learn CSS the easy way! How to build a CSS-only accordion How to create Breadcrumb Navigation with CSS Different Ways to Get CSS Gradient Shadows | CSS-Tricks A CSS-only responsive Stepper component A Fancy Hover Effect For Your Avatar | CSS-Tricks How to make a zoom effect using CSS Creating a Custom Cursor using CSS CSS Infinite 3D Sliders | CSS-Tricks CSS Infinite Slider Flipping Through Polaroid Images | CSS-Tricks How to create a responsive sidebar menu using CSS CSS Infinite and Circular Rotating Image Slider | CSS-Tricks Making Static Noise From a Weird CSS Gradient Bug | CSS-Tricks CSS Grid and Custom Shapes, Part 3 | CSS-Tricks Fancy Image Decorations: Outlines and Complex Animations | CSS-Tricks Fancy Image Decorations: Masks and Advanced Hover Effects | CSS-Tricks Fancy Image Decorations: Single Element Magic | CSS-Tricks How to create an infinite image slider using CSS How to Create Wavy Shapes & Patterns in CSS | CSS-Tricks How I Made a Pure CSS Puzzle Game | CSS-Tricks How to create a Tooltip/Speech Bubble using CSS CSS Grid and Custom Shapes, Part 2 | CSS-Tricks CSS Grid and Custom Shapes, Part 1 | CSS-Tricks Zooming Images in a Grid Layout | CSS-Tricks How to create a CSS-only loader with one element Exploring CSS Grid’s Implicit Grid and Auto-Placement Powers | CSS-Tricks How to create background pattern using CSS & conic-gradient Single Element Loaders: The Bars | CSS-Tricks
Modern Layouts using CSS Grid
2023-06-10 · via CSS Articles by Temani Afif

Modern Layouts using CSS Grid

Written on by Temani AfifWeb Developer

Estimated read time 5 minutes

In this article, we will use CSS Grid to build some common layouts. The goal is to combine modern CSS tricks to create such layouts with the smallest code possible. In the end, you will have a collection of CSS boilerplate that you can easily reuse in your projects.

Overview of the modern CSS layouts

Full-Screen Container

Let’s start with our first layout which is the most basic one: Having a full-screen container. There are already a lot of codes where you have those cascade height: 100% to achieve such an effect or the use of height: 100vh.

We are not going to do this. Our code is as simple as the following:

html {
  display: grid;
  min-height: 100%;
}
main {
  height: 100%;
}

No cascading height declaration, no use of 100vh, and it does work with the default body margin. Not only this, but the container can also grow if its content is bigger than the screen height. In other words, the above code will set the minimum height to be equal to the screen height.

Here is a demo to illustrate. You can toggle the visibility of the content inside main to see how the height grows to fit with no overflow issue.

See the Pen by Temani Afif () on .

How does it work? What is the logic behind the code?

First, we make the html as big as the Viewport using min-height: 100% and not height :100% so it can grow if there is more content. Then, by using display: grid we rely on the default stretch alignment that will make the body element fill all the width/height of the html element while respecting its default margin. This is very useful as we can easily have padding/margin without affecting the full-screen behavior.

Now that we have our body element stretched to fill all the space, we use height: 100% on the main element to fill that same space and have a full screen container!

You are probably wondering why we are able to use height: 100% when there is no explicit height on the body element. This is the magic part of using CSS Grid and the default stretch alignment.

From the specification you can read:

Note: Since formulas calculated using only definite sizes, such as the stretch fit formula, are also definite, the size of a grid item which is stretched is also considered definite.

The size of a stretched element is considered definite so a child element can safely use percentage height.

In addition to height: 100%, we can also make the body a grid container, and the main element will stretch inside it. Two different codes for the same layout.

html {
  display: grid;
  min-height: 100%;
}
body {
  display: grid;
}

Worth noting that such configuration allows us to easily have centered content which is also a starter layout commonly used.

40%

💰 40% OFF YOUR FIRST MONTH WITH VERPEX MANAGED CLOUD SERVERS

with the discount code

SERVERS-SALE

Let’s add a footer and header to our previous layout and make sure the main content takes the remaining height. This is also a common layout where we want the footer to stick to the bottom of the page if we have small content.

For this, we will turn the body element into a grid layout with the following code:

html {
  display: grid;
  min-height: 100%;
}
body {
  display: grid;
  grid-template-rows: auto 1fr auto;
}

We define three rows where the middle one takes all the remaining height using 1fr. As simple as that.

See the Pen by Temani Afif () on .

This configuration will push the footer out of view if you have a lot of content inside main and you will get a scrollbar on the whole page.

We can have a different configuration where we move the scrollbar to the main element and keep the footer always visible.

Instead of using min-height, we use height on the html element. Then we add min-height: 0 to the body element which is the trickiest part here. CSS grid (and also Flexbox) define a default min-height (and min-width) to child items as we can read in the specification:

To provide a more reasonable default minimum size for grid items, the used value of its automatic minimum size in a given axis is the content-based minimum size if all of the following are true: It is not a scroll container It spans at least one track in that axis whose min track sizing function is auto If it spans more than one track in that axis, none of those tracks are flexible Otherwise, the automatic minimum size is zero, as usual.

I know, the above is not intuitive and difficult to understand but always keep in mind this trick. You may need to add min-height: 0 or min-width: 0 in some cases where you face a strange overflow issue or you want an element to collapse/shrink even if it has a lot of content.

Finally, we add overflow: auto to the main element and we have another configuration where the footer is always visible and we can scroll the main content.

html {
  display: grid;
  height: 100%;
}
body {
  display: grid;
  min-height: 0;
  grid-template-rows: auto 1fr auto;
}
main {
  overflow: auto;
}

Worth noting that even using this configuration we can keep the default body margin. No more headaches trying to remove that extra margin coming from nowhere!

Now let’s add a sidebar to our layout. In addition to the three rows, we will have two columns.

html {
  display: grid;
  height: 100%; /* OR min-height: 100% */
}
body {
  display: grid;
  min-height: 0;
  grid-template-rows: auto 1fr auto;
  grid-template-columns: 200px 1fr; /* 2 columns */
}
header,
footer {
  grid-column: 1/-1; /* header and footer takes both columns */
}
main {
  overflow: auto;
}

I used 200px to set the width of the sidebar but you can also use a percentage value or even auto.

We need to make the header and the footer span both columns using grid-column: 1/-1 which means starting at the first column and ending at the last one.

We can also create a sticky sidebar if you are using the configuration where we have the scrollbar on the whole page (min-height instead of height)

It’s important to notice the usage of align-self: start to disable the default stretch alignment and allow the sidebar to have a sticky behavior:

aside {
  position: sticky;
  top: 8px;
  align-self: start;
}

40%

💰 40% OFF YOUR FIRST MONTH WITH VERPEX MANAGED HOSTING PLANS FOR WORDPRESS

with the discount code

SERVERS-SALE

What if the sidebar was optional? What if we want a right sidebar instead of a left one? We can write a conditional code that adjusts the CSS based on the HTML structure. We have 3 cases:

  1. The sidebar doesn’t exist.

  2. If the sidebar exists, it’s before the main element (left sidebar)

  3. If the sidebar exists, it’s after the main element (right sidebar)

For this we are going to use the :has() selector.

html {
  display: grid;
  height: 100%; /* OR min-height: 100% */
}
body {
  display: grid;
  min-height: 0;
  grid-template-rows: auto 1fr auto;
}
body:has(main + aside) {
  grid-templat-columns: 1fr 200px;
}
body:has(aside + main) {
  grid-templat-columns: 200px 1fr;
}
header,
footer {
  grid-column: 1/-1;
}
main {
  overflow: auto;
}

By default, we don’t define any columns (there is no sidebar). When the aside element exists and is placed after the main element body:has(main + aside) we define two columns where the second one will have the width of the sidebar. When the aside element exists and is before the main element we do the opposite. We start by defining the width of the sidebar.

You can try all the combination and it works for all of them

See the Pen by Temani Afif () on .

The same can be done with the footer and header if you want to consider them as optional. I won’t give you the code, I let you try this alone as a small exercise to practice what we covered until now.

Equal height sections

We all wanted to have all those sections equal in height and more precisely equal to the tallest one among them. This can be done using one line of code:

body {
  display: grid;
  grid-auto-rows: 1fr;
}

That’s all!

See the Pen by Temani Afif () on .

All the sections are equal in height to the tallest one.

What about having equal height sections that are also full-screen? We can combine the code of the first layout with this last trick to achieve it

html {
  display: grid;
  height: 100%;
  overflow: hidden; /* disable the scroll on the page */
}
body {
  display: grid;
  grid-auto-rows: 100%; /* all rows equal in height and take all the space */
  overflow: auto; /* Let's have the scroll on the body element */
}

I am still keeping the default body margin to illustrate that the code works fine even with it but in most of the case you will want to remove it

I am also adding a touch of scroll snap in the last demo to enhance the scrolling experience.

We can make the code more generic and introduce a variable to control the number of visible sections at a time. All the sections are full height so we see only one but we can control this by changing the grid-auto-rows like below:

body {
  --n: 2; /* the number of sections */
  --g: 8px; /* the gap */

  gap: var(--g);
  grid-auto-rows: calc((100% - (var(--n) - 1)*var(--g))/var(--n));
}

Intuitively we should use 100%/n to define the height of the sections but we have to consider the gap in the calculation that’s why we need to remove n - 1 gaps from the total height before dividing by n. When two sections are visible we will have one gap visible as well.

See the Pen by Temani Afif () on .

Vertical Layout

We can take the previous layout and turn it into a vertical one with simple changes:

html {
  display: grid;
  height: 100%;
  overflow: hidden;
}
body {
  --n: 2;   /* the number of sections */
  --g: 1em; /* the gap */
  display: grid;
  gap: var(--g);
  grid-auto-flow: column; /* we define a column layout, the default was row */
  /* We use "grid-auto-columns" instead of "grid-auto-rows" */
  grid-auto-columns: calc((100% - (var(--n) - 1)*var(--g))/var(--n));
  overflow: auto;
}

Another layout with almost the same code structure

See the Pen by Temani Afif () on .

We can also define rows and get another fancy layout

grid-template-rows: repeat(2,1fr);

The same can be done with the horizontal version. Go back to the code and try to do the same. Yes, another exercise for you!

Complex Layout

In this section, I won’t introduce a new layout but I will show you how easy is to combine all the previous code to create a more complex layout. We can have a header, a footer, an optional sidebar, scrollable sections inside the main element, and everything full screen.

See the Pen by Temani Afif () on .

I also added a few media queries to make things responsive and to illustrate how easily we can update the layout by adjusting a few values. I let you dig into the code and you will see that all it takes is a few variable changes to update the layout.

Responsive Grid of images

You probably already know this one but let’s add it to the collection since it’s a common layout.

body {
  display: grid;
  grid-template-columns: repeat(auto-fill,minmax(150px,1fr));
}
img {
  aspect-ratio: 1; /* change this if you don't want square images */
  width: 100%;
  height: auto;
  object-fit: cover;
}

See the Pen by Temani Afif () on .

In the above, I am using images with different sizes to illustrate that all of them will end up having the same size defined by the grid.

40%

💰 40% OFF YOUR FIRST MONTH WITH VERPEX MANAGED CLOUD SERVERS

with the discount code

SERVERS-SALE

Temani Afif

Temani Afif is an expert web developer, a content creator, and a CSS addict. He is the mastermind behind CSS Loaders, CSS Generators, CSS Tip and many other CSS-related websites.

View more posts by Temani Afif