Menu

  • Home
  • Trending

CATEGORIES

  • Stock Market & Trading
  • Banking & Finance
    • Banking
    • Credit and Debit Cards
  • Business & Startups
    • Business Basics
    • Business ideas
  • Cooking & Food
    • Easy Healthy Meals
  • Education
    • Business Skills
    • Content Writing Skills
    • Digital Marketing
  • Fashion & Lifestyle
    • Fashion
  • Freelancing
    • Finance & Accounting
  • Health & Fitness
    • Health
    • Health care
    • Health care occupations
    • Health sciences
    • Human pregnancy
  • Hobbies
    • Entertainment
    • Environmental design
    • Handicrafts
    • Hobbies
    • Home appliances
    • Home improvement
    • Horticulture and gardening
  • Learn & Earn
  • Learners Hub
    • NFT Beginners
  • Movie
  • Music
    • Musical instruments
  • News
    • Nation Wants to Know
    • National News
  • Personal Development
    • Marriage
    • Parenting
    • Personal care and service occupations
    • Personal development
    • Personal life
  • Real Estate
  • Religion
    • Prayer Method
    • Puran
    • Sanatan Dharma Basic
    • Sanatan Dharma Symbols & meaning
    • Scienfic reason behind Sanatan Dharma
  • Social Media
  • Technology
    • Services & Software
    • Software development
    • Technology News
  • Travel & Tourism
    • Things to Do
    • Travel Blogs

Subscriptions

  • admin

Recent News

  • Peanut Alert: Various Two Rivers Hot Chocolate Products
  • Migraines And Dating: How It Feels To Date Someone Who Cares
  • Content Traceability and Standardization | TechWhirl
  • Home
  • Browse
    • Personal Development
    • Fashion & Lifestyle
    • Health & Fitness
    • Cooking & Food
    • Stock Market & Trading
    • Business & Startups
    • Technology
    • Travel & Tourism
    • Business
    • Freelancing
    • Learners Hub
  • Refer & Earn
  • Blogs
No Result
View All Result
  • Login
No Result
View All Result
  • Feedback/Suggestions
  • About Us
  • Contact Us
Home Education Programin & Technology Skills

Animating CSS Grid (How To + Examples) | CSS-Tricks

admin by admin
January 25, 2023
in Programin & Technology Skills
0 0
0
Cryptocurrencies With Incredibly High Market Caps
0
SHARES
0
VIEWS
Share on FacebookShare on Twitter

I’m pleased to shine a light on the fact that the CSS grid-template-rows and grid-template-columns properties are now animatable in all major web browsers! Well, CSS Grid has technically supported animations for a long time, as it’s baked right into the CSS Grid Layout Module Level 1 spec.

But animating these grid properties only recently gained supported by all three major browsers. Shall we take a look at a few examples to get the creative juices flowing?

Example 1: Expanding sidebar

First of all, this is what we’re talking about:

A simple two-column grid. Now, before, you might not have built this using CSS Grid because animations and transitions weren’t supported, but what if you wanted the left column — perhaps a sidebar navigation — to expand on hover? Well, now that’s possible.

I know what you’re thinking: “Animating a CSS property? Easy peasy, I’ve been doing it for years!” Me too. However, I ran into an interesting snag while experimenting with a particular use case.

So, we want to transition the grid itself (specifically grid-template-columns, which is set on the .grid class in the example). But the left column (.left) is the selector that requires the :hover pseudo-class. While JavaScript can solve this conundrum easily — thanks, but no thanks — we can accomplish it with CSS alone.

Let’s walk through the whole thing, starting with the HTML. Pretty standard stuff really… a grid with two columns.

<div class="grid">
  <div class="left"></div>
  <div class="right"></div>
</div>

Putting the cosmetic CSS aside, you’ll first need to set display: grid on the parent container (.grid).

.grid {
  display: grid;
}

Next, we can define and size the two columns using the grid-template-columns property. We’ll make the left column super narrow, and later increase its width on hover. The right column takes up the rest of the remaining space, thanks to the auto keyword.

.grid {
  display: grid;
  grid-template-columns: 48px auto;
}

We know we’re going to animate this thing, so let’s go ahead and throw a transition in there while we’re at it so the change between states is smooth and noticeable.

.grid {
  display: grid;
  grid-template-columns: 48px auto;
  transition: 300ms; /* Change as needed */
}

That’s it for the .grid! All that’s left is to apply the hover state. Specifically, we’re going to override the grid-template-columns property so that the left column takes up a greater amount of space on hover.

This alone isn’t all that interesting, although it’s awesome that animations and transitions are supported now in CSS Grid. What’s more interesting is that we can use the relatively new :has() pseudo-class to style the parent container (.grid) while the child (.left) is hovered.

.grid:has(.left:hover) {
  /* Hover styles */
}

In plain English this is saying, “Do something to the .grid container if it contains an element named .left inside of it that is in a hover state.” That’s why :has() is often referred to as a “parent” selector. We can finally select a parent based on the children it contains — no JavaScript required!

So, let’s increase the width of the .left column to 30% when it is hovered. The .right column will continue to take up all the leftover space:

.grid {
  display: grid;
  transition: 300ms;
  grid-template-columns: 48px auto;
}

.grid:has(.left:hover) {
  grid-template-columns: 30% auto;
}

We could use CSS variables as well, which may or may not look cleaner depending on your personal preferences (or you might be using CSS variables in your project anyway):

