CSS Project: Building a Pure CSS Animated Pricing Table

Written by

in

In the digital landscape, a clear and visually appealing pricing table is crucial for any business offering services or products. It’s often the first thing potential customers see when evaluating your offerings. A well-designed pricing table can significantly impact conversion rates by making it easy for users to understand different plans and their associated features. However, creating an engaging and functional pricing table can be a challenge, especially for beginners. The goal is to present complex information in a digestible format while also attracting attention and encouraging interaction. In this project, we’ll dive into the world of CSS and create a fully animated pricing table that’s both informative and visually stunning. This project is ideal for those looking to expand their CSS skills and create something useful in the process.

Understanding the Basics: HTML Structure

Before we jump into the CSS magic, let’s establish the HTML foundation. The HTML structure provides the content and organization of our pricing table. We’ll use semantic HTML elements to ensure accessibility and maintainability. Here’s a basic outline:

<div class="pricing-table">
  <div class="pricing-plan">
    <h3>Basic</h3>
    <p class="price">$9.99</p>
    <ul>
      <li>Feature 1</li>
      <li>Feature 2</li>
      <li>Feature 3</li>
    </ul>
    <button>Choose Plan</button>
  </div>
  <div class="pricing-plan">
    <h3>Standard</h3>
    <p class="price">$19.99</p>
    <ul>
      <li>Feature 1</li>
      <li>Feature 2</li>
      <li>Feature 3</li>
      <li>Feature 4</li>
    </ul>
    <button>Choose Plan</button>
  </div>
  <div class="pricing-plan">
    <h3>Premium</h3>
    <p class="price">$29.99</p>
    <ul>
      <li>Feature 1</li>
      <li>Feature 2</li>
      <li>Feature 3</li>
      <li>Feature 4</li>
      <li>Feature 5</li>
    </ul>
    <button>Choose Plan</button>
  </div>
</div>

Let’s break down the HTML:

  • <div class="pricing-table">: This is the main container for the entire pricing table.
  • <div class="pricing-plan">: Each of these divs represents an individual pricing plan (e.g., Basic, Standard, Premium).
  • <h3>: The heading for each plan (e.g., “Basic”).
  • <p class="price">: The price of the plan.
  • <ul> and <li>: An unordered list containing the features included in each plan.
  • <button>: A button to encourage users to select the plan.

This structure is clean, semantic, and easy to understand. It also provides clear hooks for applying our CSS styles and animations.

Styling with CSS: The Foundation

Now, let’s move on to the CSS. We’ll start with the basic styling to structure the table and make it readable. Here’s a basic CSS structure to get you started:

.pricing-table {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-wrap: wrap;
  width: 100%;
  padding: 20px;
}

.pricing-plan {
  border: 1px solid #ccc;
  border-radius: 5px;
  padding: 20px;
  margin: 10px;
  width: 300px;
  text-align: center;
}

.pricing-plan h3 {
  font-size: 1.5rem;
  margin-bottom: 10px;
}

.price {
  font-size: 2rem;
  font-weight: bold;
  margin-bottom: 20px;
}

.pricing-plan ul {
  list-style: none;
  padding: 0;
}

.pricing-plan li {
  margin-bottom: 10px;
}

.pricing-plan button {
  background-color: #007bff;
  color: white;
  border: none;
  padding: 10px 20px;
  border-radius: 5px;
  cursor: pointer;
  transition: background-color 0.3s ease;
}

.pricing-plan button:hover {
  background-color: #0056b3;
}

Let’s break down the CSS:

  • .pricing-table: We use `display: flex` to arrange the pricing plans horizontally and `flex-wrap: wrap` to make the plans wrap to the next line on smaller screens. We also use `justify-content: center` to center the plans horizontally and `align-items: center` to center them vertically.
  • .pricing-plan: Each plan is given a border, padding, and margin to create visual separation. The `width` is set to control the plan’s size.
  • h3: Styles the plan headings.
  • .price: Styles the price display.
  • ul and li: Removes default list styles and adds margin for spacing.
  • button: Styles the button, including a hover effect for user interaction.

With this basic CSS, you should now have a functional pricing table. However, it’s not very engaging. Let’s add some animation to make it more appealing.

Adding Animation: Enhancing the User Experience

