CSS Button With Waving Text Effect

CSS Button With Waving Text Effect
Code Snippet:Super Mario Bros. Wonder Button
Author: Nicolas Jesenberger
Published: March 4, 2024
Last Updated: March 4, 2024
Downloads: 420
License: MIT
Edit Code online: View on CodePen
Read More

This code creates a button with animated text. It uses CSS for styling and animations. The button showcases a waving text effect when hovered over. This effect is achieved using transitions and animations. Similarly, JavaScript dynamically separates characters for animation purposes. The waving motion is applied individually to each character. Overall, it’s a creative way to style buttons with engaging text effects.

Moreover, you can use this code to enhance buttons on your website. It’s great for call-to-action buttons or special feature highlights. The waving text effect adds an engaging touch, grabbing user attention instantly.

How to Create CSS Button With Waving Text Effect

1. Start by setting up the basic HTML structure for the button. Utilize the <button> element and include nested <span> elements for styling layers.

<button class="button" type="button">
  <span class="button-outline">    
    <span class="button-shadow">
      <span class="button-inside">
        <span class="button-text visually-hidden">Super Mario Bros. Wonder</span>
        <span class="button-text-characters-container" aria-hidden="true"></span>
      </span>
    </span>
  </span>
</button>

2. Now, style the button using the following CSS styles. You can modify CSS variables for customization. Adjust the variables like --bevel, --border-width, and colors according to your preference. The waving effect is applied on hover.

@font-face {
  src: url("https://assets.codepen.io/4175254/DIN2014-DemiBold.ttf") format("truetype");
  font-family: 'DIN 2014';
  font-weight: 600;
}
body {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  min-height: 100dvh;
  background-color: #1c1c1c !important;
}

.button {
  --bevel: 3px;
  --border-width: 3px;
  font-family: 'DIN 2014';
  font-weight: 600;
  color: #1d2119;
  filter: drop-shadow(1px 1px 1px rgba(0, 0, 0, 0.95));
  min-width: 10em;
}

.button-outline {
  --bevel-1: calc((var(--bevel) + (var(--border-width)) * 2) - ((var(--border-width) * 0.41421)) * 2);
  --bevel-2: calc(var(--bevel-1) + var(--border-width));
  --bevel-3: calc(var(--bevel-2) + var(--border-width));
  display: block;
  margin-top: calc(var(--border-width) * -1);
  margin-left: calc(var(--border-width) * -1);
  padding: var(--border-width);
  background-color: #fff;
  clip-path: polygon(var(--bevel-2) var(--border-width), calc(100% - var(--bevel-2)) var(--border-width), 100% var(--bevel-3), 100% calc(100% - var(--bevel-1)), calc(100% - var(--bevel-1)) 100%, var(--bevel-3) 100%, var(--border-width) calc(100% - var(--bevel-2)), var(--border-width) var(--bevel-2));
  transition-property: clip-path;
  transition-duration: .2s;
}
.button:hover:not(:active) .button-outline, .button:focus-visible:not(:active) .button-outline {
  clip-path: polygon(var(--bevel-1) 0, calc(100% - var(--bevel-3)) 0, 100% var(--bevel-3), 100% calc(100% - var(--bevel-1)), calc(100% - var(--bevel-1)) 100%, var(--bevel-3) 100%, 0 calc(100% - var(--bevel-3)), 0 var(--bevel-1));
}

