Welcome to the exciting opportunity of modern CSS layout and animations!
In this tutorial, you will learn how to create beautiful responsive web layouts using CSS Grid, which is one of the most powerful layout systems available today in CSS. We will also learn how to add smooth transitions and animations that make your websites feel alive.
Why is this worthwhile?
You will be able to create modern, interactive layouts that look and feel like they’re professionally developed by the end of this tutorial. Let’s get started!
To begin, we want to generate the earliest outline of the HTML for our layout:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>CSS Grid, Transitions & Animations</title>
<link rel="stylesheet" href="Grid_Transitions_Animations.css">
</head>
<body>
<div class="container">
<div class="main">
<div class="card">Option 1</div>
<div class="card">Option 2</div>
<div class="card">Option 3</div>
<div class="card">Option 4</div>
</div>
<div class="footer">Footer Content</div>
</div>
</body>
</html>
What’s happening here?
Pro Tip: When you build your HTML, we suggest always structuring it in a semantic way before adding styles. This makes logic easier to follow to read and maintain!
And here are a few lines of foundational styles:
body {
font-family: sans-serif;
margin: 0;
padding: 20px;
background: #f4f4f4;
}
Understanding this code:
Note: Always set your body margins to zero to prevent bizarre spacing issues!
Now we are ready to set up the CSS Grid magic:
.container {
display: grid;
grid-template-areas:
"header header"
"sidebar main"
"footer footer";
grid-template-columns: 1fr 3fr;
grid-template-rows: 80px auto 60px;
gap: 20px;
}
Breaking it down:
Pro tip: The f r unit (fractional unit) is great for responsive layouts. 1f r 3 f r means you are saying “give the first column 1 part and the second column 3 parts of the available space”!
.header {
grid-area: header;
background: #4caf50;
color: white;
display: flex;
align-items: center;
justify-content: center;
font-size: 24px;
}
What this accomplishes:
Now this is where it gets really cool:
.main {
grid-area: main;
display: grid;
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
gap: 10px 10px;
}
Why this is cool:
Note: This single line makes an entirely responsive grid! The cards will wrap to new rows when the screen size is less. No media queries!
Now let’s make our cards interactive:
.card {
background: white;
padding: 20px;
border-radius: 10px;
transition: transform 0.3s ease, box-shadow 0.3s ease;
animation: fadeIn 1s ease forwards;
}
.card:hover {
transform: scale(1.05);
box-shadow: 0 10px 20px rgb(197, 198, 197);
}
Understanding transitions:
Pro tip: Specify transition properties and do not use transition: all for performance!
Now let’s define a custom animation:
@keyframes fadeIn {
from {
opacity: 0;
transform: translateY(20px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
How are animations created:
Now, we can apply this to an element:
.animation {
animation: fadeIn 1s ease forwards;
}
Finally, let’s complete our layout:
.footer {
grid-area: footer;
background: #333;
color: white;
display: flex;
align-items: center;
justify-content: center;
}
It’s simple, but effective:
The Grid Layout System
The most impressive aspect of this project is the number of lines of code involved in the CSS Grid for the entire page layout. The container utilizes named grid areas, which makes the CSS extremely readable and maintainable.
The process:
Nesting a Grid
The .main section shows off a powerful method – nesting grids. Within our main grid layout, we create a grid for the cards. This grid utilizes auto-fill and minmax() to create a responsive card layout that fills any screen width and height.
Why is this important? You’ll never need to write media queries for a basic responsive layout when working with this.
Transitions vs Animations
Transitions and animations are both shown in this project:
In summary, the primary difference between a transition and animation is that transitions require a trigger (like hover, focus, etc.), while an animation can simply happen on the timeline you created.
1. Components / Keywords Used
Name | Description |
display: grid | Activates CSS Grid layout on an element |
grid-template-areas | Defines named areas in the grid using a visual template |
grid-template-columns | Specifies the width of each column in the grid |
grid-template-rows | Specifies the height of each row in the grid |
grid-area | Assigns an element to a named grid area |
gap | Creates spacing between grid items |
repeat() | Function to repeat column/row patterns |
auto-fill | Automatically creates as many tracks as will fit |
minmax() | Sets minimum and maximum size for grid tracks |
fr (fractional unit) | Represents a fraction of available space |
transition | Creates smooth changes between property values |
@keyframes | Defines custom animation sequences |
animation | Applies a keyframe animation to an element |
:hover | Pseudo-class triggered when mouse hovers over element |
2. Key Properties / Parameters / Features
Name | Description |
transform: scale() | Enlarges or shrinks an element |
transform: translateY() | Moves an element vertically |
box-shadow | Adds shadow effects around an element |
opacity | Controls transparency (0 = invisible, 1 = fully visible) |
border-radius | Rounds the corners of an element |
ease | Timing function that starts slow, speeds up, then slows down |
forwards | Animation keeps final keyframe values after completion |
align-items: center | Centers flex/grid items vertically |
justify-content: center | Centers flex/grid items horizontally |
auto | Allows the browser to calculate size automatically |
1fr 3fr | Divides space in a 1:3 ratio |
Looking to learn more? Here’s some good material:
Official Documentation:
Interactive Learning:
Practice & Tools:
Video Tutorials:
Source code:
Completing Day 6 – CSS Grid, Transitions & Animations, taught you to create modern responsive layouts with CSS Grid and embellish them with fluid transitions and animations. You learned how to define grid areas, nested grids, the powerful functions, repeat(), auto-fill, and minmax(), and hover transition effects for interactions. You learned how keyframe animations work, and how they add life through movement. Now, you can create attractive, professional, responsive web designs without relying on complex positioning and media queries. Make sure to continue practicing different grid structures holistically and different animation approaches to develop your design practice!
CSS Grid is meant for creating responsive, two-dimensional layouts where you control rows, columns, and spacing easily.
Flexbox works best for one-dimensional layouts (row OR column) while CSS Grid is best for two-dimensional layouts (rows AND columns).
minmax() sets a minimum and maximum size for grid items which helps grids to remain responsive on all screen sizes.
Transitions need a trigger (such as hover) while animations automatically run using keyframes.
Yes! It is very common to use Grid for page layout and Flexbox inside sections for alignment and distribution.
Copyright © 2025 Niotechone Software Solution Pvt. Ltd. All Rights Reserved.