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

  • Creating Scrolling Text With HTML, CSS, and JavaScript
  • How to Create a Pokémon Breeding Gaming Calculator Using HTML, CSS, and JavaScript
  • Step-by-Step Guide to Creating a Calculator App With HTML and JS (With Factor Calculator Example)
  • Scrolling With Konva.js and React

Trending

  • How to Configure and Customize the Go SDK for Azure Cosmos DB
  • A Complete Guide to Modern AI Developer Tools
  • Subtitles: The Good, the Bad, and the Resource-Heavy
  • Scaling Mobile App Performance: How We Cut Screen Load Time From 8s to 2s
  1. DZone
  2. Coding
  3. Languages
  4. Pure CSS Slide-Down Animation

Pure CSS Slide-Down Animation

In this post, you'll learn how to make great user interfaces using only HTML and CSS by adding a slide-down animation into your CSS code.

By 
Joseph Zimmerman user avatar
Joseph Zimmerman
·
Aug. 30, 18 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
449.3K Views

Join the DZone community and get the full member experience.

Join For Free

Sometimes something seems like it should be really easy, but it turns out to be extremely difficult. The case we'll be checking out today is creating a slide-down animation using purely CSS. What could be so hard about that, right? 

What Are We Talking About?

If you're not sure what I mean by a "slide-down animation," check out the slideDown method from jQuery. This function will take a hidden element and make it visible by increasing the element's height from 0 to whatever the height of the element should be. That shouldn't be too hard, right?

How You Might Try to Do It

Let's take a look at some quick ideas of how you might expect to be able to do this easily. If you simply go by what I said earlier, you'd just set the height to 0, and then to expand it, set the height to the number that shows the whole element (with a transition as well; this is assumed for all of them, otherwise it won't be an animation). Something like this:

Note: This is using :target and links to activate the animations, so the lnks aren't toggles. You'll need to go back in the frame's history or press "Rerun" to toggle them off. 

Note that it works perfectly! Problem solved right? Yes and no. This works perfectly if you know the height of the element, but what if you don't know the height? We'll need a generic animation that will work no matter what height the element is. This is especially true on a responsive site, where the height can change depending on screen size.

In this case, the seemingly obvious solutions is to just set the height to auto in the expanded version's styles.

Oh how I wish this worked. This would make everything so much easier, but sadly transitions only work on numeric values. Sure, auto does eventually compute to a numeric value, but that message doesn't seem to get to transition, so it just pops right open instead of doing a nice animation.

So what can we do? How about CSS Transforms? We'll set scaleY to zero and then set it to one when it should be expanded. Will that work?

When you use a transform, it doesn't actually change the amount of space it takes up, so there are big gaps between the links instead of making that area appear at the same time the element starts expanding. So this isn't working either. So how about we take a look at the method that I discovered.

The Answer

The answer is to set everything that can affect height (e.g. vertical padding, line-height, etc.) to 0 and then set it to the value you'd like when expanded. You need to be careful with this because there are lots of things that can affect height. For example, if there were multiple paragraphs of text inside the element, you'd need to adjust the margins between those paragraphs. If you have any images, you'll need to make sure the height and vertical margins around it are also set to 0. This works better than just using height since you're far more likely to know the values of each of these properties than you are to know the height of the expandable element.

Note: I also set the text color to toggle between transparent and black to give it a fading look. This is not necessary, but looks a little nicer. You can also accomplish this with opacity.

Conclusion

I've spent days trying to figure this out in the past. There just had to be a way! But aside from using JavaScript, which is also pretty complicated, I just couldn't figure it out for the longest time. Now that I have it figured out, I hope it can help you as well. God bless and happy coding!

CSS Element HTML

Published at DZone with permission of Joseph Zimmerman, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Creating Scrolling Text With HTML, CSS, and JavaScript
  • How to Create a Pokémon Breeding Gaming Calculator Using HTML, CSS, and JavaScript
  • Step-by-Step Guide to Creating a Calculator App With HTML and JS (With Factor Calculator Example)
  • Scrolling With Konva.js and React

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!