A step-by-step guide on making an animation series with html and css keyframes

I've been working on creating a captivating animation using HTML and CSS keyframes, but I seem to have hit a roadblock. Despite trying different rotations and transforms, I can't seem to achieve the desired effect depicted in the image below. The animation involves the boxes folding inwards to conceal the text "Hi," followed by unfolding outwards to reveal the text again. While the box colors are not a problem, the animation itself is proving tricky.

Here's what I've managed to accomplish so far:

/* styles.css */
.container {
  display: flex;
  justify-content: center;
  align-items: center; 
  height: 400px; 
}

.group {
  display: flex;
  flex-direction: column;
  justify-content: center;
  width: 200px;
  margin: 0 15px; 
}

.topleft {
  width: 100px; 
  height: 75px; 
  border-radius: 10px;
  margin: 0 auto;
  transform-origin: bottom left;
}
... (CSS code continues) ...
  <div class="container">
    <div class="group left">
      <div class="box topleft steelblue"></div>
      <div class="box bottomleft yellow"></div>
    </div> ... (HTML code continues) ...

https://i.sstatic.net/kN1MW.gif

Answer №1

Utilizing the transform origin for this animation poses a slight challenge as it can be confusing. To simplify things, I've created a snippet with the correct animation. Essentially, it relies on a single transform statement for all boxes, with the animation direction controlled by a CSS variable individually set on each box (or combined):

transform: translateX(calc(-100% * var(--xdir,1))) rotateZ(calc(90deg * var(--rdir,1)));

By organizing your boxes logically and implementing something like grid, you can ensure they always appear well-balanced. Rather than physically moving the boxes (using left or top), utilize transforms as they impact only the visual appearance of your page, not its layout. This approach helps prevent unintended interactions between the boxes. Give this a try:

document.body.addEventListener('click', event => {
    
  document.querySelector('main').classList.toggle('expanded');
  
});
main {
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-template-rows: 1fr 1fr;
}
main span {
  /* Text box style */
  grid-column: 1 / span 2;
  grid-row: 1 / span 2;
  margin: auto;
  display: block;
}
main span.box {
  /* Square box style */
  margin: 0;
  display: block;
  width: 100px;
  height: 100px;
  background: red;
  transition: transform .5s;
}
main span.box.tl,
main span.box.bl {
  grid-column: 1;
  margin-left: auto;
}
main span.box.tr,
main span.box.br {
  grid-column: 2;
  margin-right: auto;
  --xdir: -1;
}
main span.box.tl,
main span.box.tr {
  grid-row: 1;
  margin-top: auto;
}
main span.box.bl,
main span.box.br {
  grid-row: 2;
  margin-bottom: auto;
}
main span.box.bl,
main span.box.tr {
  background: blue;
}
main span.box.tr,
main span.box.bl {
  --rdir: -1;
}

main.expanded span.box {
  transform: translateX(calc(-100% * var(--xdir,1))) rotateZ(calc(90deg * var(--rdir,1)));
}
<main>
  <span>Hi!</span>
  <span class="box tl"></span>
  <span class="box tr"></span>
  <span class="box bl"></span>
  <span class="box br"></span>
</main>

Alternatively, consider adding a looping animation:

main {
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-template-rows: 1fr 1fr;
}
main span {
  /* Text box style */
  grid-column: 1 / span 2;
  grid-row: 1 / span 2;
  margin: auto;
  display: block;
}
main span.box {
  /* Square box style */
  margin: 0;
  display: block;
  width: 100px;
  height: 100px;
  background: red;
  transition: transform .5s;
}
main span.box.tl,
main span.box.bl {
  grid-column: 1;
  margin-left: auto;
}
main span.box.tr,
main span.box.br {
  grid-column: 2;
  margin-right: auto;
  --xdir: -1;
}
main span.box.tl,
main span.box.tr {
  grid-row: 1;
  margin-top: auto;
}
main span.box.bl,
main span.box.br {
  grid-row: 2;
  margin-bottom: auto;
}
main span.box.bl,
main span.box.tr {
  background: blue;
}
main span.box.tr,
main span.box.bl {
  --rdir: -1;
}

