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

推荐订阅源

U
Unit 42
P
Proofpoint News Feed
The Last Watchdog
The Last Watchdog
S
Secure Thoughts
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
N
News | PayPal Newsroom
Application and Cybersecurity Blog
Application and Cybersecurity Blog
O
OpenAI News
S
Security @ Cisco Blogs
宝玉的分享
宝玉的分享
Hacker News: Ask HN
Hacker News: Ask HN
T
Troy Hunt's Blog
Google Online Security Blog
Google Online Security Blog
WordPress大学
WordPress大学
有赞技术团队
有赞技术团队
TaoSecurity Blog
TaoSecurity Blog
Help Net Security
Help Net Security
Latest news
Latest news
NISL@THU
NISL@THU
S
Security Affairs
博客园_首页
C
CXSECURITY Database RSS Feed - CXSecurity.com
博客园 - 聂微东
AI
AI
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Recent Announcements
Recent Announcements
P
Privacy & Cybersecurity Law Blog
小众软件
小众软件
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Hugging Face - Blog
Hugging Face - Blog
博客园 - 司徒正美
AWS News Blog
AWS News Blog
W
WeLiveSecurity
Google DeepMind News
Google DeepMind News
I
InfoQ
Schneier on Security
Schneier on Security
Recent Commits to openclaw:main
Recent Commits to openclaw:main
T
The Exploit Database - CXSecurity.com
IT之家
IT之家
T
Threatpost
Scott Helme
Scott Helme
L
LINUX DO - 热门话题
腾讯CDC
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
N
News and Events Feed by Topic
L
LINUX DO - 最新话题
F
Full Disclosure
大猫的无限游戏
大猫的无限游戏
H
Heimdal Security Blog
S
SegmentFault 最新的问题

Bryan Robinson's Blog

Does our technology still work for us? Product pricing, dev bad habits, and the role of the pit of success Astro Server Island for latest Bluesky post (heavily cached!) Type-safe environment variables in Astro 5.0 New Website, but really is it? Netlify Durable Cache: Caching for a third-party world Introducing the Hygraph Astro Content Loader Integrating Astro.js Starlight Documentation into a Next.js Project Using Proxies Jamstack is meaningless 😱 Book Release: Eleventy by Example – Learn 11ty with 5 in-depth projects 11ty Second 11ty: Creating Template Filters 11ty Second 11ty: Global Data files (JS and JSON) 11ty second 11ty: The Render Plugin Part 1 Help needed: Netlify Frontend environment variables with Astro.js Quick experiment with the Slinkity 11ty plugin Creating a dynamic color converter with 11ty Serverless Using 11ty JavaScript Data files to mix Markdown and CMS content into one collection How to show your template code in 11ty blog posts New City, New Job, New Content Using Nunjucks Climbing the 11ty Performance leaderboard with Cloudinary, critical CSS and more Three JAMstack movements to watch in 2020 Create a Codepen promo watermark with no additional HTML, CSS or JS 3 underused CSS features to learn for 2020 Adapt client-side JavaScript for use in 11ty (Eleventy) data files CSS Gap creates a bright future for margins in Flex as well as Grid Create your first CSS Custom Properties (Variables) Use CSS Grid to create a self-centering full-width element Creating an 11ty Plugin - SVG Embed Tool Now offering design and code reviews at PeerReviews.dev Routing contact-form emails to different addresses with Netlify, Zapier and SendGrid Create an Eleventy (11ty) theme based on a free HTML template Client work and the JAMstack Grid vs. Flex: A Tale of a "Simple" Promo Space Using Eleventy The Tech Barrier to Entry What Can We Learn from CERN Let Practical CSS Grid - Launching My First Course Build Trust on the Web incorporating User Worries with your User Stories 2019 The Year of Markup-First Development Refactoring CSS into a Sass mixin Starting a new journey with Code Contemporary Dynamic Static Sites with Netlify and iOS Shortcuts Top 3 uses for the ::before and ::after CSS pseudo elements How To: Use CSS Grid to Mix and Match Design Patterns Use CSS ::before and ::after for simple, spicy image overlays Modern CSS: Four Things Every Developer and Designer Should Know About CSS 3 Strategies for Getting Started with CSS Grid CSS Tip: Use rotate() and skew() together to introduce some clean punk rock to your CSS The 5 Stages of Grid Love How To: A CSS-Only Mobile Off Canvas Navigation How To: Use CSS Grid Layout to Make a Simple, Fluid Card Grid Make a More Flexible Cover Screen with CSS Grid Can CSS Grid open up interesting CMS Layout options? Firefox 52 to Introduce New Box-Alignment Values Falling Forward — Rethinking Progressive Enhancement, Graceful Degradation and Developer Morality Start Exploring the Magic of CSS Grid Layout I Converted My Blog to CSS Grid Layout and Regret Nothing Feature Queries are on the Rise CSS Shapes — Let the Text Flow Around You Flexbox -- Let Memorializing Prince and Print vs. The Web I went to Italy and noticed UX fails How to Get Designers to Contribute in Open Source The True Gift of Your Former Code
Use CSS Subgrid to layout full-width content stripes in an article template
2019-11-15 · via Bryan Robinson's Blog

Promo image showing a snippet of the diagram of our grid and subgrid from below

CSS Grid was missing one important piece when it’s Level 1 specification was released into the world: Subgrid. The Level 2 specification is still in “Working Draft” status, but we have our first browser implementation. Mozilla has the Subgrid syntax in Firefox Nightly.

Now that we have a live implementation, I’ve begun playing with the subgrid. Subgrid allows for elements that are grandchildren of the Grid container to take part in the initial grid definition. If you want a primer on all things Grid Level 2, read Rachel Andrew’s excellent article on it. For a fun layout using its power, read Michelle Barker’s CSS{IRL} post.

In this article, we’ll be exploring one specific use case: augmenting a Grid-infused article layout. This article layout will allow for certain sections of content to break out into full-width areas.

If you’re looking for more information on this design pattern, it’s covered in my CSS Grid course here.

Setting up our Grid

Let’s dive in to the code.

Our first task will be setting up our article’s basic Grid. In this case, we’ll take advantage of the power of Grid’s “named grid lines” to make our lives easier down the road.

.article-body {
    display: grid;
    grid-template-columns: [fullWidth-start] 1rem 
                           [left-start]      1fr 
                           [article-start right-start] minmax(20ch, 80ch) 
                           [article-end left-end] 1fr 
                           [right-end] 1rem [fullWidth-end];
}

This setup gives us 1rem gutters on the left and right hand side, followed by “variably squishy” gutters of 1fr. Finally, we have a center column with a minimum size of 20ch and a maximum of 80ch. The ch unit giving us a comfortable reading line-length for the center column.

We can use the names in the braces ([]) to then place content in any of the areas we’ve created via grid-column: fullWidth;.

Struggling to visualize that? I don’t blame you. Here’s a quick graphic to illustrate:

Graphic illustrating how the lines are formed and named with a few boxes illustrating where boxes assigned to various lines will show up.

Placing content on our grid

Now that our grid is set up, we need to place content on it properly. Any elements within the article-body element will fill grid areas across the horizontal axis. This will look absolutely busted (a colloquialism meaning REALLY broken).

Let’s fix that by putting any direct child into the article grid column.

.article-body > * {
    grid-column: article;
}
.full-width {
    grid-column: fullWidth;
    background-color: lightblue; /* For that full-width feeling! */
}

Now our general content will be in a nice constrained column and any element with class="full-width" will go in a full-width stripe.

This is handy enough without going any deeper. Anything inside that element can no be styled as a full-with item with all the white space it wants. But what if you want to have an element centered the same way inside the stripe? You’d need to create a new grid context and create columns of the proper size. In our example, we could do that, but in some circumstances dealing with “variable squishiness” may make that impossible. Even though it’s possible in our case, the code would have us repeating ourselves in odd ways.

To create additional layouts inside our full-width stripe that take part in the initial grid declaration, we need subgrid!

Enter Subgrid

The subgrid specification gives us access to the initial grid declaration’s columns and/or rows. I remember hearing some debate around the syntax and whether it should inherit the entire grid or let the author control columns or rows. I’m so glad they landed where they did. The syntax feels inspired.

In our use case, we just need the columns.

   .full-width {
        display: grid;
        grid-template-columns: subgrid;
   }

That’s it! we now get all of the columns that were declared on .article-body.

Let’s use those named lines and create some classes that we can use for various types of content inside a full-width stripe.

Boxes placed inside the full-width element using the grid-column declarations below

.fullWidth-center {
    grid-column: article;
}
.fullWidth-right {
    grid-column: right;
    text-align: right;
}
.fullWidth-left {
    grid-column: left;
}

When we put it all together, we can create some interesting layouts with minimal effort!

Here’s the finished Codepen. You’ll need to be running the Firefox Nightly build to see the finished product.

{% codepen ‘https://codepen.io/brob/pen/qBBxydZ?editors=0100’ ‘css,result’, ‘500’ %}

Speaking of needing a specific browser … I can hear a few of you out there!

“But Bryan!” you say. “Browsers don’t support this yet! Even when the modern browsers are supporting this, old browser still need support! I guess we can’t use this yet. Oh well!”

No, no. You don’t get off that easy.

Supporting browsers that don’t support subgrid

A comparison of Firefox Nightly vs Firefox 70 and how this support query looks

Just like supporting browsers that don’t support Grid yet, we can support browsers that don’t support subgrid. 

Since we know the answer to the question “Do websites need to look exactly the same in every browser (… dot com…) is “No,” let’s talk about what this design pattern can look like in older browsers.

What if we started our stripes as full-width with centered content? In most situations that should be enough. It’s a clean design pattern and then we can “fall forward” into newer, cooler design patterns.

Let’s talk about what we need to change.

First, let’s declare a base style for .full-width. We’ll use one of my favorite unexpected design patterns: the self-centering stripe with grid.

.full-width {
    grid-column: fullWidth; /* Sets where the element is in the parent grid */
    background-color: lightblue; /* Pretty light blue! */
    
    display: grid; /* Sets a new Grid context */
    grid-template-columns: minmax(20ch, 80ch); /* 1 column to match the [article] sizing */
    justify-content: center; /* Center the content */

    padding: 1rem; /* Keeps gutters in shape for mobile */
}

Now, we’ll use the power of CSS Feature Queries to fall forward into subgrid support. In order to do this, we’ll unset a few values from the previous code and put our subgrid code in the CSS.

@supports (grid-template-columns: subgrid) {
    .full-width {
        grid-template-columns: subgrid; /* changes columns from 1 to inheriting grid lines */
        padding: 0; /* Unset padding... that's built into our columns */
    }
    /* All the other selectors we need */
    .fullWidth-center {
        grid-column: article;
    }
    .fullWidth-right {
        grid-column: right;
        text-align: right; /* Don't want to right align the text unless the element is right-aligned as well */
    }
    .fullWidth-left {
        grid-column: left;
    }
}

You now have an interesting layout in browsers that support subgrid and a perfectly lovely layout for browsers that don’t.

The future of web layout is amazing. What are some other design patterns you might use CSS Subgrid for? Send me a message on Twitter and let me know your thoughts.