How to position these divs on top of each other - html

I have 3 divs that only contain a background color and what I want to make is something similar to the google chrome logo.
This is just part of the code, there is more content that comes on top of all this, but this is basically the background for the page.
Right now, green is on top of red which is fine. But yellow is below both red and green and it should be on TOP of green and UNDER red div. Sort of like entangled divs.
Is there a way to put the yellow div on top of green and under red div to make it look more like google chrome logo.
This is the best i can explain lol i know its complex to understand what im doing.
Here's the code (click for codepen)
.container {
position: relative;
width: 100%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
}
.red {
position: absolute;
left: 30%;
width: 100%;
height: 150vh;
transform: rotate(58deg);
background-color: #b71724;
opacity: 1;
transition: all 0.7s ease-in;
}
.green {
position: absolute;
right: 20%;
width: 110%;
height: 150vh;
transform: rotate(-58deg);
background-color: #2c4b2b;
opacity: 1;
transition: all 0.7s ease-in;
}
.yellow {
position: absolute;
top: 58%;
width: 100%;
height: 100vh;
background-color: #d1aa3b;
z-index: -1;
opacity: 1;
transition: all 0.7s ease-in;
}
<div class="container">
<div class="bg-div red"></div>
<div class="bg-div green"></div>
<div class="bg-div yellow"></div>
</div>

No. This is possible with something like paper, but z-index is more like a stack of plates. It would be like stacking plates so that the bottom one is also on top of the top one. Not going to happen.
The only way you could try to "cheat" a solution is to copy the bottom element (yellow), raise it all the way to the top, and somehow mask it so that it looks like it's both under red and above green. I'm not sure what you're trying to do in your actual application, but I would recommend just achieving the effect with an image.
But there is no way to make just these three divs work in the way you describe.

Assign differents z-index :
.red {
z-index : 3;
}
.yellow {
z-index : 2;
}
.green {
z-index : 1;
}
If you put the same z-index for two divs, then their place into the HTML code will determine their position (last in the body in front)

You can use the z-index on the red and green as you used on yellow changing the z-index of yellow to 0 and defining the z-index of green to -1 and the z-index of red to 1.

One solution to your problem is to use an extra div to close the position of the last div, I show you an example code:
.contain {
position: relative;
padding: 20px;
}
div {
width: 100px;
height: 400px;
}
.red {
position: absolute;
background: red;
left: 220px;
transform: rotate(-33deg);
z-index: 2;
}
.yellow {
position: absolute;
background: yellow;
transform: rotate(90deg);
top: 140px;
left: 160px;
z-index: 1;
}
.greenOut {
height: 200px;
z-index: 3;
background: green;
transform: rotate(34deg);
position: absolute;
left: 138px;
top: 34px;
}
.green {
position: absolute;
background: green;
transform: rotate(34deg);
left: 80px;
}
<div class="contain">
<div class="red"></div>
<div class="yellow"></div>
<div class="green"></div>
<div class="greenOut"></div>
</div>

Related

How to lay out with z-index positioned elements in a third level hierarchy

