I would like to reduce the width
of the ribbon so it fits without changing the position of the ribbon.
Can someone guide me as to where I’m going wrong?
.ribbon-content {
position: relative;
}
.ribbon-content>[class*=ribbon] {
position: absolute;
z-index: 2000;
line-height: 2em;
display: block;
width: 10em;
height: 2.5em;
text-align: center;
text-decoration: none;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
.ribbon-content>[class*=ribbon]::before,
.ribbon-content>[class*=ribbon]::after {
content: '';
position: absolute;
inset: 0;
-webkit-clip-path: polygon(2em 0, 8em 0, 10em 2em, 10em 2.5em, 9.5em 2em, 0.5em 2em, 0 2.5em, 0 2em);
clip-path: polygon(2em 0, 8em 0, 10em 2em, 10em 2.5em, 9.5em 2em, 0.5em 2em, 0 2.5em, 0 2em);
}
.ribbon-content>[class*=ribbon]::before {
top: 0.1em;
height: 1.8em;
border: 1px dotted rgba(0, 0, 0, 0.3);
border-left: none;
border-right: none;
}
.ribbon-content>[class*=ribbon]::after {
z-index: -1;
background-color: #ff6347;
border-bottom: 0.5em solid #505050;
transition: background-color 0.2s ease-in-out;
}
.ribbon-content>.ribbon-top-left {
top: 0;
left: 0;
transform: translate(-3.85em, 1.4em) rotate(-45deg);
color: #fff;
font-size: 0.65rem;
}
.box {
width: 100%;
max-width: 20em;
min-height: 10em;
margin: 2em;
background-color: #333;
}
<div class="box ribbon-content"><span class="ribbon-top-left" href="#">NEW</span></div>
2
Answers
The width, the
tramsform
property and the parameter forclip-path
are all related to the width so I’d use CSS custom properties and calc to ensure they scale.I’ve done the
clip-path
for you but you’ll have to sort out the translate yourself. You might need to do a bit of geometry to figure this out. Code below:This is kind of a two part answer. The second builds on the first.
You want a block to change span to div and remove the
display:block
since that is the difference there.Move the ribbon to "wrap around" better
top: 0.75em; left: 0.5em;
Remove the box
position: relative;
and the ribbonposition: absolute;
as not needed.Then, set up the box as a border box with no margin or padding:
Font size not strictly needed here but since all modern browsers default to that but this makes it specific.
Set the margin on the ribbon
margin-left: 1.5em;
so it moves to "wrap" the box visually.Remove the
/* top: 0.1em; height: 1.8em;*/
as not needed.Now with all the above just adjust the ribbon font size as needed perhaps: