Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Right corner ribbon on a div CSS

Tags:

css

css-shapes

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.

like image 229
user4058171 Avatar asked May 28 '15 10:05

user4058171


2 Answers

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>
like image 55
Paulie_D Avatar answered Nov 23 '22 15:11

Paulie_D


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>
like image 44
Ben Philipp Avatar answered Nov 23 '22 14:11

Ben Philipp