Saturday, June 28, 2025
News PouroverAI
Visit PourOver.AI
No Result
View All Result
  • Home
  • AI Tech
  • Business
  • Blockchain
  • Data Science & ML
  • Cloud & Programming
  • Automation
  • Front-Tech
  • Marketing
  • Home
  • AI Tech
  • Business
  • Blockchain
  • Data Science & ML
  • Cloud & Programming
  • Automation
  • Front-Tech
  • Marketing
News PouroverAI
No Result
View All Result

How to Align Column Rows with CSS Subgrid — SitePoint

March 25, 2024
in Cloud & Programming
Reading Time: 3 mins read
0 0
A A
0
Share on FacebookShare on Twitter



In this quick tip, we’ll look at how to use the subgrid feature of CSS Grid to align the content of boxes that sit side by side. Note: before delving into subgrid, it’s important to understand the basics of Grid layout. If you’re new to Grid, or you need a refresher, check out our beginner’s guide to CSS Grid.

The Problem
The image below shows three boxes in a row. They have different amounts of content, but they’re all the same height thanks to Grid layout. However, the components within each box don’t align with each other, which doesn’t look so nice, and there’s nothing Grid can do about that. As far as Grid is concerned, there’s just one row of boxes, and it doesn’t offer a way to align the content they contain into rows. But by using subgrid, we can get the result shown below. Let’s dive into how to use Grid and subgrid to get this result.

Step 1: Setup
Here’s the basic HTML for our demo:

We have and

wrapper containing three

elements. The

has the following CSS:
“`html

display: grid;
grid-template-columns: 1fr 1fr 1fr;

“`

This CSS causes the

elements to be arranged in three columns. Each

contains an

, a

    and a

    :
    “`html

      “`

      At this stage, each column in the grid is actually the same height, but not each column is full of content, as shown below. There’s a different amount of content in each column, so they don’t appear to be the same height.

      Step 2: Setting display: grid on the sections
      We can only use the subgrid value on an element that’s set to display: grid. As we want to use subgrid to align the content of our

      elements, we therefore need to set them to display: grid first:
      “`html

      display: grid;

      “`

      The content now fills each of our columns, as shown in the inspector. Here’s our updated demo. Note: the content is stretched to full height because the default setting for columns is align-content: stretch. (That’s not important for this demo, but worth noting anyway!)

      Step 3: Using subgrid to Align Content
      The final step is to get the three elements in each column to align in rows. Firstly, we set the grid-template-rows property to subgrid:
      “`html

      display: grid;
      grid-template-rows: subgrid;

      “`

      This produces the result pictured below. Oops! What’s gone wrong here? We can only see the last element of each column. The problem is that our

      element only has one row, so the elements within each section are stacked on top on one another within that one row. The final step we need to take is to tell the subgrid content to span three rows:
      “`html

      display: grid;
      grid-template-rows: subgrid;
      grid-row: span 3;

      “`

      Our content now spans three rows of the subgrid, as shown below. But look, there are gaps between each row! That’s because subgrid inherits the gap setting of its parent grid. We can change that by setting a different gap value. If we apply gap: 0 to our

      elements, we get the final result we’re seeking. Here’s our completed demo. As a side note, an alternative to grid-row: span 3 would be grid-row: 1 / 3.

      Browser Support
      Since late 2023, subgrid has worked across all major browsers, as explained on the caniuse site. So it’s definitely viable to start using subgrid now. For browsers that support Grid but not subgrid, you’ll probably get an acceptable result. Our original demo above was quite acceptable, even though it looks nicer to have the content aligned horizontally. For browsers that don’t support Grid layout at all, users should get perfectly usable content all in a single column.

      Conclusion
      The subgrid value can be set for grid-template-columns and/or grid-template-rows, allowing subgrid content to align with the columns and rows of parent grids. As you can see, subgrid is quite easy to use but is a powerful and much-needed addition to Grid layouts. To learn more about all the things you can do with subgrid, check out the following resources.



      Source link

      Tags: alignColumncssRowsSitePointSubgrid

    Previous Post

    Hurun list: Mumbai pips Beijing to become Asia’s billionaire capital for the first time

    Next Post

    Most common causes of machine vibration – gilautomation

    Related Posts

    Top 20 Javascript Libraries You Should Know in 2024
    Cloud & Programming

    Top 20 Javascript Libraries You Should Know in 2024

    June 10, 2024
    Simplify risk and compliance assessments with the new common control library in AWS Audit Manager
    Cloud & Programming

    Simplify risk and compliance assessments with the new common control library in AWS Audit Manager

    June 6, 2024
    Simplify Regular Expressions with RegExpBuilderJS
    Cloud & Programming

    Simplify Regular Expressions with RegExpBuilderJS

    June 6, 2024
    How to learn data visualization to accelerate your career
    Cloud & Programming

    How to learn data visualization to accelerate your career

    June 6, 2024
    BitTitan Announces Seasoned Tech Leader Aaron Wadsworth as General Manager
    Cloud & Programming

    BitTitan Announces Seasoned Tech Leader Aaron Wadsworth as General Manager

    June 6, 2024
    Copilot Studio turns to AI-powered workflows
    Cloud & Programming

    Copilot Studio turns to AI-powered workflows

    June 6, 2024
    Next Post
    Most common causes of machine vibration – gilautomation

    Most common causes of machine vibration – gilautomation

    Run large-scale simulations with AWS Batch multi-container jobs

    Run large-scale simulations with AWS Batch multi-container jobs

    AI investment accounting platform co FundGuard raises $100m

    AI investment accounting platform co FundGuard raises $100m

    Leave a Reply Cancel reply

    Your email address will not be published. Required fields are marked *

    • Trending
    • Comments
    • Latest
    23 Plagiarism Facts and Statistics to Analyze Latest Trends

    23 Plagiarism Facts and Statistics to Analyze Latest Trends

    June 4, 2024
    How ‘Chain of Thought’ Makes Transformers Smarter

    How ‘Chain of Thought’ Makes Transformers Smarter

    May 13, 2024
    Amazon’s Bedrock and Titan Generative AI Services Enter General Availability

    Amazon’s Bedrock and Titan Generative AI Services Enter General Availability

    October 2, 2023
    Is C.AI Down? Here Is What To Do Now

    Is C.AI Down? Here Is What To Do Now

    January 10, 2024
    The Importance of Choosing a Reliable Affiliate Network and Why Olavivo is Your Ideal Partner

    The Importance of Choosing a Reliable Affiliate Network and Why Olavivo is Your Ideal Partner

    October 30, 2023
    Managing PDFs in Node.js with pdf-lib

    Managing PDFs in Node.js with pdf-lib

    November 16, 2023
    Can You Guess What Percentage Of Their Wealth The Rich Keep In Cash?

    Can You Guess What Percentage Of Their Wealth The Rich Keep In Cash?

    June 10, 2024
    AI Compared: Which Assistant Is the Best?

    AI Compared: Which Assistant Is the Best?

    June 10, 2024
    How insurance companies can use synthetic data to fight bias

    How insurance companies can use synthetic data to fight bias

    June 10, 2024
    5 SLA metrics you should be monitoring

    5 SLA metrics you should be monitoring

    June 10, 2024
    From Low-Level to High-Level Tasks: Scaling Fine-Tuning with the ANDROIDCONTROL Dataset

    From Low-Level to High-Level Tasks: Scaling Fine-Tuning with the ANDROIDCONTROL Dataset

    June 10, 2024
    UGRO Capital: Targeting to hit milestone of Rs 20,000 cr loan book in 8-10 quarters: Shachindra Nath

    UGRO Capital: Targeting to hit milestone of Rs 20,000 cr loan book in 8-10 quarters: Shachindra Nath

    June 10, 2024
    Facebook Twitter LinkedIn Pinterest RSS
    News PouroverAI

    The latest news and updates about the AI Technology and Latest Tech Updates around the world... PouroverAI keeps you in the loop.

    CATEGORIES

    • AI Technology
    • Automation
    • Blockchain
    • Business
    • Cloud & Programming
    • Data Science & ML
    • Digital Marketing
    • Front-Tech
    • Uncategorized

    SITEMAP

    • Disclaimer
    • Privacy Policy
    • DMCA
    • Cookie Privacy Policy
    • Terms and Conditions
    • Contact us

    Copyright © 2023 PouroverAI News.
    PouroverAI News

    No Result
    View All Result
    • Home
    • AI Tech
    • Business
    • Blockchain
    • Data Science & ML
    • Cloud & Programming
    • Automation
    • Front-Tech
    • Marketing

    Copyright © 2023 PouroverAI News.
    PouroverAI News

    Welcome Back!

    Login to your account below

    Forgotten Password? Sign Up

    Create New Account!

    Fill the forms bellow to register

    All fields are required. Log In

    Retrieve your password

    Please enter your username or email address to reset your password.

    Log In