@keyframes flip {
  40% { transform: none; }
  80% { transform: var(--flip-transform); }
  to { transform: var(--flip-transform); }
}

main span.box {
  --flip-transform: translateX(calc(-100% * var(--xdir,1))) rotateZ(calc(90deg * var(--rdir,1)));
  animation: flip 1s 1s alternate infinite;
  animation-fill-mode: both;
}
<main>
  <span>Hi!</span>
  <span class="box tl"></span>
  <span class="box tr"></span>
  <span class="box bl"></span>
  <span class="box br"></span>
</main>

Answer №2

Check out this creative concept. Hover over to experience the animation:

.container {
  width: 200px;
  aspect-ratio:1;
  display: grid;
  place-content: center;
  margin: auto;
  font-size: 40px;
  position: relative;
}
.container:before,
.container:after,
.container span:before,
.container span:after {
  content:"";
  position: absolute;
  width: 50%;
  height: 50%;
  transition: 1s;
}
.container:before {
  inset: 0 auto auto 0;
  transform-origin: 0 0;
  background: pink;
  rotate: var(--r,0deg);
}
.container:after {
  inset: 0 0 auto auto;
  transform-origin: 100% 0;
  background: lightblue;
  rotate: calc(-1*var(--r,0deg));
}
.container span:before {
  inset: auto auto 0 0;
  transform-origin: 0 100%;
  background: red;
  rotate: calc(-1*var(--r,0deg));
}
.container span:after {
  inset: auto 0 0 auto;
  transform-origin: 100% 100%;
  background: blue;
  rotate: var(--r,0deg);
}

.container:hover {
  --r: 90deg;
}
<div class="box">
  hello
  <span></span>
</div>

Answer №3

Here is a code snippet that I found:

p, #boxes {
  margin: 0;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 100px;
  text-align: center;
}

.box {
  width: 50px;
  height: 50px;
  float: left
}

.green {
  background-color: green
 }
 
 .red {
  background-color: red
 }
 
 .yellow {
  background-color: yellow
 }
 
 .blue {
  background-color: blue
 }
 
@keyframes spinAndMoveToTheLeft {
  0% {
    transform: translateX(0) rotate(0deg);
  }
  100% {
    transform: translateX(-50px) rotate(90deg);
  }
}


.spin-animation-left {
  animation: spinAndMoveToTheLeft 1s linear infinite;
  animation-direction: alternate-reverse;
}

@keyframes spinAndMoveToTheRight {
  0% {
    transform: translateX(0) rotate(0deg);
  }
  100% {
    transform: translateX(50px) rotate(90deg);
  }
}


.spin-animation-right {
  animation: spinAndMoveToTheRight 1s linear infinite;
  animation-direction: alternate-reverse;
}
<p>Hi</p>
<div id="boxes">
  <div class="box green spin-animation-left"></div>
  <div class="box red spin-animation-right"></div>
  <div class="box yellow spin-animation-left"></div>
  <div class="box blue spin-animation-right"></div>
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Utilize AJAX to fetch and display the response from a Node JS route directly onto an HTML page

At the moment, I am retrieving client-side HTML/JS inputs (var1 and var2) which are then sent to server-side Node JS. The input values are stored in variables through an AJAX post call made to a specific route. What I want now is to define the values of v ...

I'm curious about the method used to create this body fadeIn effect

Recently, I stumbled upon the website "". I must say, it is truly remarkable. I can't help but wonder how the content on this page is cleverly faded in and slides up. This specific page caught my attention: ...

Issue with splitting a JavaScript function in JQuery within the Wordpress platform can cause errors

My split function seems to be causing an error, as shown in this console error image. There is a hidden input field within the PHP file: <input class="file-id" name="_file-id" type="hidden" value="<?php echo esc_attr($file_ids); ?>" /> I hav ...

Display a pop-up when hovering over a layer with react-leaflet

I am attempting to display a popup when hovering over a layer in react leaflet. Utilizing GeoJson to render all layers on the map and onEachFeature() to trigger the popup on hover, I encountered an issue where the popup only appeared upon click, not hover. ...

Layering elements using CSS and HTML to create a background effect