I have a problem with z-index property. I have some absoluted positioned elements (div in the code)one inside each other. The first div inside (red div) naturally would be in front of its parent (aliceblue element), thus I give a negative z-index to the red div inside and it's ok. But the aqua div (inside the red div) is in front of red element too (instead I would lay out the aqua element behind the red element), to lay out it behind I give it z-index:-5 for example but it doesn't work because I created a new stacking context different from stacking context of its father
div {
width: 300px;
height: 300px;
text-align: center;
position: absolute;
}
.a {
background-color: aliceblue;
position: relative;
}
.a1 {
background-color: red;
left: 20px;
top: 20px;
z-index: -1;
}
.a11 {
background-color: aqua;
left: 10px;
top: 10px;
/* z-index: -5; This doesn't work*/
}
.a2 {
background-color: yellow;
top: 40px;
z-index: -2;
}
<div class="a">
A
<div class="a1">
A1
<div class="a11">A11</div>
</div>
<div class="a2">A2</div>
</div>
So far we haven't found a way to do this using z-index.
The following method may or may not be suitable for your particular use case/overall context but it does solve the problem for the code given in the question.
This snippet uses 3d transforms rather than z-index to position the elements in relation to the viewer who is looking head on to the screen.
Of course we don't actually want to introduce perceptible perspective so the snippet uses a small shift back (0.1px). This seems enough to get the system to position elements on top of each other in the required order but small fractions were ignored (at least on my Windows10 Edge).
div {
width: 300px;
height: 300px;
text-align: center;
position: absolute;
transform-style: preserve-3d;
}
.a {
background-color: aliceblue;
position: relative;
transform: translate3d(0, 0, 0);
}
.a1 {
background-color: red;
left: 20px;
top: 20px;
transform: translate3d(0, 0, -0.1px);
}
.a11 {
background-color: aqua;
left: 10px;
top: 10px;
transform: translate3d(0, 0, -0.2px);
}
.a2 {
background-color: yellow;
top: 40px;
transform: translate3d(0, 0, -0.4px);
}
<div class="a">
A
<div class="a1">
A1
<div class="a11">A11</div>
</div>
<div class="a2">A2</div>
</div>

Issue with z-index and transformations

This has been asked before but none of the answers seem to be working for me.
My issue is related to a lost z-index when a transformation is applied.
I have an overlay div with a defined z-index, it has a sibling with no z-index and this div contains a child with a z-index greater than the overlay. This child can be dragged around.
At some point I rotate this sibling and it's child loses the z-index.
How can I prevent this from happening?
I tried several solutions attemps like transform-style: flat; or transform-style: preserve-3d; but with no luck
This is the code
HTML
<div class="main">
<div class="some_container">
<div class="drag"></div>
</div>
</div>
<div class="overlay"></div>
<br><br><br>
<button>rotate!</button>
CSS
body {
padding: 20px
}
div {
border: 1px solid black;
width: 50px;
height: 50px;
}
.main {
border: 1px dashed blue;
padding: 15px;
}
.some_container {
position: relative;
background-color: green;
}
.overlay {
background-color: red;
position: absolute;
left: 35px;
top: 35px;
z-index: 5
}
.drag {
position: relative;
width: 30px;
height: 30px;
background-color: lime;
z-index: 10;
cursor: move;
}
.rotated {
transform: rotateZ(15deg);
}
.rotated .drag {
background-color: yellow;
transform: rotateZ(-15deg);
position: relative;
z-index: 100;
transform-style: flat;
}
JS
$(".drag").draggable();
$("button").click(function()
{
$(".some_container").addClass("rotated");
});
fiddle: https://jsfiddle.net/2zkn9dap/
The transform that you have in your .rotated class creates a new stacking context that is changing the order that the elements are layered. A great explanation with more detail can be found here: z-index is canceled by setting transform(rotate)
The best approach to solving this is to move the .drag div to be a sibling of the .overlay and .some_container div. Then update your JS to add the rotated class to the green and yellow squares so they are both rotated. Otherwise, you'll never be able to get the yellow square on top of the red one consistently, because the z-index of the parent, in this case the .some_container div takes precedence.
$("button").click(function(){
$(".green").addClass("rotated")
$(".lime").addClass("rotated").css({backgroundColor: 'yellow'});
});
body {
padding: 20px
}
div {
border: 1px solid black;
width: 50px;
height: 50px;
}
.container {
border: 1px dashed blue;
padding: 15px;
}
.green {
position: absolute;
background-color: green;
z-index: 2;
}
.red {
background-color: red;
position: absolute;
left: 35px;
top: 35px;
z-index: 3;
}
.lime {
position: absolute;
width: 30px;
height: 30px;
background-color: lime;
z-index: 4;
cursor: move;
}
.rotated {
transform: rotateZ(15deg);
}
<div class="container">
<div class="green">
</div>
<div class="lime"></div>
</div>
<div class="red"></div>
<br><br><br>
<button>rotate!</button>
<script src="http://code.jquery.com/jquery-3.3.1.js"></script>
Change the position: relative to absolute of .lime.
If you don't want to rotate the '.lime' div, then remove `.addClass("rotated") on the 4th line of the script.

