Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • CSS Tutorial
  • CSS Exercises
  • CSS Interview Questions
  • CSS Selectors
  • CSS Properties
  • CSS Functions
  • CSS Examples
  • CSS Cheat Sheet
  • CSS Templates
  • CSS Frameworks
  • Bootstrap
  • Tailwind
  • CSS Formatter
Open In App
Next Article:
How to Use Icons to Make an Animated Effect Using CSS?
Next article icon

How to Animate Rainbow Heart from a Square using CSS ?

Last Updated : 27 Jul, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, you will learn how to make a magical heart using only CSS. Animations in CSS are a widely fascinating part of CSS. We will be creating a heart that changes color every 3 seconds. We will be creating the animation in two steps.

1. Building the heart: In this step we will be building the shape of the heart.

First we will create two divisions in the body, with the classes of square and container. Then, in style.css file, we will add some general body and class styling to make all the content centered. We will then create two circles using the :before and :after selectors and using the border-radius property for making it a circle.

This will create the shape of the heart that will be animated in the next step. The code below demonstrates the HTML and CSS code.

 

HTML File:

HTML

<!DOCTYPE html>
<html>
  
<head>
    <link rel="stylesheet" href="style.css" />
</head>
  
<body>
    <div class="container">
        <div class="square"></div>
    </div>
</body>
  
</html>
                      
                       

CSS File:

CSS

* {
    margin: 0%;
    padding: 0%;
    box-sizing: border-box;
}
  
.container {
    width: 100vw;
    height: 100vh;
    background-color: black;
    display: grid;
    place-items: center;
    align-items: center;
}
  
.square {
    width: 10rem;
    height: 10rem;
    background-color: orange;
    position: relative;
    transform: rotate(45deg);
}
  
/* Draw one of the circles */
.square::before {
    content: "";
    width: 100%;
    height: 100%;
    background-color: green;
    position: absolute;
    border-radius: 50%;
    transform: translateY(-50%);
}
  
/* Draw another of the circles */
.square::after {
    content: "";
    width: 100%;
    height: 100%;
    background-color: pink;
    position: absolute;
    border-radius: 50%;
    transform: translateX(-50%);
}
                      
                       

Note: As of now, two different colors are used for drawing the circles, so that you can understand better, but in the final program, we will take the same color for the two circles and square.

Output:

 

We can make circles of different colors so that they can be distinguished easily. If we make all the colors the same, the output would be as given below:

2. Animating the heart: In this step we add the animations of the heart.

We will use two sets of animation for the heart. One is the motion of the heart and the other is the changing of colors. We will be using keyframes for defining the animations. The colors of the heart will be defined in each of the keyframes as needed. This will toggle the colors of the heart throughout the course of the animation.

The motion of the heart when changing from a square to the heart can be added using the transform property in one of the keyframes. This will cause the transformation from a square to the heart shape.

Complete Program

HTML

<!DOCTYPE html>
<html>
  
<head>
    <style>
        * {
            margin: 0%;
            padding: 0%;
            box-sizing: border-box;
        }
  
        .container {
            width: 100vw;
            height: 100vh;
            background-color: black;
            display: grid;
            place-items: center;
            align-items: center;
        }
  
        .square {
            width: 10rem;
            height: 10rem;
            background-color: pink;
            position: relative;
            transform: rotate(45deg);
            animation: beater 3s linear infinite;
        }
  
        .square::before {
            content: "";
            width: 100%;
            height: 100%;
            background-color: pink;
            position: absolute;
            border-radius: 50%;
            transform: translateY(-50%);
            animation: beater 3s linear infinite;
        }
  
        .square::after {
            content: "";
            width: 100%;
            height: 100%;
            background-color: pink;
            position: absolute;
            border-radius: 50%;
            transform: translateX(-50%);
            animation: beater 3s linear infinite;
        }
  
        /* Define the keyframes for the animation */
        @keyframes beater {
            0% {
                background: red;
            }
  
            15% {
                background: orange;
            }
  
            30% {
                transform: scale(0.5);
                background: yellow;
            }
  
            45% {
                background: greenyellow;
            }
  
            60% {
                background: blue;
            }
  
            75% {
                background: indigo;
            }
  
            100% {
                background: violet;
            }
        }
    </style>
</head>
  
<body>
    <div class="container">
        <div class="square"></div>
    </div>
</body>
  
</html>
                      
                       

Output:



Next Article
How to Use Icons to Make an Animated Effect Using CSS?

A

anshitaagarwal
Improve
Article Tags :
  • CSS
  • Web Technologies
  • CSS-Properties
  • CSS-Questions

Similar Reads

  • How to Use Icons to Make an Animated Effect Using CSS?
    Icons are small, visual representations of actions or content. They can add a lot of style and functionality to a website, especially when combined with animations. we'll explore how to use CSS to animate icons, giving your website a lively and interactive touch. These are the following approaches f
    3 min read
  • How to create a marquee effect using CSS ?
    In this article, we will be creating the marquee effect by using HTML and CSS. This effect can be created by using the <marquee> tag in HTML, but we will not use this tag as it is not widely used nowadays. We will create and design a marquee effect by using the CSS classes and styling properti
    2 min read
  • How to create Shooting Star Animation Effect using CSS ?
    The Shooting Star effect is one of the coolest background effects that is used for dark-themed websites. Shooting Stars Animation is an extraordinary illustration of a loading screen that grabs your eye for a considerable length of time for the remainder of the content to load on the website. This e
    4 min read
  • How to Create a Gradient Shadow using CSS ?
    A gradient shadow in CSS refers to creating a shadow effect that transitions smoothly from one color to another, using gradients. While the box-shadow property doesn't support gradients directly, you can simulate this effect by layering a semi-transparent gradient as the background or using pseudo-e
    2 min read
  • How to create a rainbow using HTML5 ?
    To create a rainbow using HTML5, utilize the <svg> element to draw multiple <circle> elements with varying radii and gradient fills. Each circle, centered at the same point, represents a color of the rainbow, achieving a vibrant and visually appealing effect in web design. ApproachFirstl
    2 min read
  • How to Move an Element in a Circular Path using CSS ?
    In this article, we will see how to move an element in a circular path using CSS. Approach: We will create an HTML element, and add some CSS styles to it. We will use the animation property to add an animation effect to move the element in a circular path.Add some CSS styles to set the width, height
    2 min read
  • How to Create a Ribbon using CSS?
    In this article, we will learn how to create a ribbon using CSS. PrerequisitesHTMLCSSApproachThe structure consists of a <button> element with the class "btn" containing the text "GFG DSA Course". Inside the button, there is an <span> element with the class "ribbon" containing the text "
    2 min read
  • How to animate a straight line in linear motion using CSS ?
    Animating a straight line in linear motion using CSS involves moving an element along a straight path at a constant speed. This is achieved with CSS animations and the @keyframes rule, specifying positions and using properties like left, right or transform. ApproachHTML Approach:Structure Setup: The
    2 min read
  • How to make a morph spinner animation using CSS ?
    Morphing is the rotating animation of certain objects changing their shape. The changing of shape is called morphing. In this article, we will make a morph spinner that will act as a loading animation by using CSS. This method can also be used to make eye-catching card backgrounds, banners, buttons,
    3 min read
  • How to Create Jumping Text 3D Animation Effect using CSS ?
    In this article, you will learn to implement Jumping Texts animation effect using simple HTML and CSS. HTML is used to create the structure of the page and CSS is used to set the styling. HTML Code: In this section, we will design the basic structure of the body. [GFGTABS] html <!DOCTYPE html>
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences