CSS for Web Development

0% completed

Previous
Next
Create an Animated Loading Spinner Using CSS Keyframes

Problem Statement

  • Build a circular loading spinner purely with CSS.
  • The spinner must rotate continuously using keyframe animations.
  • Use CSS transitions or animations to ensure smooth, infinite rotation.
  • Center the spinner on the page both horizontally and vertically.
  • Ensure the spinner is responsive and maintains its proportions on different screen sizes.

Solution

HTML

. . . .

Explanation:

  • Centering the Spinner:

    • The body and html are set to 100% height and use Flexbox (display: flex) with justify-content: center and align-items: center. This centers the spinner both horizontally and vertically on the page.
  • Spinner Styling:

    • The .spinner class defines a circular element by setting width and height to 80px and applying a border-radius: 50%.
    • A thick border is applied; the entire spinner has an 8px border where the majority is a light grey (#ddd), but the top border is a contrasting color (#0077cc). This colored segment creates the visual effect of movement during rotation.
  • Animation:

    • The animation: spin 1s linear infinite; property applies a keyframe animation named spin, which rotates the spinner from 0° to 360° over 1 second using a linear timing function, and repeats infinitely.
    • The keyframes defined under @keyframes spin smoothly animate the rotation, ensuring a continuous and smooth spinning effect.
  • Responsiveness:

    • While the spinner’s dimensions are fixed in pixels for this example, you can easily change them to relative units (like vw or em) if needed. The central alignment via Flexbox ensures it remains properly positioned on different devices.

This solution meets all the challenge requirements by creating a fully functional, animated loading spinner using only HTML and CSS, making it responsive and visually engaging.

.....

.....

.....

Like the course? Get enrolled and start learning!
Previous
Next