How to give a matte-finish (non-glassy) look to an image with CSS?

I would like to show the image with a non glassy display. Similar to the following one,
I am using the image as it is. I would like to show that with matte finish.
normal image:
Matte finish:
I am not able find it online. May be I am not using the right search keywords. Could anyone help me with this?
Inside a container I did put an image with reduced contrast / brightness / saturation plus a little bit of blur (all these four effects made by CSS filter). The image could've be placed as the container background but I wanted to apply these filters so it went separated.
After it, there's a colored layer with transparency covering the whole area. The letter represents the page's content that can be anything.
UPDATE: multiple filters must be all in a row, like it is on this latest update:
body {
width: 100%;
height: 100vh;
margin: 0px;
font-family: Georgia, serif;
}
#container {
width: 100%;
height: 100%;
position: relative;
background-color: navy;
overflow: hidden;
}
#thepic {
width: 100vw;
height: 100vh;
object-fit: cover;
-webkit-filter: brightness(90%) contrast(90%) blur(2px) grayscale(10%);
filter: brightness(90%) contrast(90%) blur(2px) grayscale(10%);
}
#color_layer {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: navy;
opacity: 0.3;
}
#content {
position: absolute;
top: 50%;
-webkit-transform: translateY(-50%);
-ms-transform: translateY(-50%);
transform: translateY(-50%);
left: 0;
right: 0;
margin: auto;
text-align: center;
}
h1 {
display: inline-block;
color: white;
text-shadow: 1px 2px 2px #000;
font-size: 4em;
font-weight: 100;
letter-spacing: 2px;
text-align: center;
vertical-align: middle;
}
#letter {
vertical-align: middle;
}
<div id=container>
<img id=thepic src="http://i.imgur.com/s9J4MnI.jpg">
<div id=color_layer></div>
<span id=content><img id=letter src="http://i.imgur.com/CB1vUqy.png" alt=img><h1> 書面</h1></span>
</div>
#freestock.tk - That's the idea I had in mind also.
Here's another way to do it with less markup:
img {
max-width: 100%;
height: auto;
}
.container {
position: relative;
z-index: -1;
}
.container:before {
content: "";
position: absolute;
z-index: 1;
top: 0;
bottom: 0;
left: 0;
right: 0;
background: rgba(0, 0, 0, 0.5);
}
<div>
<h1>Original Image</h1>
<img src="http://i.imgur.com/WjbwTUH.jpg">
</div>
<div class="container" id="content">
<h1> With Transparent Overlay </h1>
<img src="http://i.imgur.com/WjbwTUH.jpg">
</div>
In this example, I put the image inside a container that is relatively positioned. The z-index is -1 so it will be behind the next layer.
Then I used a pseudo element that is absolutely positioned so it will stretch across the whole width of the container and cover the image. The positive z-index sets it on top of the first layer. Rather than setting an opacity, I used an rgba value for the background color. The first three numbers or the red, green, and blue values as usual, but the last number is a decimal between 0 and 1 that sets the opacity level. I made it a little darker than you probably want just so you can see the difference. You may also choose a different color to fit your image.
For reference: http://nicolasgallagher.com/css-background-image-hacks/

CSS: Prevent summing color in opacited elements

I have 3 overlapping html divs, one next to another, colored: red, green and blue. All elements have opacity 0.5. First two divs (red and green) I want to summate color (creates something between red and green) - standard behaviour, without changes here.
My problem is how to prevent summating colors only between green and blue divs?
It would be great if we could do this without additional elements.
html:
<div id="d0"></div>
<div id="d1"></div>
<div id="d2"></div>
css:
div {
position: absolute;
opacity: 0.5;
}
#d0 {
top: 60px;
height: 100px;
left: 50px;
width: 100px;
background-color: red;
}
#d1 {
height: 150px;
left: 130px;
top: 50px;
width: 200px;
background-color: green;
}
#d2 {
height: 100px;
left: 300px;
top: 80px;
width: 120px;
background-color: blue;
}
EDIT:
I forgot about: http://plnkr.co/edit/5MIduRMFo0dZ54xqzpAa?p=preview
It should look likt this (fourth element is to show that blue also has opacity):
If you want to keep opacity of all divs to be still 0.5. Then here is your pure CSS solution. No additional elements added.
Here is a fiddle for that.
http://jsfiddle.net/tdh7ks2x/2/
**HTML**
<div id="d0"></div>
<div id="d1"></div>
<div id="d2"></div>
<div id="d4"></div>
**CSS**
#d2 {
opacity: 1;
height: 100px;
width: 120px;
left: 300px;
top: 80px;
}
#d2:before,
#d2:after{
content: "";
position :absolute;
left: 0;
top: 0;
height: 100px;
background-color: blue;
}
#d2:before{
width: 30px;
z-index: 2;
opacity: 0.99999999;
background-color: #7F7FFF;
}
#d2:after{
width: 120px;
z-index: 1;
opacity: 0.5;
}
#d4 {
width: 200px;
height: 80px;
left: 400px;
top: 90px;
background-color: red;
}
Just added this CSS instead of #d2, rest all your CSS is fine. Let me know if this resolves your issue.
Pick the color of the div with opacity and use it in the ":before" div.
You can use z-index property to bring a div to front or back. Higher the value of z-index to move it to the top and decrease it to move it back.
Moreover you have used opacity:0.5 due to which you will see the back colors at the intersection. You must increase the opacity to see the exact colors there
div {
position: absolute;
opacity: 0.5;
}
#d0 {
top: 60px;
height: 100px;
left: 50px;
width: 100px;
background-color: red;
z-index:2
}
#d1 {
height: 150px;
left: 130px;
top: 50px;
width: 200px;
background-color: green;
}
#d2 {
height: 100px;
left: 300px;
top: 80px;
width: 120px;
background-color: blue;
}
<div id="d0"></div>
<div id="d1"></div>
<div id="d2"></div>
css:
Basically, this problems occurs due to overlapping transparent colors. Example is rgba(255,255,255,0.3) overlapping with rgba(255,255,255,0.3) to form a brighter color.
If your design can do without transparent colors, you can easily solve this by converting your transparent colors (rgba) to fully opaque ones (hex) for related elements.
You will need the background color to help compute a fully opaque hex from rgba or just use a color picker browser extension after rendering.

Higher z-index does not change stacking order

What I am trying to do is, when the user hover on the image the image should reposition along the x-axis and it should reveal the .content. i have set z-index: 10 to image and z-index: 1 to .content to make .content to be underneath the image. but .content still remains on top of the image. Please help me..
Here is my code:
html
<div class="holder">
<img src="http://placehold.it/350x150" />
<div class="content">
<h3>hello there</h3>
view more
<div/>
</div>
css
.holder {
margin-top: 130px;
position: relative;
}
img {
display: block;
transition: -webkit-transform 0.5s;
transition: -moz-transform 0.5s;
z-index: 10;
}
.content {
background: blue;
height: 100%;
color: white;
z-index: 1;
position: absolute;
top: auto;
bottom: 0;
}
a {
color: white;
background: red;
padding: 10px;
}
.holder:hover img {
-webkit-transform: translateX(90px);
-moz-transform: translateX(90px);
}
Here I corrected issue of my code thanks to Jones G. Drange. As he pointed out in his comment
"z-index can only be modified in elements with a position other than static. Your img has position: static by default"
jsfiddle
img {
position: relative;
}