avatarRyan Mambou

Summary

The provided content is a step-by-step guide on how to create a smooth marquee animation using HTML and CSS, emphasizing the importance of a seamless loop for an appealing visual effect on a website.

Abstract

The web content serves as a comprehensive tutorial for implementing a marquee animation, a visually engaging feature for websites. It begins with an introduction to the concept and a demonstration of the final product, emphasizing the simplicity and enhancement such an animation brings to web design. The article outlines the prerequisites, including basic knowledge of HTML and CSS, and guides the reader through organizing the project files, structuring the HTML document with the necessary elements and classes, and styling the animation with CSS. The keyframes animation technique is detailed, along with the importance of having a duplicate set of images to maintain the continuous motion of the marquee. The styling section includes a pause-on-hover feature, adding interactivity to the animation. The tutorial concludes with a reminder to ensure the marquee slider's width is greater than the marquee container's width for optimal visual flow and invites the reader to engage with the content by clapping, following, and exploring additional resources from the 'In Plain English' community.

Opinions

  • The author believes that simplicity is a key element of brilliance in design, as evidenced by the emphasis on the marquee animation's simplicity and its impact on website enhancement.
  • The importance of a seamless animation loop is highlighted to avoid disrupting the user experience with noticeable resetting of the animated elements.
  • Duplicating the marquee__slider content is presented as a critical step in achieving the desired continuous animation effect.
  • The tutorial is written with the assumption that readers have a basic understanding of HTML and CSS, indicating a target audience of beginning to intermediate web developers.
  • Interactive features, such as pausing the animation on hover, are suggested to improve user engagement and experience.
  • The author encourages reader interaction and community engagement by inviting readers to clap, follow, and explore other platforms associated with 'In Plain English'.

How to Implement a Marquee Animation using HTML and CSS

cover image

What’s up coders? Today, we are going to implement a marquee animation. Repeating this sounds weird because I guess if you clicked on the headline, you knew what you were subscribing for lol, silly me.

This is a demo of what we are about to build:

demo

It is very simple animation but brings so much enhancement to a website. I guess the person who said “simplicity is the key of brillance” was right.

Prerequisites

In order for us to achieve our objective we need to have a basic knowledge of HTML and CSS.

Let’s get our hands dirty now!

1. Organizing the project 📁

NB: You may skip this step if you have your project setup already.

In the root directory of our project, let’s create an index.html file, an assets folder (to place our images) and a style.css file. The final result should be something like:

Project structure

2. Page Structure

In the index.html file, paste the following code:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="stylesheet" href="style.css" />
    <link
      rel="stylesheet"
      href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.2/css/all.min.css"
      integrity="sha512-SnH5WK+bZxgPHs44uWIX+LLJAJ9/2PkPKZ5QiAj6Ta86w+fsb2TkcmfRyVX3pBnMFcV7oQPJkl9QevSCWr3W6A=="
      crossorigin="anonymous"
      referrerpolicy="no-referrer"
    />
    <link rel="preconnect" href="https://fonts.googleapis.com" />
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
    <link
      href="https://fonts.googleapis.com/css2?family=Playwrite+NL:[email protected]&display=swap"
      rel="stylesheet"
    />
    <title>Marquee Animation</title>
  </head>
  <body>
    <h1>Marquee Animation</h1>
    <div class="marquee">
      <div class="marquee__slider">
        <img src="./assets/bmw.jpg" />
        <img src="./assets/apple.png" />
        <img src="./assets/mercedes.jpg" />
        <img src="./assets/tesla.jpg" />
      </div>
      <div class="marquee__slider">
        <img src="./assets/bmw.jpg" />
        <img src="./assets/apple.png" />
        <img src="./assets/mercedes.jpg" />
        <img src="./assets/tesla.jpg" />
      </div>
    </div>
  </body>
</html>
  • As you can see, we have a marque class that encloses two marquee__slider classes.
  • The marquee__slider classes contain identical images in the same order intentionally to create a seamless animation. This ensures that when the first slider finishes moving across the page, the second one continues before the first returns to its starting position.

If we fail to add a second identical marquee__slider class, the animation will look like this:

demo when marquee__slider is not duplicated

3. Styling 🎨

In the style.css file, let’s paste the following code:

* {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
}

body {
  display: flex;
  height: 100vh;
  width: 100vw;
  flex-direction: column;
  justify-content: center;
  gap: 3rem;
  align-items: center;
  background-color: #f7f7f7;
  font-family: "Playwrite NL", cursive;
}

.marquee {
  border: 2px solid black;
  display: flex;
  overflow: hidden;
  white-space: nowrap;
  width: 40%;
}
.marquee__slider {
  display: flex;
  flex-direction: row;
  object-fit: cover;
  padding: 5px;
  animation: marquee linear 5s infinite;
}

img {
  width: 150px;
  height: 150px;
  object-fit: cover;
}

.marquee:hover .marquee__slider {
  animation-play-state: paused;
}

@keyframes marquee {
  from {
    transform: translateX(0%);
  }
  to {
    transform: translateX(-100%);
  }
}
  • The key part to our styling is the key frame we have created. Here we are moving the marquee__slider from right to left and immediately as the animation ends, it is moved back to it’s initial position for the animation to start back.
  • Notice the animation property in the marquee__slider class. We added the animation-name (see key frame) plus other properties such as animation-duration, animation-timing-function and animation-iteration-count (More on this here).
  • We also made the animation to stop on hover thanks to the lines:
.marquee:hover .marquee__slider {
  animation-play-state: paused;
}

Hot take ❗:

  • For this animation to work correctly, you must always make sure that the width of your marquee_slider class is greater than that of the marquee class because when the animation ends, the slider is moved back to 0% and of your slider is too small, the user will notice it moving back to position and the animation will be less appealing to see.

Just like that, we’ve finished implementing our smooth marquee. Feel free to leave a clap if you enjoyed it!

In Plain English 🚀

Thank you for being a part of the In Plain English community! Before you go:

Web Development
HTML
CSS
Frontend Development
Animation
Recommended from ReadMedium