Curved Ribbon with Hover Effect Using CSS

Curved Ribbon with Hover Effect Using CSS
Code Snippet: Curved ribbon with hover effect
Author: Temani Afif
Published: January 30, 2024
Last Updated: February 3, 2024
Downloads: 162
License: MIT
Edit Code online: CodeHim
Read More

This code creates a stylish curved ribbon with a hover effect using CSS. The ribbon’s color, cutout part, and animation angle are customizable. The ribbon responds to cursor hover, smoothly transitioning to a specified angle. Useful for adding an eye-catching decorative element to your webpage.

How to Create Curved Ribbon With Hover Effect Using CSS

1. First of all, include the following HTML snippet in your webpage, placing it where you want the ribbon to appear.

<h2 class="ribbon">A Cool Ribbon</h2>

2. In the CSS code, variables like --c (color), --r (cutout part), and --a (animation angle) can be adjusted to personalize the ribbon. Experiment with these variables to achieve your desired look.

The .ribbon class represents the main ribbon container, and the :before and :after pseudo-elements create the ribbon edges. The --a variable controls the animated angle, triggered on hover.

@property --a {
  syntax: "<angle>";
  initial-value: 0deg;
  inherits: true;
}

.ribbon {
  --c: #60b0a7;
  /* control the cutout part (adjust each variable to understand what it does) */
  --r: .8em;  
  --a:  0deg; /* this will get animated */
  --d: .5em;

  padding-inline: .3em;
  margin: calc(.5lh + var(--r)) calc(1.2lh + var(--d));
  line-height: 1.75;
  background: var(--c);
  position: relative;
  white-space: nowrap;
  transition: --a .6s;
  cursor: pointer;
}
.ribbon:hover {
  --a: 60deg; 
}
.ribbon:before,
.ribbon:after {
  content: "";
  position: absolute;
  width: calc(1lh + var(--d));
  height: calc(1.5lh + var(--r));
  border: solid var(--c);
  box-sizing: border-box;
  rotate: calc(var(--a) - 90deg);
}
.ribbon:before {
  left: 99%;
  top: 0;
  border-width: 1lh 1lh 0 0;
  border-radius: 0 999px 0 0;
  clip-path: polygon(calc(999px*cos(var(--a))) calc(1lh + var(--d) - 999px*sin(var(--a))),999px 100%,100% 100%,calc(50% + var(--d)/2) calc(100% - var(--r)),var(--d) 100%,0 100%, 0 calc(1lh + var(--d)));
  transform-origin: 0 calc(1lh + var(--d));
}
.ribbon:after {
  right: 99%;
  bottom: 0;
  border-width: 0 0 1lh 1lh;
  border-radius: 0 0 0 999px;
  clip-path: polygon(calc(100% - 999px*cos(var(--a))) calc(100% - 1lh - var(--d) + 999px*sin(var(--a))),-999px 0,0 0,calc(50% - var(--d)/2) var(--r),calc(100% - var(--d)) 0,100% 0, 100% calc(100% - 1lh - var(--d)));
  transform-origin: 100% calc(100% - 1lh - var(--d));
}


body {
  margin: 0;
  min-height: 100vh;
  display: grid;
  place-content: center;
  font-size: 31px;
  font-family: sans-serif;
  font-weight: bold;
  color: #fff;
  background: #F9CDAD;
}

As the cursor hovers over the ribbon, the --a variable smoothly transitions, creating an engaging hover effect. Customize the hover angle by adjusting the value in .ribbon:hover { --a: 60deg; }.

Modify the --d variable to control the spacing between the ribbon and the surrounding elements. Adjust the --r variable to alter the cutout part’s size.

That’s all! hopefully, you have successfully created a Curved Ribbon with the hover effect on your website. 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