.grid {
  display: grid;
  transition: 300ms;
  grid-template-columns: var(--left, 48px) auto;
}

.grid:has(.left:hover) {
  --left: 30%;
}

I love that CSS grids can be animated now, but the fact that we can build this particular example with just nine lines of CSS is even more astounding.

Here’s another example by Olivia Ng — similar concept, but with content (click on the nav icon):

Example 2: Expanding Panels

This example transitions the grid container (the column widths) but also the individual columns (their background colors). It’s ideal for providing more content on hover.

It’s worth remembering that the repeat() function sometimes produces buggy transitions, which is why I set the width of each column individually (i.e. grid-template-columns: 1fr 1fr 1fr).

Example 3: Adding Rows and Columns

This example animatedly “adds” a column to the grid. However — you guessed it — this scenario has a pitfall too. The requirement is that the “new” column mustn’t be hidden (i.e. set to display: none), and CSS Grid must acknowledge its existence while setting its width to 0fr.

So, for a three-column grid — grid-template-columns: 1fr 1fr 0fr (yes, the unit must be declared even though the value is 0!) transitions into grid-template-columns: 1fr 1fr 1fr correctly, but grid-template-columns: 1fr 1fr doesn’t. In hindsight, this actually makes perfect sense considering what we know about how transitions work.

Here’s another example by Michelle Barker — same concept, but with an extra column and lot more pizzazz. Make sure to run this one in full-screen mode because it’s actually responsive (no trickery, just good design!).

A few more examples

Because why not?

This “Animated Mondrian” is the original proof of concept for animated CSS grids by Chrome DevRel. The grid-row‘s and grid-column‘s utilize the span keyword to create the layout you see before you, and then the grid-template-row’s and grid-template-column‘s are animated using a CSS animation. It’s nowhere near as complex as it looks!

Same concept, but with more of that Michelle Barker pizzazz. Could make a nice loading spinner?

Wrapping up with a bit of nostalgia (showing my age here), the not-very-griddy animated CSS grid by Andrew Harvard. Again — same concept — it’s just that you can’t see the other grid items. But don’t worry, they’re there.





Source link

Tags: AnimatingCSSCSSTricksExamplesGrid

Latest News

  • Trending
  • Comments
  • Latest
Cryptocurrencies With Incredibly High Market Caps

Iravin Nizhal Box Office Collection Day 3 Worldwide & Budget

July 17, 2022
Celebrity couple Riteish Deshmukh, Genelia D’Souza buys BMW iX electric SUV worth Rs 1.16 crore | Electric Vehicles News

Celebrity couple Riteish Deshmukh, Genelia D’Souza buys BMW iX electric SUV worth Rs 1.16 crore | Electric Vehicles News

September 1, 2022
2022 TVS Ronin Launched; Prices Start At Rs. 1.49 lakh

2022 TVS Ronin Launched; Prices Start At Rs. 1.49 lakh

July 6, 2022
Realme Notebook Air With 4.9mm Wide Bezels to Launch on July 12; Colour Options Tipped

Realme Notebook Air With 4.9mm Wide Bezels to Launch on July 12; Colour Options Tipped

July 7, 2022
Bitcoin Could Collapse Another 50%, Says Michael “Big Short” Burry

Bitcoin Could Collapse Another 50%, Says Michael “Big Short” Burry

0
Crypto Market Loses $60 Billion As Bitcoin Dips Below $20,000

Crypto Market Loses $60 Billion As Bitcoin Dips Below $20,000

0
Bitcoin Sees Worst Quarter In 11 Years

Bitcoin Sees Worst Quarter In 11 Years

0
Ethereum (ETH) Bends Toward $1,000 As Doubt Fills Crypto Markets

Ethereum (ETH) Bends Toward $1,000 As Doubt Fills Crypto Markets

0
Peanut Alert: Various Two Rivers Hot Chocolate Products

Peanut Alert: Various Two Rivers Hot Chocolate Products

February 3, 2023
Cryptocurrencies With Incredibly High Market Caps

Migraines And Dating: How It Feels To Date Someone Who Cares

February 2, 2023
Content Traceability and Standardization | TechWhirl

Content Traceability and Standardization | TechWhirl

February 2, 2023
Alia Bhatt hails Ranbir Kapoors new song Tere Pyaar Mein from Tu Jhooti Main Makkar

Alia Bhatt hails Ranbir Kapoors new song Tere Pyaar Mein from Tu Jhooti Main Makkar

February 2, 2023
Moneyhaat News

Money Haat

You finally have someone to confide in about money. Get financial clarity as we uncover your values, spot barriers, and set achievable goals.

  • Feedback/Suggestions
  • About Us
  • Contact Us

© 2022 Money Haat - Designed By JSD Solutions.

No Result
View All Result
  • Home
  • Browse
    • Personal Development
    • Fashion & Lifestyle
    • Health & Fitness
    • Cooking & Food
    • Stock Market & Trading
    • Business & Startups
    • Technology
    • Travel & Tourism
    • Business
    • Freelancing
    • Learners Hub
  • Refer & Earn
  • Blogs

© 2022 Money Haat - Designed By JSD Solutions.

Welcome Back!

Login to your account below

Forgotten Password?

Retrieve your password

Please enter your username or email address to reset your password.

Log In

Add New Playlist

Are you sure want to unlock this post?
Unlock left : 0
Are you sure want to cancel subscription?