Right corner ribbon on a div CSS
Asked Answered
C

6

11

I am trying to make a corner ribbon in a div and its going everywhere I want it to look neat and nice it goes over the div and does not sit well.

/* The ribbons */

.corner-ribbon {
  width: 100px;
  background: #e43;
  position: absolute;
  top: 25px;
  left: -50px;
  text-align: center;
  line-height: 50px;
  letter-spacing: 1px;
  color: #f0f0f0;
  transform: rotate(-45deg);
  -webkit-transform: rotate(-45deg);
  overflow: hidden;
}

.corner-ribbon.shadow {
  box-shadow: 0 0 3px rgba(0, 0, 0, .3);
}


/* Different positions */

.corner-ribbon.top-right {
  /* top: 18px; */
  right: -4px;
  left: auto;
  transform: rotate(45deg);
  -webkit-transform: rotate(46deg);
  overflow: hidden;
}

.corner-ribbon.blue {
  background: #39d;
}
<div class="large-4 columns">
  <div class="corner-ribbon top-right sticky blue">Hello</div>
</div>

Can someone tell me how I can put a corner ribbon in the top right looking smart and nice which can handle around 3 words.

Chasse answered 28/5, 2015 at 10:26 Comment(1)
What is this supposed to look like?Slavophile
S
32

It's not clear what this is supposed to look like but if this is merely a corner band at 45 degrees across the top of a div/body then this option is one that (so far) requires no special adjustments.

I changes 'position' automatically on changes in font-size / padding etc.

Codepen Demo

.parent {
  overflow: hidden; /* required */
  width: 50%; /* for demo only */
  height: 250px /* some non-zero number */;
  margin: 25px auto; /* for demo only */
  border:1px solid grey; /* for demo only */
  position: relative; /* required  for demo*/
}

.ribbon {
  margin: 0;
  padding: 0;
  background: rebeccapurple;
  color:white;
  padding:1em 0;
  position: absolute;
  top:0;
  right:0;
  transform: translateX(30%) translateY(0%) rotate(45deg);
  transform-origin: top left;
}
.ribbon:before,
.ribbon:after {
  content: '';
  position: absolute;
  top:0;
  margin: 0 -1px; /* tweak */
  width: 100%;
  height: 100%;
  background: rebeccapurple;
}
.ribbon:before {
  right:100%;
}

.ribbon:after {
  left:100%;
}
<div class="parent">
<h4 class="ribbon">Special Sale Today</h4>
  </div>
Slavophile answered 28/5, 2015 at 11:21 Comment(0)
N
12

Edit: I keep seeing people approving of my rather quick and inflexible suggestion - but if you don't know the content (length/size) of the ribbon, definitely Check out Paulie_D's solution! His is more accomodating, as the ribbon "adjusts", depending on text length inside the ribbon. I would suggest a min-width for the :before/:after pseudo-elements though, since you only get a very short block if the content is just "new", for example


Are you really just looking for a better positioning?

Make the ribbon longer and move it so it is positioned neatly in the corner, make sure you give the CONTAINER element overflow: hidden;

https://jsfiddle.net/svArtist/jtwuxhcv/

.corner-ribbon {
  width: 250px;
  background: #e43;
  position: absolute;
  top: 25px;
  left: -50px;
  text-align: center;
  line-height: 50px;
  letter-spacing: 1px;
  color: #f0f0f0;
  transform: rotate(-45deg);
  -webkit-transform: rotate(-45deg);
  overflow: hidden;
}
.corner-ribbon.shadow {
  box-shadow: 0 0 3px rgba(0, 0, 0, .3);
}
/* Different positions */

.corner-ribbon.top-right {
  /* top: 18px; */
    top:30px;
  right: -70px;
  left: auto;
  transform: rotate(45deg);
  -webkit-transform: rotate(46deg);
  overflow: hidden;
}
.corner-ribbon.blue {
  background: #39d;
}

.large-4, html{
    height:100%;
    overflow:hidden;
    width:100%;
}

body{
    margin:0;
    padding:0;
    position:relative;
    width:100%;
}
<div class="large-4 columns">

  <div class="corner-ribbon top-right sticky blue shadow">Hello</div>
</div>
Nesmith answered 28/5, 2015 at 10:46 Comment(1)
Container has to have overflow: hidden AND position: relativeInteraction
D
3

I made a CSS-only collection where you can find most of the ribbon you want: https://css-generators.com/ribbon-shapes/

The #5 and #6 are the ones you want:

enter image description here

Here is a simplified version if you don't want the folded part:

.ribbon {
  font-size: 28px;
  font-weight: bold;
  color: #fff;
}
.ribbon {
  position: absolute;
  top: 0;
  right: 0;
  line-height: 1.8;
  padding-inline: 1lh;
  clip-path: polygon(
    100% 100%,0 100%,999px calc(100% - 999px),calc(100% - 999px) calc(100% - 999px));
  transform: translate(calc((1 - cos(45deg))*100%), -100%) rotate(45deg);
  transform-origin: 0% 100%;
  background-color: #BD1550; /* the main color  */
}

.box {
  position: relative;
  margin: 50px;
  height: 300px;
  background: #f5f5f5;
}
<div class="box">
  <div class="ribbon">Ribbon</div>
</div>
Debility answered 22/1 at 8:43 Comment(0)
S
0

body {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: #f0f0f0;
}
.box {
  position: relative;
  max-width: 600px;
  width: 90%;
  height: 400px;
  background: #fff;
  box-shadow: 0 0 15px rgba(0,0,0,.1);
}