Animations can significantly enhance the user experience by providing visual feedback and making the table more interactive. We’ll focus on a few key animations:

  • Hover Effects: Subtle animations on hover can draw the user’s attention to interactive elements.
  • Entrance Animations: When the page loads, we can animate the plans to slide in or fade in.

Hover Effects

Let’s add a subtle animation to the pricing plans when a user hovers over them. We’ll use the `transform` property to scale the plan slightly and add a box-shadow to give it depth. Also, let’s add an animation to the button too.

.pricing-plan:hover {
  transform: scale(1.05);
  box-shadow: 0px 5px 15px rgba(0, 0, 0, 0.1);
  transition: transform 0.3s ease, box-shadow 0.3s ease;
}

.pricing-plan button {
  transition: all 0.3s ease;
}

.pricing-plan button:hover {
  transform: scale(1.05);
  box-shadow: 0px 5px 15px rgba(0, 0, 0, 0.1);
}

Here’s what this code does:

  • .pricing-plan:hover: When the user hovers over a plan, the `transform: scale(1.05)` property slightly increases the size. The `box-shadow` adds a shadow to give the illusion of depth.
  • transition: transform 0.3s ease, box-shadow 0.3s ease: This ensures a smooth transition for the scaling and shadow effects.
  • .pricing-plan button:hover: On hover, the button also scales up and has a shadow.

Entrance Animations

To make the pricing table more dynamic when the page loads, we’ll add an entrance animation. We’ll use the `opacity` and `transform` properties to create a fade-in and slide-up effect. We’ll also use the `transition` property to control the animation duration and timing.

First, let’s add an initial state to the pricing plans. Add these styles to the `pricing-plan` class:

.pricing-plan {
  /* Existing styles */
  opacity: 0;
  transform: translateY(20px);
  transition: opacity 0.5s ease, transform 0.5s ease;
}

Now, let’s add a class to the `.pricing-plan` when the element is visible on the screen. We can do this using JavaScript or by adding a class manually. For simplicity, let’s add it manually for now. We will add the class name “active” to the HTML, for example:

<div class="pricing-plan active">
  <h3>Basic</h3>
  <p class="price">$9.99</p>
  <ul>
    <li>Feature 1</li>
    <li>Feature 2</li>
    <li>Feature 3</li>
  </ul>
  <button>Choose Plan</button>
</div>

Then, add the following CSS to make the transition:

.pricing-plan.active {
  opacity: 1;
  transform: translateY(0);
}

Here’s how this works:

  • opacity: 0; and transform: translateY(20px);: Initially, the plan is hidden and shifted down.
  • transition: opacity 0.5s ease, transform 0.5s ease;: This ensures a smooth transition.
  • .pricing-plan.active: When the “active” class is added, the opacity becomes 1 (visible), and the plan moves back to its original position.

To make the entrance animation more dynamic, you can use JavaScript to add the “active” class when the elements are in the viewport. This will make the animation happen when the user scrolls down the page.

Advanced Animation Techniques

Let’s explore some more advanced animation techniques to elevate your pricing table.

Animation on Price

You can add a subtle animation to the price to make it more noticeable. For this, we will animate the price when the page loads.

.price {
    font-size: 2rem;
    font-weight: bold;
    margin-bottom: 20px;
    opacity: 0;
    transform: translateY(10px);
    transition: opacity 0.5s ease, transform 0.5s ease;
}

.pricing-plan.active .price {
    opacity: 1;
    transform: translateY(0);
}

In this example, we’ve set the initial `opacity` of the price to 0 and translated it down slightly. When the parent element (`.pricing-plan`) has the “active” class, the price fades in and slides up.

Animation on Features

You can also animate the features to provide a more engaging experience. Let’s animate the features to slide in one by one.

.pricing-plan ul li {
    opacity: 0;
    transform: translateX(-20px);
    transition: opacity 0.3s ease, transform 0.3s ease;
}

.pricing-plan.active ul li:nth-child(1) {
    transition-delay: 0.2s;
    opacity: 1;
    transform: translateX(0);
}

.pricing-plan.active ul li:nth-child(2) {
    transition-delay: 0.4s;
    opacity: 1;
    transform: translateX(0);
}

.pricing-plan.active ul li:nth-child(3) {
    transition-delay: 0.6s;
    opacity: 1;
    transform: translateX(0);
}

Here’s how we’re animating the list items:

  • Each list item starts with opacity: 0 and is translated to the left.
  • The nth-child selector targets each list item individually.
  • transition-delay is used to stagger the animation.
  • When the parent element has the “active” class, the list items fade in and slide to their original positions.

