Yogesh Chauhan's Blog

How to add a ribbon inside a container using CSS?

in CSS on February 10, 2021

Our end goal is to add ribbon like this:

add ribbon using css

Step 1: Let’s create our container first.

All we need is a box with fixed size. You can have responsive box but the ribbon needs to be responsive as well. For the sake of example, I have it in fixed size.

We will set the position of the box relative.

Step 2: Add a ribbon container.

Let’s add the container that will hold the ribbon.

It’s position must be absolute so that it can live on top of the container and be easily visible and not hiding behind it.

We’ll keep the overflow hidden.

I have added background-color property temporarily just so that the container can be visible. It will be removed in the next step when we have the ribbon inside the container.

Step 3: Add the ribbon in the ribbon container.

First thing first. Let’s remove the temporary background-color of the ribbon container e added in the previous step.

After that let’s add the actual ribbon (just a span tag) inside the ribbon container. The position of the ribbon should be set absolute as well so that it will live inside the container as well as on top of the container. That will make it clearly visible!

You can display it as block or inline-block. Both will work!

And at the end, add some nice style… Add some nice color, some texts, align it and blah blah blah… Basically, set the ribbon style the way you want!

Step 4: Change the position of the ribbon.

Let’s adjust the position of the ribbon so that we can place it the way we want.

Let’s bring it a bit down and push it inside from left.

Step 5: Final placement of the ribbon.

Let’s finally push the ribbon in the perfect position. We can use transform property to do so. We will rotate it by -45deg to place on the top left corner.

Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Aug 15 Is PHP still good for back-end programming? #Aug 10 How to create a multisite network in WordPress? #Aug 3 How to create a circle that follows a cursor using JavaScript and CSS? #Aug 3 How to make a curtain slider using jQuery and CSS? #Aug 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS?
You might also like these
All different methods for accessing elements in the DOM using JavaScriptJavaScriptHow to use @each rule with key-value pair in SCSS?SCSSJavaScript String fromCharCode() MethodJavaScriptFilling a button background from left to right using CSSCSSReverse a String in JavaScriptJavaScriptHow to create a blurry text effect using CSS?CSS