I have created a menu using div elements and I want one of the menu items to have a background that extends behind it to the body. Here is the structure: body { background-image: url(path/body.png); } #menu { overflow: hidden; width: 400px; ba ...

Creating a border indentation for a table row using the <tr> tag

In my database, there is a table that shows the parent-child relationship. Check out this link for more details The HTML structure of the table is as follows: <table> <tr class="parent_row"> <td >1</td> <td>2</t ...

Navigating the battleground between native and HTML5 mobile applications

As an experienced iOS developer with knowledge in Windows Phone and Android, I have observed the increasing popularity of HTML5 web-apps over native apps. This shift towards HTML5 development frustrates me, as a strong argument can be made for native app d ...

Unable to attach the event listener for the 'onchange' event to a div nested within a ul element

Here is the HTML code I am working with: <ul> <li> <a href="#"> <div></div> Actions <div></div> </a> <ul> <li> <a> &l ...

Show items in the sequence of clicking

Is there a way to display elements in the order they're clicked, rather than their order in the HTML using jQuery? For example: CSS code: .sq{ display:none; } HTML Code: <a href="#" id="1">A</a> <a href="#" id="2">B</a> ...

Ways to maximize the amount of content contained within a box

My current struggle involves meeting the requirements of my customers. I have a small box (230px x 200px) with an image that will display a list on hover. However, the customer now wants more items in the list than can fit in the box. Scrolling within the ...

CSS Dilemma: Font Refusing to Change

Hey everyone, I'm new to web development and currently building a basic website on my computer. However, I'm facing an issue where the font I want to use is not changing. Both my HTML and CSS files are located in the correct folder and I am confi ...

Customizing the appearance of individual columns in the Material-UI DataGrid

My goal is to center an IconButton within a DataGrid table, all of which are MUI components. Despite reading the documentation and trying various methods, I haven't achieved the desired result yet. In my latest attempt, I implemented the following: G ...

Dynamic column group in Datatable - toggle visibility based on user selection

In my application, I am utilizing Jquery datatable with a table that includes the following columns: Name, Office, A1, B1, Diff1, A2, B2, Diff2, A3, B3, Diff3, A4, B4, Diff4 Additionally, there is a select box containing the options: 1. All 2. Diff1 3. D ...

How to Use CSS to Perfectly Align Form Elements in the Center of

No matter what I try, I just can't seem to get these form elements centered on the page using CSS. Here is my code snippet: http://jsfiddle.net/VJLst/1/ <style> #divQuizFillBlank { margin-left: auto; margin-right: auto; } #textQuizFill ...

Insert HTML code that is activated when the selection is modified

I have a simple dropdown menu in my HTML code. HTML <select name="breed" onchange="breedChanged()"> <?php while ($row = gdrcd_query($result, 'fetch')){ ?> <option value="<?php echo $row['id_breed']; ?& ...

Event dedicated to the selection of mobile options

I am facing an issue with binding an event to the options of a select tag on mobile devices. The code inside the click event handler doesn't seem to be working and I'm unsure of the reason behind it. My assumption is that perhaps the click event ...

In my HTML document, I have three identical Id's and I need to figure out how to target the third one using CSS. Can you help

I am working on an HTML document that contains multiple instances of ids and classes. Here is a snippet of the code: <!DOCTYPE html> <html> <body> <div id="Caption_G" class="editor-group"> editor-group<br /> ...

Reducing size and Assembling JavaScript and CSS

I find myself in a bit of a dilemma when it comes to using just one JS file and one CSS file for a website. Let me elaborate: As someone who mainly works with CMS platforms like Joomla, I often run into issues during page speed tests where experts recomme ...

Tips for arranging a menu horizontally with buttons in CSS

I am currently working with a dropdown menu that I coded using CSS, similar to the one shown at . My issue is that I have 4 menu items and I want the total width of the parent div to be divided equally among each item, regardless of the resolution. Curre ...

Certain browsers have difficulty running CSS keyframes animations

As I work on my website, I have integrated a CSS keyframes animation that is triggered by clicking a link connected to a JavaScript function. Here is an excerpt from my CSS file: #banner { background-color: #00BBD2; position: absolute; width: 100%; heigh ...