This answer consists of two parts, with the second part expanding on the first.
To transform a span into a div and eliminate the display:block
property, a block needs to be adjusted.
Optimize the positioning of the ribbon by adjusting the following properties:
top: 0.75em; left: 0.5em;
Unnecessary properties like position: relative;
for the box and position: absolute;
for the ribbon should be removed.
Ensure the box is structured as a border box with zero margin or padding:
margin: 0;
padding: 0;
box-sizing: border-box;
font-size: 16px;
}
Although specifying font size is not imperative due to modern browser defaults, it adds specificity.
Adjust the ribbon margin with margin-left: 1.5em;
for better visual alignment with the box.
Remove the /* top: 0.1em; height: 1.8em;*/
as it serves no purpose.
.ribbon-content {
margin: 0;
padding: 0;
box-sizing: border-box;
font-size: 16px;
}
.ribbon-content>[class*=ribbon] {
margin-left: 1.5em;
z-index: 2000;
line-height: 2em;
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.75em;
left: 0.5em;
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; can be anything since this is BOTH elements contained */
background-color: #333;
}
<div class="box ribbon-content">
<div class="ribbon-top-left" href="#">NEW</div>
</div>
After implementing the above changes, adjust the ribbon font size accordingly:
If needed, you can also wrap the text to resize it while modifying the ribbon height and left margin as follows:
.ribbon-content>.ribbon-top-left {
font-size: 0.5rem;
top: 0.75em;
left: 0.5em;
transform: translate(-3.85em, 1.4em) rotate(-45deg);
color: #fff;
}
.ribbon-content {
margin: 0;
padding: 0;
box-sizing: border-box;
font-size: 16px;
}
.ribbon-content>[class*=ribbon] {
margin-left: 1.75em;
z-index: 2000;
width: 10em;
height: 3em;
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 2em, 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 {
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 {
font-size: 0.5rem;
top: 0.75em;
left: 0.5em;
transform: translate(-3.85em, 1.4em) rotate(-45deg);
color: #fff;
}
.ribbon-content>.ribbon-top-left .just-text {
font-size: 1.5em;
height: 2.5em;
}
.box {
width: 100%;
max-width: 20em;
min-height: 10em;
/* margin: 2em; can be anything since this is BOTH elements contained */
background-color: #333;
}
<div class="box ribbon-content">
<div class="ribbon-top-left" href="#"><span class="just-text">NEW</span></div>
</div>