/* common */
.ribbon {
  width: 150px;
  height: 150px;
  overflow: hidden;
  position: absolute;
}
.ribbon::before,
.ribbon::after {
  position: absolute;
  z-index: -1;
  content: '';
  display: block;
  border: 5px solid #2980b9;
}
.ribbon span {
  position: absolute;
  display: block;
  width: 225px;
  padding: 15px 0;
  background-color: #3498db;
  box-shadow: 0 5px 10px rgba(0,0,0,.1);
  color: #fff;
  font: 700 18px/1 'Lato', sans-serif;
  text-shadow: 0 1px 1px rgba(0,0,0,.2);
  text-transform: uppercase;
  text-align: center;
}

/* top left*/
.ribbon-top-left {
  top: -10px;
  left: -10px;
}
.ribbon-top-left::before,
.ribbon-top-left::after {
  border-top-color: transparent;
  border-left-color: transparent;
}
.ribbon-top-left::before {
  top: 0;
  right: 0;
}
.ribbon-top-left::after {
  bottom: 0;
  left: 0;
}
.ribbon-top-left span {
  right: -25px;
  top: 30px;
  transform: rotate(-45deg);
}

/* top right*/
.ribbon-top-right {
  top: -10px;
  right: -10px;
}
.ribbon-top-right::before,
.ribbon-top-right::after {
  border-top-color: transparent;
  border-right-color: transparent;
}
.ribbon-top-right::before {
  top: 0;
  left: 0;
}
.ribbon-top-right::after {
  bottom: 0;
  right: 0;
}
.ribbon-top-right span {
  left: -25px;
  top: 30px;
  transform: rotate(45deg);
}

/* bottom left*/
.ribbon-bottom-left {
  bottom: -10px;
  left: -10px;
}
.ribbon-bottom-left::before,
.ribbon-bottom-left::after {
  border-bottom-color: transparent;
  border-left-color: transparent;
}
.ribbon-bottom-left::before {
  bottom: 0;
  right: 0;
}
.ribbon-bottom-left::after {
  top: 0;
  left: 0;
}
.ribbon-bottom-left span {
  right: -25px;
  bottom: 30px;
  transform: rotate(225deg);
}

/* bottom right*/
.ribbon-bottom-right {
  bottom: -10px;
  right: -10px;
}
.ribbon-bottom-right::before,
.ribbon-bottom-right::after {
  border-bottom-color: transparent;
  border-right-color: transparent;
}
.ribbon-bottom-right::before {
  bottom: 0;
  left: 0;
}
.ribbon-bottom-right::after {
  top: 0;
  right: 0;
}
.ribbon-bottom-right span {
  left: -25px;
  bottom: 30px;
  transform: rotate(-225deg);
}
<div class="box">
  <div class="ribbon ribbon-top-left"><span>ribbon</span></div>
  <div class="ribbon ribbon-top-right"><span>ribbon</span></div>
  <div class="ribbon ribbon-bottom-left"><span>ribbon</span></div>
  <div class="ribbon ribbon-bottom-right"><span>ribbon</span></div>
</div>
Sleeper answered 17/9, 2023 at 10:5 Comment(0)
P
-1

Try this Demo

.corner - ribbon {
    width: 100 px;
    background: #e43;
    position: absolute;
    text - align: center;
    line - height: 50 px;
    letter - spacing: 1 px;
    color: #f0f0f0;
    transform: rotate(45 deg); - webkit - transform: rotate(45 deg);
    overflow: hidden;
    right: 0;
    top: 1.7e m;
}

.corner - ribbon.shadow {
    box - shadow: 0 0 3 px rgba(0, 0, 0, .3);
}

/* Different positions */

.corner - ribbon.blue {
        background: #39d;}
.large-4{
    position: relative;    
}
Peng answered 28/5, 2015 at 10:37 Comment(1)
Link is down. Updating it is needTymothy
B
-1

You can try this code, In HTML,

<div class="large-4 columns top-right">
  <div class="corner-ribbon sticky blue">Hello</div>
</div>

In CSS,

.columns {
 font-size: 16px !important;
 /* This ribbon is based on a 16px font side and a 24px vertical rhythm. I've used em's to position each element for scalability. If you want to use a different font size you may have to play with the position of the ribbon elements */

 width: 50%;

 position: relative;
 background: #ba89b6;
 color: #fff;
 text-align: center;
 padding: 1em 2em; /* Adjust to suit */
 margin: 2em auto 3em; /* Based on 24px vertical rhythm. 48px bottom margin - normally 24 but the ribbon 'graphics' take up 24px themselves so we double it. */
}
.columns:before, .columns:after {
 content: "";
 position: absolute;
 display: block;
 bottom: -1em;
 border: 1.5em solid #986794;
 z-index: -1;
}
.columns:before {
 left: -2em;
 border-right-width: 1.5em;
 border-left-color: transparent;
}
.columns:after {
 right: -2em;
 border-left-width: 1.5em;
 border-right-color: transparent;
}
.columns .corner-ribbon:before, .columns .corner-ribbon:after {
 content: "";
 position: absolute;
 display: block;
 border-style: solid;
 border-color: #804f7c transparent transparent transparent;
 bottom: -1em;
}
.columns .corner-ribbon:before {
 left: 0;
 border-width: 1em 0 0 1em;
}
.columns .corner-ribbon:after {
 right: 0;
 border-width: 1em 1em 0 0;
}
.columns.top-right {
  top: 75px;
  right: -75px;
  left: auto;
  transform: rotate(45deg);
  -webkit-transform: rotate(45deg);
  /* overflow: hidden; */
  z-index: 10;
}

For reference, just go through this nice article https://css-tricks.com/snippets/css/ribbon/

Betts answered 28/5, 2015 at 10:38 Comment(1)
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes.Ivey

© 2022 - 2024 — McMap. All rights reserved.