What is the best way to utilize the z-index property to ensure that the preceding element covers the one that follows

Is it possible to create an effect where the previous block overlaps the next one using CSS?

.container {
  padding: 70px;
  width: 500px;
  margin: auto;
  display: flex;
}

.block {
  margin-left: -30px;
  width: 200px;
  height: 100px;
  border: 1px dotted green;
  transform: skewY(20deg)
}

.red {
  background-color: red;
}

.green {
  background-color: green;
}

.blue {
  background-color: blue;
}

.black {
  background-color: black;
}
<div class="container">
  <div class="block red">Text...</div>
  <div class="block green">Text...</div>
  <div class="block blue">Text...</div>
  <div class="block black">Text...</div>
</div>

You could manually set z-index for each block with CSS, but imagine if there were a hundred blocks?

Codepen link:

https://codepen.io/pen/?template=zYoJVdp

Answer №1

Exploring the concept of a 3D rotation:

.container {
  padding: 70px;
  width: 500px;
  margin: auto;
  display: flex;
  transform-style: preserve-3d; /*  necessary for the technique */
}

.block {
  margin-left: -30px;
  width: 200px;
  height: 100px;
  border: 1px dotted green;
  transform: rotateY(-1deg) skewY(20deg); /* slight rotation applied here */
}

.red {
  background-color: red;
}

.green {
  background-color: green;
}

.blue {
  background-color: blue;
}

.black {
  background-color: black;
}
<div class="container">
  <div class="block red">Text...</div>
  <div class="block green">Text...</div>
  <div class="block blue">Text...</div>
  <div class="block black">Text...</div>
</div>

For further information, visit : Explaining why an element with a z-index value cannot cover its child elements

Answer №2

To achieve this effect, you may want to consider using JavaScript or jQuery.

One simple way to accomplish this with jQuery is by implementing a function that increases the z-index of each block by one:

$(".block").each(function(i) {
  i++
  $(this).css("z-index", i);
});
.container{
  padding: 70px;
  width: 500px;
  margin: auto;
  display: flex;
}
.block{
  margin-left: -30px;
  width: 200px;
  height: 100px;
  border: 1px dotted green;
  transform: skewY(20deg)
}
.red{
  background-color: red;
}
.green{
  background-color: green;
}
.blue{
  background-color: blue;
}
.black{
  background-color: black;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="container">
  <div class="block red">Text...</div>
  <div class="block green">Text...</div>
  <div class="block blue">Text...</div>
  <div class="block black">Text...</div>
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Step-by-step guide on displaying a fresh page within a JavaScript code

I've been working with XHTML, JSF, and JavaScript to develop a form that validates user input in selected fields when a button is clicked. The goal is to redirect the user to a different page called homepage.xhtml after successful validation. However, ...

In what rare scenarios does JS in string concatenation within insertAdjacentHTML fail to evaluate?

On a public website that I do not own, there is a piece of JavaScript code that writes a magic pixel image to the page to call an API I provided. Here's the snippet of code: n.insertAdjacentHTML('beforebegin', '<img src=& ...

Automatically adjust the image size in CSS to fit the screen when the iPhone is rotated from portrait to landscape

I am trying to create a responsive design where an image will fit the screen without being cropped in both portrait and landscape mode. I have attempted the following code: <!doctype html> <html> <head> <style> img { max-height ...

Tips for maintaining the active tab in Jquery even after the page is reloaded, especially for tabs that are constantly changing

$(document).ready(function(){ $('.scroll_nav li a').click(function(){ $('li a').removeClass("active_scroll"); $(this).addClass('active_scroll'); $('ul li .btn-br_all_gun').click(function(){ ...

What can I do to ensure that the cells within a CSS grid row have consistent heights across various columns?

Seeking for a solution to this issue seems futile - every response I find is about a slightly different issue, where all the rows in a single column end up being the same height as the tallest cell in that column. The suggestion is to add grid-auto-rows: 1 ...

The CSS functionality for the body tag operates seamlessly even without the presence of the body tag in the

My style.css file has the following code: body{ background-color:pink; font-size: 130%; } This is the code in my style_placement.html file: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

Incorporating a YouTube or Vimeo video while maintaining the proper aspect ratio

On my video page, I embed Vimeo videos dynamically with only the video ID. This causes issues with the aspect ratio as black bars appear on the sides due to the lack of width and height settings. The dynamic video ID is implemented like this: <iframe ...

jQuery UI DatePicker: without any styling, no picking dates

I'm struggling to configure a jQuery UI DatePicker. I moved the development-bundle folder contents to the directory res/jqueryui and initialized two datepickers as shown below: <script src="res/jquery-2.1.0.min.js"></script> <script sr ...

Error encountered when transitioning to TypeScript: Unable to resolve '@/styles/globals.css'

While experimenting with the boilerplate template, I encountered an unusual issue when attempting to use TypeScript with the default NextJS configuration. The problem arose when changing the file extension from .js to .tsx / .tsx. Various versions of NextJ ...

Problem with jQuery Window Resize Trigger Not Reactivating

I am facing a challenge in integrating a slider within multiple jquery tabs. As the slider requires specific width and height to display correctly, I have to trigger a window resize event when the tabs are switched. Surprisingly, the code I implemented onl ...

Check out the page design for section one!

I have been attempting to create a link to a specific section on a one-page website. Sometimes it works oddly, but most of the time it gets stuck at the section I clicked on and then manual scrolling does not function properly. From what I've researc ...

What is the correct way to implement checkbox validations using jQuery?

I have a total of 3 checkboxes that I would like to arrange in a particular order based on their checks. Currently, when one checkbox is selected for download, the logic I have implemented only considers the first checkbox and ignores the rest. I am look ...

Is there a way for me to choose all the classNames that conclude with a specific word within the MUI sx property?

I am currently working with MUI and I have a need to modify certain properties that are prefixed with random IDs. How can I target, for instance, the first one using: '& endsWith(MuiAccordionDetails-root)' I wish to achieve this because the ...

The video on mobile is not loading properly, as the play button appears crossed out

There seems to be an issue with GIFs not loading on mobile devices, even though they are implemented as mobile-ready. <video src="/wp-content/uploads/2017/09/5.mp4" preload="metadata" autoplay="autoplay" loop="loop" muted="muted" controls="controls" ...

How can I align the middle of the element within its parent until it hits the top-left edge, at which point should I stop center

I am trying to center an element within a parent container. It seems simple with methods like transform, flexbox, and grid... The issue arises with overflow behavior. When the parent container shrinks below the dimensions of the child element, scrollbars a ...

Is it possible to showcase HTML content within a C# string in a straightforward manner?

I am attempting to achieve a specific format, similar to the following: string html = @"Hello <b> World ! </b>"; The intended result should look like this: Hello World ! The string html can be utilized on various elements such as label, t ...

Display a pop-up when hovering over a layer with react-leaflet

I am attempting to display a popup when hovering over a layer in react leaflet. Utilizing GeoJson to render all layers on the map and onEachFeature() to trigger the popup on hover, I encountered an issue where the popup only appeared upon click, not hover. ...

What is the best way to utilize Django forms with a <select> value that is frequently used?

I am currently developing an application that enables users to interact with a database of information, including searching and saving entries. Within the search feature of the application, there are multiple fields where users should specify comparison o ...

Can a div be added within an SVG element?

I have been attempting to insert an HTML div into an SVG in order to create a graph. Despite using the code provided below to append the div, when I inspect the element using Firebug, I see that the div is contained within the rect element code, but it doe ...

Leveraging CSS in React/JSX

I am struggling to understand how to implement CSS with React. I have tried using inline styles but couldn't get it to work. Additionally, I am unsure where to apply CSS in my JSX code within the react class. For example, in one of my react classes, ...