I have this example where I'm trying to make borders with gradients in CSS: https://codesandbox.io/s/sparkling-pine-uvdcj?file=/src/styles.css
As you can see, I am able to show the border in the first card, the one that is not stacked into a container (just a simple div with a background).
When I put the same component into another element (as the container), I'm not able to show the border at all.
What is the problem?
Here the CSS and HTML:
body {
background-color: gray;
}
.container {
background-color: black;
padding: 10px;
margin: 10px;
}
.card {
position: relative;
margin-top: 32px;
width: 300px;
height: 180px;
border-radius: 32px;
background: linear-gradient(45deg, #1b1a1f, #1b1a1f 65%, rgba(77, 58, 40, 1));
box-shadow: 0px 10px 10px rgba(0, 0, 0, 0.2);
}
.card:after {
content: "";
position: absolute;
display: block;
top: 0;
right: 0;
bottom: 0;
left: 0;
height: 182px;
width: 302px;
border-radius: inherit;
margin: -1px;
background: linear-gradient(40deg, #1b1a1f, #1b1a1f 60%, #ffb966 90%);
z-index: -1;
}
<div class="card"></div>
<div class="container">
<div class="card"></div>
</div>
That is happening because you have given the .card:after
z-index
of -1
. Whereas the default z-index
is auto
or you can think of it as 0
.
So, the container
has the z-index
of auto
which is higher than the z-index
of .card:after
. That is why it is behind the black
background.
You can do so by:
.container
..container {
position: relative;
z-index: -2;
}
Check it in action on Codepen.