Reputation: 5149
.container {
width: 100px;
height: 100px;
font-size: 100px;
}
.background {
color: red;
width: 100%;
height: 100%;
margin-bottom: -100%;
transform: rotate(90deg);
}
.foreground {
color: blue;
width: 100%;
height: 100%;
}
<div class="container">
<div class="background">B</div>
<div class="foreground">F</div>
</div>
This example is also in here: https://jsfiddle.net/jasajona/vmzfgkcb/ How to keep transformed background div behind foreground? Found several examples but they seems not to work in this situation.
Upvotes: 3
Views: 1569
Reputation: 14541
You can use the isolation
property - which creates a new stacking context - on the foreground element.
.container {
width: 100px;
height: 100px;
font-size: 100px;
}
.background {
color: red;
width: 100%;
height: 100%;
margin-bottom: -100%;
transform: rotate(90deg);
}
.foreground {
color: blue;
width: 100%;
height: 100%;
isolation: isolate;
}
<div class="container">
<div class="background">B</div>
<div class="foreground">F</div>
</div>
Upvotes: 1
Reputation: 3824
You can give the foreground position:relative
and it will appear on top like this:
.container {
width: 100px;
height: 100px;
font-size: 100px;
}
.background {
color: red;
width: 100%;
height: 100%;
margin-bottom: -100%;
transform: rotate(90deg);
}
.foreground {
color: blue;
width: 100%;
height: 100%;
position:relative;
}
<div class="container">
<div class="background">B</div>
<div class="foreground">F</div>
</div>
Upvotes: 1
Reputation: 4209
The problem is that transform
ing an element creates a new "stacking context", which supersedes the source order that would otherwise determine which element is on "top".
There are several ways of dealing with this problem, but my favorite way of getting around this strangeness of CSS's z-index calculations is to add an opacity
property to your foreground element:
.container{
width: 100px;
height: 100px;
font-size: 100px;
}
.background {
color: red;
width: 100%;
height: 100%;
margin-bottom: -100%;
transform: rotate(90deg);
}
.foreground {
color: blue;
width: 100%;
height: 100%;
opacity: 0.999;
}
<div class="container">
<div class="background">B</div>
<div class="foreground">F</div>
</div>
Here are two resources about z-index
stacking context:
Upvotes: 2
Reputation: 17350
Just add a position: relative
to both layers, and then sort them using z-index
(if position
is not set, it's considered static
and static
does not account for z-indexes)
.container {
width: 100px;
height: 100px;
font-size: 100px;
}
.background {
color: red;
width: 100%;
height: 100%;
margin-bottom: -100%;
transform: rotate(90deg);
position: relative;
z-index: 1;
}
.foreground {
color: blue;
width: 100%;
height: 100%;
position: relative;
z-index: 2;
}
<div class="container">
<div class="background">B</div>
<div class="foreground">F</div>
</div>
Upvotes: 4