You can adjust the `transition-delay` values to control the timing of the animations.

Responsive Design: Adapting to Different Screen Sizes

In today’s world, it’s essential to ensure your pricing table looks great on all devices, from desktops to smartphones. This means making it responsive. Here’s how to make your pricing table responsive using CSS media queries:

@media (max-width: 768px) {
  .pricing-table {
    flex-direction: column;
    align-items: center;
  }

  .pricing-plan {
    width: 90%;
    margin: 10px 0;
  }
}

Let’s break down the media query:

  • @media (max-width: 768px): This media query applies styles when the screen width is 768px or less (typical for tablets and smaller devices).
  • flex-direction: column;: Changes the flex direction to column, stacking the pricing plans vertically.
  • align-items: center;: Centers the plans horizontally.
  • width: 90%;: Sets the width of the plans to 90% of the container, allowing them to fill the screen more effectively.
  • margin: 10px 0;: Adjusts the margins for better spacing.

By using media queries, you can customize the layout and appearance of your pricing table to fit different screen sizes. This ensures a consistent and user-friendly experience across all devices.

Common Mistakes and How to Fix Them

When creating a CSS animated pricing table, several common mistakes can occur. Here’s how to avoid or fix them:

  • Ignoring the HTML Structure: A well-structured HTML document is the foundation of any good website. Make sure you use semantic HTML elements and properly nest your elements.
  • Over-Animating: Too many animations can be distracting and slow down your website. Use animations sparingly and ensure they enhance the user experience.
  • Not Testing on Different Devices: Always test your pricing table on different devices and screen sizes to ensure it’s responsive and looks good everywhere.
  • Using Inline Styles: Avoid using inline styles (styles directly in your HTML). This makes your code harder to maintain. Instead, use CSS classes and external stylesheets.
  • Not Optimizing for Performance: Large animations can impact performance. Optimize your CSS by using hardware acceleration where possible.

Summary / Key Takeaways

We’ve covered the essential steps to create a pure CSS animated pricing table. We started with the HTML structure, then added basic CSS for layout and styling. We enhanced the user experience by adding hover effects and entrance animations. We then looked at advanced animation techniques to make it even more engaging. We also explored responsive design using media queries to ensure our table looks great on any device. Remember the key takeaways:

  • Start with a solid HTML foundation: Use semantic elements to structure your content.
  • Prioritize readability and clarity: Make sure the table is easy to understand.
  • Use animations strategically: Enhance the user experience without being distracting.
  • Test on different devices: Ensure your table is responsive.

By following these steps, you can create a dynamic and engaging pricing table that will make a positive impact on your website visitors.

Optional FAQ Section

Here are some frequently asked questions about creating a CSS animated pricing table:

Q: Can I use JavaScript for the animations?

A: Yes, while this tutorial focuses on CSS animations, you can also use JavaScript to create more complex animations and interactions. JavaScript provides more flexibility and control.

Q: How do I handle different screen sizes?

A: Use CSS media queries to adjust the layout and styling of your pricing table for different screen sizes. This ensures your table is responsive and looks good on all devices.

Q: Can I customize the colors and fonts?

A: Absolutely! Customize the CSS to match your brand’s colors and fonts. Modify the `color`, `background-color`, `font-family`, and other CSS properties to achieve your desired look.

Q: How can I improve the performance of my animations?

A: Use hardware acceleration (e.g., `transform: translateZ(0);`) to improve animation performance. Optimize your CSS and avoid complex animations that can slow down your website.

Q: Where can I find more examples and inspiration?

A: Websites like CodePen, Dribbble, and Behance are great resources for finding inspiration and examples of CSS animations and pricing tables.

Building a CSS animated pricing table is a fantastic way to enhance your web development skills and create a visually appealing and functional element for your website. This project provides a solid foundation for understanding CSS animations and responsive design. By experimenting with different styles and animations, you can create a unique and engaging pricing table that will attract and retain your users. The combination of well-structured HTML, thoughtful CSS styling, and subtle animations will not only make your website look more professional but also improve user engagement, ultimately leading to higher conversion rates and a better user experience. Take the concepts learned here, and use them as a springboard to explore more advanced techniques and create even more captivating designs. Embrace the power of CSS to make your websites shine.