Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS :after + z-index

I'm trying to create a message box with a down pointing arrow. I want to use the :after pseudo element for the arrow to avoid extra HTML, and I want the pseudo element to inherit the box-shadow of its parent.

My attempt:

main {
    padding: 30px;
}

div {
    width: 180px;
    height: 30px;
    line-height: 30px;
    background: #EC5F54;
    border-radius: 8px;
    text-align: center;
    color: white;
    font-family: Arial,Verdana,sans-serif;
    font-size: 13px;
    box-shadow: 0 1px 2px #999999;
    position: relative;
}

div:after {                        
    height: 20px;
    width: 20px;
    background: #EC5F54;
    content: '';
    -webkit-transform: rotate(45deg);
    box-shadow: inherit;
    position: absolute;
    left: 80px;
    top: 18px;
    z-index: -1px;
}
<main>
  <div>Some test message here</div>
</main>

I just can't get that pseudo element to go behind the parent. Can any of you figure out where I'm going wrong?

like image 325
dai.hop Avatar asked Dec 16 '25 23:12

dai.hop


1 Answers

You used -1px for z-index, z-index is not measured in pixels, just numbers, make it -1 and it works as intended

like image 98
Andy Avatar answered Dec 19 '25 14:12

Andy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!