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
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Subtitles: The Good, the Bad, and the Resource-Heavy
  • Loader Animations Using Anime.js
  • Next.js Theming: CSS Variables for Adaptive Data Visualization
  • Creating Scrolling Text With HTML, CSS, and JavaScript

Trending

  • Java’s Next Act: Native Speed for a Cloud-Native World
  • A Guide to Container Runtimes
  • Building Scalable and Resilient Data Pipelines With Apache Airflow
  • Google Cloud Document AI Basics
  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 user avatar
Shashank Sharma
·
Mar. 20, 23 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
1.6K Views

Join the DZone community and get the full member experience.

Join For Free

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

  • Subtitles: The Good, the Bad, and the Resource-Heavy
  • Loader Animations Using Anime.js
  • Next.js Theming: CSS Variables for Adaptive Data Visualization
  • Creating Scrolling Text With HTML, CSS, and JavaScript

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!