.button-shadow {
  --padding: calc(var(--border-width) * 2);
  --bevel-1: calc((var(--bevel) + var(--border-width)) - (var(--border-width) * 0.41421));
  --bevel-2: calc(var(--bevel-1) + var(--border-width));
  --bevel-3: calc(var(--bevel-2) + var(--border-width));
  display: block;
  padding: calc(var(--border-width) * 2) var(--padding) var(--padding) calc(var(--border-width) * 2);
  background-color: #1d2119;
  clip-path: polygon(var(--bevel-2) var(--border-width), calc(100% - var(--bevel-2)) var(--border-width), 100% var(--bevel-3), 100% calc(100% - var(--bevel-1)), calc(100% - var(--bevel-1)) 100%, var(--bevel-3) 100%, var(--border-width) calc(100% - var(--bevel-2)), var(--border-width) var(--bevel-2));
  transition-property: clip-path;
  transition-duration: .2s;
}
.button:hover:not(:active) .button-shadow, .button:focus-visible:not(:active) .button-shadow {
  clip-path: polygon(var(--bevel-1) 0, calc(100% - var(--bevel-3)) 0, 100% var(--bevel-3), 100% calc(100% - var(--bevel-1)), calc(100% - var(--bevel-1)) 100%, var(--bevel-3) 100%, 0 calc(100% - var(--bevel-3)), 0 var(--bevel-1));
}

.button-inside {
  --padding-vertical: 6px;
  display: block;
  padding: var(--padding-vertical) 24px calc(var(--padding-vertical) - .125em);
  background-color: #fff;
  clip-path: polygon(var(--bevel) 0, calc(100% - var(--bevel)) 0, 100% var(--bevel), 100% calc(100% - var(--bevel)), calc(100% - var(--bevel)) 100%, var(--bevel) 100%, 0 calc(100% - var(--bevel)), 0 var(--bevel));
  text-align: center;
  transition-property: transform;
  transition-duration: .2s;
}
.button:hover:not(:active) .button-inside, .button:focus-visible:not(:active) .button-inside {
  transform: translate(calc(var(--border-width) * -1), calc(var(--border-width) * -1));
}
.button:hover .button-inside, .button:focus-visible .button-inside {
  background-color: #fcd200;
  background-image: linear-gradient(to right, rgba(0, 0, 0, 0), rgba(252, 210, 0, 0.9)), radial-gradient(#fff60d 1px, rgba(0, 0, 0, 0) 0%), radial-gradient(#fff60d 1px, rgba(0, 0, 0, 0) 0%);
  background-size: auto, 6px 6px, 6px 6px;
  background-position: 0 0, 0 0, 3px 3px;
  animation: scroll-background 1s linear infinite;
}

@keyframes scroll-background {
  to {
    background-position-x: 0, -6px, -3px;
  }
}
.button-text-characters-container {
  display: inline-block;
  transform: skewX(-6deg);
}

.button-text-character {
  display: inline-block;
}
.button:hover:not(:active) .button-text-character, .button:focus-visible:not(:active) .button-text-character {
  animation: jump 4s cubic-bezier(0.75, 0.25, 1, 2) var(--delay) infinite;
}

@keyframes jump {
  5% {
    transform: translateY(-0.125em);
  }
  10% {
    transform: translateY(0);
  }
}

3. The JavaScript portion dynamically separates each character of the text for individual animation. Make sure to include this script at the end of your HTML file or within a <script> tag.

const buttonText = document.querySelector('.button-text');
const buttonTextCharactersContainer = document.querySelector('.button-text-characters-container');

const buttonTextCharacters = buttonText.textContent.split('');

const characterCountWithoutWhitespaces = buttonTextCharacters.filter(character => character => !/\s/.test(character)).length;

const buttonTextCharactersFragment = document.createDocumentFragment();

let characterIndex = 1;

buttonTextCharacters.forEach(character => {
  const span = document.createElement('span');
  span.textContent = character;

  if (!/\s/.test(character)) {
    span.classList.add('button-text-character');
    const delay = `calc(2s / ${characterCountWithoutWhitespaces} * ${characterIndex} + 1s)`;
    span.style.setProperty('--delay', delay);

    characterIndex++;
  }
  
  buttonTextCharactersFragment.appendChild(span);
});

buttonTextCharactersContainer.appendChild(buttonTextCharactersFragment);

That’s all! hopefully, you have successfully created the CSS button with the waving text effect. If you have any questions or suggestions, feel free to comment below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

Please Rel0ad/PressF5 this page if you can't click the download/preview link

X