问题
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.
回答1:
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>
回答2:
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>
回答3:
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;
}
回答4:
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/
来源:https://stackoverflow.com/questions/30503866/right-corner-ribbon-on-a-div-css