html - Z-index not working on :before & :after -
i'm trying make ribbon 1 in image linked, when use z-index send edges behind main part of ribbon disappears behind pages background color. can please tell me whats wrong code?ribbon image
the html:
<!doctype html> <html> <head> <title>newsletter signup</title> <link href="style.css" rel="stylesheet"> </head> <body> <div class="container"> <div class = "header"> <h1 class = "logo">skillcrush</h1> <div class="ribbon"> <p>the delightful way learn</p> </div> </div><!--header--> </div><!--container--> </body> </html>
the css:
.container-body{ margin: 0 auto; width:1200px; height:702px; background-color:#fff0da; z-index: auto; } .ribbon{ text-align: center; position: absolute; left:0; right:0; top:120px; margin-left: auto; margin-right: auto; font-size:16px; background-color:#f6515d; height:28px; width:260px; color:rgb(255, 240, 218); border: solid #fff0da 2px; z-index: 2; } .ribbon:before { content: ' '; position:absolute; width: 30px; height: 0; left: -30px; top: -10px; border-width: 20px 10px; border-style: solid; border-color: #f6515d #f6515d #f6515d transparent; z-index: 1; } .ribbon:after { content: ' '; position:absolute; width: 30px; height: 0; right:-30px; top: -10px; border-width: 20px 10px; border-style: solid; border-color: #f6515d transparent #f6515d #f6515d; z-index: 1; }
you need establish new stacking context on .container-body
.
.container-body { z-index: 1; position: relative; /* ... */ }
then use negative z-indexing on pseudo-elements:
.ribbon { /* no z-index, ... */ } .ribbon:before, .ribbon:after { z-index: -1; /* ... */ }
here full example:
.container-body{ margin: 0 auto; width:1200px; height:702px; background-color:#fff0da; z-index: 1; position: relative; } .ribbon{ text-align: center; position: absolute; left:0; right:0; top:120px; margin-left: auto; margin-right: auto; font-size:16px; background-color:#f6515d; height:28px; width:260px; color:rgb(255, 240, 218); border: solid #fff0da 2px; } .ribbon:before { content: ' '; position:absolute; width: 30px; height: 0; left: -30px; top: -10px; border-width: 20px 10px; border-style: solid; border-color: #f6515d #f6515d #f6515d transparent; z-index: -1; } .ribbon:after { content: ' '; position:absolute; width: 30px; height: 0; right:-30px; top: -10px; border-width: 20px 10px; border-style: solid; border-color: #f6515d transparent #f6515d #f6515d; z-index: -1; }
<div class="container-body"> <div class="ribbon">test</div> </div>
Comments
Post a Comment