CSS Grid's grid-template-rows
and grid-template-columns
properties are now available for animation in all major browsers! In fact, CSS Grid has long supported animations because it is built into the CSS Grid layout module level 1 specification.
But until recently, all three major browsers fully support animation of these mesh properties. Let’s look at a few examples to inspire your creativity!
Catalog
- Catalog
- Example 1: Expand the sidebar
- Example 2: Expand the panel
- Example 3: Add rows and columns
- More examples
Example 1: Expand the sidebar
First of all, this is what we are going to discuss:
A simple two-column grid. Previously, you probably wouldn't have built it with CSS Grid because animations and transitions are not supported, but what if you want the left column (probably sidebar navigation) to expand on hover? Now, this is possible.
I know what you are thinking: "Animation CSS attributes? A piece of cake, I've been doing it for many years!" So do I. However, I encountered an interesting hurdle when trying a specific use case.
So we want to convert the mesh itself (especially the .grid
set on the grid-template-columns
class in the example). But the left column (.left
) is the selector that requires the :hover
pseudo-class. While JavaScript can easily solve this problem - thank you, no need - we can implement it with just CSS.
Let's explain the whole process step by step, starting with HTML. It's actually very standard...a grid with two columns.
<div class="grid"> <div class="left"></div> <div class="right"></div> </div>
Decorative CSS aside, you first need to set .grid
on the parent container (display: grid
).
.grid { display: grid; }
Next, we can use the grid-template-columns
attribute to define and resize the two columns. We set the left column very narrow and later increase its width when hovering. The column on the right will take up the rest of the space, thanks to the auto
keyword.
.grid { display: grid; grid-template-columns: 48px auto; }
We know we want to animate it, so let's add a transition effect while adding the animation so that the changes between states will be smooth and obvious.
.grid { display: grid; grid-template-columns: 48px auto; transition: 300ms; /* 根據(jù)需要更改 */ }
.grid
It's done! The rest is the application hover state. Specifically, we will override the grid-template-columns
property so that the left column takes up more space when hovering.
This alone isn't that fun, although CSS Grid now supports animation and transitions. What's more interesting is that we can use the relatively new :has()
pseudo-class to style the parent container (.left
) when the child element (.grid
) is hovered.
<div class="grid"> <div class="left"></div> <div class="right"></div> </div>
In simple English, this means: "If the .grid
container contains an element named .left
and the element is hovering, then perform some operations on the .grid
container." This is why :has()
is often referred to as a "parent" selector. We can finally select the parent element based on the child elements it contains - no JavaScript is required!
So let's increase the width of the .left
column to 30% when hovering. The .right
column will continue to occupy all remaining space:
.grid { display: grid; }
We can also use CSS variables, which may or may not look concise, depending on your personal preferences (or you may already use CSS variables in your project):
.grid { display: grid; grid-template-columns: 48px auto; }
I really like the CSS grids can now make animations, but the fact that we can build this particular example with only nine lines of CSS code is even more amazing.
This is another example of Olivia Ng—a concept similar, but contains content (click on the navigation icon):
Example 2: Expand the panel
This example converts the grid container (column width) and also converts the individual columns (their background color). It's perfect for providing more content on hover.
It is worth remembering that the repeat()
function sometimes produces wrong transitions, which is why I set the width of each column separately (i.e. grid-template-columns: 1fr 1fr 1fr
).
Example 3: Add rows and columns
This example animates a column to the grid. But—you guessed it—there is also a trap for this situation. The requirement is that the "new" column cannot be hidden (i.e. set to display: none
) and that the CSS Grid must acknowledge its existence when it sets its width to 0fr.
So for a three-column grid - grid-template-columns: 1fr 1fr 0fr
(yes, the unit must be declared even if the value is 0!) will be converted correctly to grid-template-columns: 1fr 1fr 1fr
, but grid-template-columns: 1fr 1fr
will not. In hindsight, this is actually completely reasonable given our understanding of how the transition works.
This is another example of Michelle Barker—the same concept, but with a column and more cool effects. Make sure to run this example in full screen mode, as it is actually responsive (no tricks, just good design!).
More examples
Why not?
This "animated Mondrian" is the initial proof of concept for animated CSS grids produced by Chrome DevRel. grid-row
and grid-column
Use the span
keywords to create the layout you see before you, and then use CSS animation to animate grid-template-row
and grid-template-column
. It's not as complicated as it seems!
Same concept, but more of Michelle Barker’s cool effects. Can you make a good loading spinner?
Finally review nostalgia (I'm age here), Andrew Harvard makes an animated CSS grid that isn't quite like a grid. Same – the same concept – you just can't see other grid items. But don't worry, they're there.
The above is the detailed content of Animating CSS Grid (How To Examples). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

There are three ways to create a CSS loading rotator: 1. Use the basic rotator of borders to achieve simple animation through HTML and CSS; 2. Use a custom rotator of multiple points to achieve the jump effect through different delay times; 3. Add a rotator in the button and switch classes through JavaScript to display the loading status. Each approach emphasizes the importance of design details such as color, size, accessibility and performance optimization to enhance the user experience.

To deal with CSS browser compatibility and prefix issues, you need to understand the differences in browser support and use vendor prefixes reasonably. 1. Understand common problems such as Flexbox and Grid support, position:sticky invalid, and animation performance is different; 2. Check CanIuse confirmation feature support status; 3. Correctly use -webkit-, -moz-, -ms-, -o- and other manufacturer prefixes; 4. It is recommended to use Autoprefixer to automatically add prefixes; 5. Install PostCSS and configure browserslist to specify the target browser; 6. Automatically handle compatibility during construction; 7. Modernizr detection features can be used for old projects; 8. No need to pursue consistency of all browsers,

Themaindifferencesbetweendisplay:inline,block,andinline-blockinHTML/CSSarelayoutbehavior,spaceusage,andstylingcontrol.1.Inlineelementsflowwithtext,don’tstartonnewlines,ignorewidth/height,andonlyapplyhorizontalpadding/margins—idealforinlinetextstyling

Setting the style of links you have visited can improve the user experience, especially in content-intensive websites to help users navigate better. 1. Use CSS's: visited pseudo-class to define the style of the visited link, such as color changes; 2. Note that the browser only allows modification of some attributes due to privacy restrictions; 3. The color selection should be coordinated with the overall style to avoid abruptness; 4. The mobile terminal may not display this effect, and it is recommended to combine it with other visual prompts such as icon auxiliary logos.

Use the clip-path attribute of CSS to crop elements into custom shapes, such as triangles, circular notches, polygons, etc., without relying on pictures or SVGs. Its advantages include: 1. Supports a variety of basic shapes such as circle, ellipse, polygon, etc.; 2. Responsive adjustment and adaptable to mobile terminals; 3. Easy to animation, and can be combined with hover or JavaScript to achieve dynamic effects; 4. It does not affect the layout flow, and only crops the display area. Common usages are such as circular clip-path:circle (50pxatcenter) and triangle clip-path:polygon (50%0%, 100 0%, 0 0%). Notice

TheCSSPaintingAPIenablesdynamicimagegenerationinCSSusingJavaScript.1.DeveloperscreateaPaintWorkletclasswithapaint()method.2.TheyregisteritviaregisterPaint().3.ThecustompaintfunctionisthenusedinCSSpropertieslikebackground-image.Thisallowsfordynamicvis

To create responsive images using CSS, it can be mainly achieved through the following methods: 1. Use max-width:100% and height:auto to allow the image to adapt to the container width while maintaining the proportion; 2. Use HTML's srcset and sizes attributes to intelligently load the image sources adapted to different screens; 3. Use object-fit and object-position to control image cropping and focus display. Together, these methods ensure that the images are presented clearly and beautifully on different devices.

Different browsers have differences in CSS parsing, resulting in inconsistent display effects, mainly including the default style difference, box model calculation method, Flexbox and Grid layout support level, and inconsistent behavior of certain CSS attributes. 1. The default style processing is inconsistent. The solution is to use CSSReset or Normalize.css to unify the initial style; 2. The box model calculation method of the old version of IE is different. It is recommended to use box-sizing:border-box in a unified manner; 3. Flexbox and Grid perform differently in edge cases or in old versions. More tests and use Autoprefixer; 4. Some CSS attribute behaviors are inconsistent. CanIuse must be consulted and downgraded.
