CSS Grid is a powerful layout system built in CSS that can easily create complex and responsive web layouts, it defines a 2D layout system that can precisely control rows and columns, you can place elements within this grid by specifying their positions in rows and columns, unlike Flexbox, which is 1-dimensional (works on a single axis).
These are the Following methods through which we can animate CSS Grid:
The easiest way is to animate individual grid objects by changing their position, size, rows
or rotation, this does not change the grid layout but it allows adding dynamic effects to elements within the grid.
Example: In this example, we will be animating individual grid items with a transform.
HTML
/* index.html */
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content=
"width=device-width, initial-scale=1.0">
<title>GeeksforGeeks Grid Animation</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<h1>GeeksforGeeks</h1>
<h2>Approach 1) Animating Individual Grid Items with transform</h2>
<div class="grid-container">
<div class="grid-item">1</div>
<div class="grid-item">2</div>
<div class="grid-item">3</div>
<div class="grid-item">4</div>
<div class="grid-item">5</div>
<div class="grid-item">6</div>
</div>
</div>
</body>
</html>
CSS
/* style.css */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: Arial, sans-serif;
}
body {
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
background-color: #f4f4f4;
}
.container {
text-align: center;
}
h1 {
color: #197b43;
margin-bottom: 10px;
font-size: 2.5rem;
}
h2 {
color: #000000;
margin-bottom: 20px;
font-size: 1.5rem;
}
.grid-container {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 25px;
padding: 40px;
border: 5px solid #197b43;
border-radius: 10px;
background-color: white;
}
.grid-item {
background-color: #197b43;
padding: 40px;
color: white;
text-align: center;
font-size: 1.5rem;
transition: transform 0.5s ease-in-out;
border-radius: 5px;
}
.grid-item:hover {
transform: scale(1) rotate(15deg);
}
Output:
Animating Grid Template Areas
This method allows you to change between different web template areas and animate the entire web layout, this allows you to create the effect of configuration changes without changing the HTML structure.
Example: In this example, we will animate grid template areas.
HTML
/* index.html */
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content=
"width=device-width, initial-scale=1.0">
<title>GeeksforGeeks Grid Template Areas Animation</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<h1>GeeksforGeeks</h1>
<h2>Approach 2) Animating Grid Template Areas</h2>
<div class="grid-container">
<div class="header">Header</div>
<div class="sidebar">Sidebar</div>
<div class="main">Main Content</div>
<div class="footer">Footer</div>
</div>
<button id="toggle-layout">Toggle Layout</button>
</div>
<script src="index.js"></script>
</body>
</html>
CSS
/* style.css */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: Arial, sans-serif;
}
body {
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
background-color: #f4f4f4;
}
.container {
text-align: center;
}
h1 {
color: #197b43;
margin-bottom: 10px;
font-size: 2.5rem;
}
h2 {
color: #000000;
margin-bottom: 20px;
font-size: 1.5rem;
}
.grid-container {
display: grid;
grid-template-areas:
"header header"
"sidebar main"
"footer footer";
grid-template-columns: 1fr 2fr;
gap: 15px;
padding: 20px;
border: 5px solid #197b43;
border-radius: 10px;
background-color: white;
transition: grid-template-areas 0.5s ease;
}
.header {
grid-area: header;
background-color: #4CAF50;
color: white;
padding: 20px;
}
.sidebar {
grid-area: sidebar;
background-color: #87cefa;
color: white;
padding: 20px;
}
.main {
grid-area: main;
background-color: #32cd32;
color: white;
padding: 20px;
}
.footer {
grid-area: footer;
background-color: #ffa500;
color: white;
padding: 20px;
}
.grid-container.active {
grid-template-areas:
"header header"
"main sidebar"
"footer footer";
}
button {
margin-top: 20px;
padding: 10px 20px;
background-color: #10d566;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 1rem;
}
button:hover {
background-color: #388e3c;
}
JavaScript
document.getElementById('toggle-layout').addEventListener('click', function() {
document.querySelector('.grid-container').classList.toggle('active');
});
Output:
Animating Grid Track Sizes
The alteration of grid track size animation can help to implement the sensitive resizing of columns or rows, this is helpful in responsive or interactive designs where some space expands or contracts based on user action.
Example: In this example, we will animate grid track sizes.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content=
"width=device-width, initial-scale=1.0">
<title>GeeksforGeeks Grid Track Sizes Animation</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<h1>GeeksforGeeks</h1>
<h2>Approach 3) Animating Grid Track Sizes</h2>
<div class="grid-container">
<div class="grid-item">1</div>
<div class="grid-item">2</div>
<div class="grid-item">3</div>
</div>
<button id="resize-grid">Resize Grid</button>
</div>
<script src="index.js"></script>
</body>
</html>
CSS
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: Arial, sans-serif;
}
body {
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
background-color: #f4f4f4;
}
.container {
text-align: center;
}
h1 {
color: #197b43;
margin-bottom: 10px;
font-size: 2.5rem;
}
h2 {
color: #000000;
margin-bottom: 20px;
font-size: 1.5rem;
}
.grid-container {
display: grid;
grid-template-columns: 1fr 2fr 1fr;
gap: 15px;
padding: 20px;
border: 5px solid #197b43;
border-radius: 10px;
background-color: white;
transition: grid-template-columns 1s ease-in-out;
}
.grid-item {
background-color: #197b43;
padding: 40px;
color: white;
text-align: center;
font-size: 1.5rem;
border-radius: 5px;
}
button {
margin-top: 20px;
padding: 10px 20px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 1rem;
}
button:hover {
background-color: #197b43;
}
.grid-container.resized {
grid-template-columns: 2fr 1fr 2fr;
}
JavaScript
document.getElementById('resize-grid').addEventListener('click', function() {
document.querySelector('.grid-container').classList.toggle('resized');
});
Output:
Animating Grid Item Position
This approach allows you to animate the position of individual grid items by transitioning their grid-column
and grid-row
values. It can create a visual effect of moving elements within the grid, such as reordering items or shifting them to different sections.
Example: In this example, we will animate grid item position using grid-column and grid-row.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content=
"width=device-width, initial-scale=1.0">
<title>GeeksforGeeks Grid Item Position Animation</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<h1>GeeksforGeeks</h1>
<h2>Approach 4) Animating Grid Item Position Using grid-column and grid-row</h2>
<div class="grid-container">
<div class="grid-item item-1">1</div>
<div class="grid-item item-2">2</div>
<div class="grid-item item-3">3</div>
<div class="grid-item item-4">4</div>
</div>
<button id="reposition-grid">Reposition Items</button>
</div>
<script src="index.js"></script>
</body>
</html>
CSS
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: Arial, sans-serif;
}
body {
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
background-color: #f4f4f4;
}
.container {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
text-align: center;
}
h1 {
color: #197b43;
margin-bottom: 10px;
font-size: 2.5rem;
}
h2 {
color: #020202;
margin-bottom: 20px;
font-size: 1.5rem;
}
.grid-container {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(100px, 1fr));
/* Use auto-fit to dynamically adjust the column width */
gap: 15px;
padding: 20px;
border: 5px solid #197b43;
border-radius: 10px;
background-color: white;
transition: grid-column 1s ease, grid-row 1s ease;
width: 100%;
/* Set width to 100% to prevent overflow */
max-width: 500px;
/* Add a maximum width if needed */
}
.grid-item {
background-color: #197b43;
padding: 40px;
color: white;
text-align: center;
font-size: 1.5rem;
border-radius: 5px;
}
.item-1 {
grid-column: 1;
grid-row: 1;
}
.item-2 {
grid-column: 2;
grid-row: 1;
}
.item-3 {
grid-column: 3;
grid-row: 1;
}
.item-4 {
grid-column: 1;
grid-row: 2;
}
.grid-container.repositioned .item-1 {
grid-column: 2;
grid-row: 2;
}
.grid-container.repositioned .item-2 {
grid-column: 3;
grid-row: 1;
}
.grid-container.repositioned .item-3 {
grid-column: 1;
grid-row: 1;
}
.grid-container.repositioned .item-4 {
grid-column: 2;
grid-row: 1;
}
button {
margin-top: 20px;
padding: 10px 20px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 1rem;
}
button:hover {
background-color: #197b43;
}
JavaScript
document.getElementById('reposition-grid').addEventListener('click', function() {
document.querySelector('.grid-container').classList.toggle('repositioned');
});
Output:
Similar Reads
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
React Tutorial React is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version
7 min read
JavaScript Interview Questions and Answers JavaScript is the most used programming language for developing websites, web servers, mobile applications, and many other platforms. In Both Front-end and Back-end Interviews, JavaScript was asked, and its difficulty depends upon the on your profile and company. Here, we compiled 70+ JS Interview q
15+ min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read