DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Programmatic Brand Extraction: Pulling Logos, Colors, and Assets from Any URL
  • The Death of the CSS Selector: Architecting Resilient, AI-Powered Web Scrapers
  • A Guide to Parallax and Scroll-Based Animations
  • Building a Card Layout Using CSS Subgrid

Trending

  • Running TypeSafe AI's Jev Inside Neo4j for Confidence-Aware Sentiment Decisions
  • AI-Era Cybersecurity, Part 1: Mapping Glasswing, Daybreak, Lightwell and the Tools Racing to Find and Fix Bugs
  • When One Docker Swarm Replica Became Unreachable Over an Overlay Network: A Production Case With Traefik
  • Git Blame Isn’t Enough: Building Verifiable Provenance for AI-Generated Code

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Languages
  4. Learn CSS Grid “The Easy Way”!

Learn CSS Grid “The Easy Way”!

Explore the basics of CSS Grid with a step-by-step guide. Discover how to create complex, responsive web designs using rows and columns with CSS Grid.

By 
Shashank Sharma
·
Mar. 20, 23 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
1.9K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

CSS Grid is a layout system for the web, allowing developers to create complex, responsive designs using rows and columns. Grid is used to make complex web design layouts more easily. In this article, we will explore the basics of CSS Grid with a step-by-step guide. Readers will discover how to create complex, responsive web designs using rows and columns with CSS Grid.

First things first, we have to make a div and give it a class of containers or anything you want.

HTML
 
<div class="container">  <div>one</div>  <div>two</div> </div>


After creating our div, let's go to our CSS and start applying the grid on it. we assign our container a display property of the grid.

CSS
 
.container{ display: grid; }


Now, After assigning display: grid; nothing happens because we didn't set the width of the container so for that there is a different property in the grid to set the width, which is grid-template-columns.

CSS
 
.container{ display: grid; grid-template-columns: 50% 50%; }


So, this gives us two columns, each having 50% width.

NOTE: There is a short-hand property to write grid-template-columns,

CSS
 
.container{ display: grid; grid-template-columns: repeat(2, 50%); }


The repeat() property works same as the above values which is 50% and 50%, but, here instead of writing 50% twice, we set it to repeat() then 2 which means to create 2 columns and then 50% which means to set a width of 50%.

KEY-POINT:- Instead of using % or pixels, we use fr(fraction) in grid-template-columns value. for ex. 1fr, 2fr, 1/2fr...etc.

CSS
 
.container{ display: grid; grid-template-columns: repeat(2, 1fr); }


fr works very well and it's very important to add for responsiveness of the website.

Now, if we want spaces b/w our grids, then the property for that in the grid is gap or grid-gap or row-gap/column-gap.

CSS
 
.container{ display: grid; grid-template-columns: repeat(2, 1fr); grid-gap: 10px }


This grid-gap property gives us a space of 10px around our rows and columns. If you want to add a gap in b/w rows or columns, you can use a property of row-gap or column-gap.

CSS
 
.container{ display: grid; grid-template-columns: repeat(2, 1fr); row-gap: 10px; }  or or or 
.container{ display: grid; grid-template-columns: repeat(2, 1fr); column-gap: 10px; }


Now, in order to add some height to our container, we use a property in the grid called grid-template-rows.

CSS
 
.container{ display: grid; grid-template-columns: repeat(2, 1fr); grid-gap: 10px; grid-template-rows: 200px; }


But there's a catch in grid-template-rows, Now from the above-given example, you might think that what if our content is more than 200px and then our content starts overflowing, and it doesn't look good on layouts? So, what might be the solution here? You must be thinking, oh, I wish there would be a property that adjusts itself according to the content...and guess what, there is a property for this called grid-auto-rows.

CSS
 
.container{ display: grid; grid-template-columns: repeat(2, 1fr); grid-gap: 10px; grid-auto-rows: minmax(200px, auto); }


Now, in the grid-auto-rows property you write minmax(), and in between parenthesis, you add two values. In the above examples, we add 200px as a minimum value and auto as a maximum value.

Now if our content is less than 200px or exactly 200px, there won't be any issue, and it gets fit in easily, but if our content is more than 200px, let's say 300px, then our auto property works, and our rows will adjust according to the content size and fit in easily without overflowing.

There are more grid properties, but I think if you practice the above-shown properties, you will easily create a very good and responsive layout. Keep in mind that by doing just the above-shown examples, you cannot create a layout. You have to add the margin, padding, and all other basic properties.

That's it for the CSS Grids from my side. Feel free to leave feedback and areas where I should improve myself as I am currently learning...

CSS

Published at DZone with permission of Shashank Sharma. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Programmatic Brand Extraction: Pulling Logos, Colors, and Assets from Any URL
  • The Death of the CSS Selector: Architecting Resilient, AI-Powered Web Scrapers
  • A Guide to Parallax and Scroll-Based Animations
  • Building a Card Layout Using CSS Subgrid

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook