Layer divs on top of each other without explicitly defining their stacking order

I am looking to stack multiple tile divs on top of each other by using negative margin-right: -10px.

My goal is to have the previous div displayed on top, with new sibling divs appearing below it. Currently, the newer div overlaps the previous one. While this can be easily fixed by applying float: right to the tile element and reversing the order, it conflicts with another requirement I have - selecting the first same class after an instance of another class.

.another + .same {
 outline: 1px solid red;
}

Is there a way to achieve the stacking effect without resorting to absolute positioning or z-index? Since these tiles are dynamically added, I prefer to avoid using JavaScript to handle z-index adjustments.

.tile {
  width: 30px;
  height: 30px;
  display: inline-block;
  margin-right: -10px;
  border: 2px solid white;
  border-radius: 100%;
}

.a {
  background-color: yellow;
}

.b {
  background-color: pink;
}

.c {
  background-color: turquoise;
}

.d {
  background-color: grey;
}

.containerA {
  margin-bottom: 30px;
  width: 150px;
  text-align: center;
}

.containerB {
  width: 150px;
}

.containerB .tile {
  float: right;
}

.another + .same {
  outline: 1px solid red;
}
<div class="containerA">
<div>
Normal
</div>
  <div class="tile a another">
  </div>
  <div class="tile b another">
  </div>
  <div class="tile c same">
  </div>
  <div class="tile d same">
  </div>
</div>

<div class="containerB">
<div>
Expected outcome with float right but sibling selector broken
</div>
  <div class="tile d same">
  </div>
  <div class="tile c same">
  </div>
  <div class="tile b another">
  </div>
  <div class="tile a another">
  </div>
</div>

Fiddle - https://jsfiddle.net/be51xxku/2/

Answer №1

If you're looking to achieve a visual effect, one approach is to modify the CSS of your element.

For example, consider using radial-gradient instead of background-color and utilizing variables for easier management:

:root {
  --back-color: #fff;
}

.tile {
  width: 30px;
  height: 30px;
  display: inline-block;
  border-radius: 100%;
  margin-right: -15px;
  background: radial-gradient(circle at left, transparent 15px, var(--back-color) 16px);
}

.a {
  background: yellow
}

.b {
  --back-color: pink;
}

.c {
  --back-color: turquoise;
}

.d {
  --back-color: grey;
}

.containerA {
  margin-bottom: 30px;
  width: 150px;
  text-align: center;
}

.containerB {
  width: 150px;
}

.containerB .tile {
  float: right;
}

.another+.same {
  position: relative;
  outline: 1px solid red;
  z-index: -1;
}
<div class="containerA">
  <div>
    Normal
  </div>
  <div class="tile a another">
  </div>
  <div class="tile b another">
  </div>
  <div class="tile c same">
  </div>
  <div class="tile d same">
  </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

Developing a division with an image source based on the selection of a checkbox

My goal is to develop a "change function" that generates a new div for each checked checkbox selection and removes the div when the checkbox is unchecked. These new divs should also display the img src of the selected checkbox. Currently, my JavaScript c ...

Having trouble with the ::after element in my React component for my latest React 3D portfolio project

For my portfolio project, I am following a tutorial by LamaDev (https://www.youtube.com/watch?v=qALsVa-V9qo&t=2334s&ab_channel=LamaDev). At around timestamp 36:40, he creates a ::after selector which is not working for me, even though the rest of t ...

Align the center of table headers in JavaScript

I'm currently in the process of creating a table with the following code snippet: const table = document.createElement('table'); table.style.textAlign = 'center'; table.setAttribute('border', '2'); const thead ...

Using numerous background images can lead to issues in Safari browser

Currently experimenting with styling the body element using two images. background: url(bg2.png) repeat-x, url(bg1.png) repeat; An issue arises in Safari where it reports an error in the console, stating that it cannot locate the image file specified at ...

Introduction to Flask with GAE (blank Firefox tab)

Recently, I completed the Getting Started with Flask on App Engine Standard Environment tutorial. If you are interested, you can find the source code here. During my testing using Firefox and Chrome, I noticed that both browsers did not render the HTML te ...

Utilize an icon within an input box using content and the :before pseudo-element instead of relying on

My problem is that I have a great font set up and I use it to add custom icons next to my buttons. (for example: check here) Now, I want to create an input box and place an icon before it like in this guide HERE Instead of using a background image, I wou ...

What is the answer to this issue where the entity name is required to directly come after the "&" in the entity reference?

Whenever I insert the code into my Blogger platform, an error pops up stating: The entity name must directly follow the '&' in the entity reference Here is the code: <script> if (typeof bc_blocks == "undefined" && wind ...

Enhancing the functionality of localStorage

I am attempting to append a value to a key within the HTML5 localStorage. Take a look at my code below: var splice_string = []; splice_string += "Test value"; var original = JSON.parse(localStorage.getItem('product_1')); origina ...

Having trouble resolving 'primeng/components/utils/ObjectUtils'?

I recently upgraded my project from Angular 4 to Angular 6 and everything was running smoothly on localhost. However, during the AOT-build process, I encountered the following error: ERROR in ./aot/app/home/accountant/customercost-form.component.ngfactory. ...

Display the actual runtime hyperlink while utilizing the asp tag helper

Whenever I use asp-tag helpers to create a hyperlink, the result is displayed like this: <a asp-page="/demo">Demo</a> The actual html output looks like this: <a href="/test/Demo">Demo</a> However, instead of displaying "Demo" to ...

Refresh the HTML content as soon as a modification is detected in the MySQL Database

Apologies if this sounds like a basic question, but I've hit a roadblock in my search for answers on the vast expanse of the internet... I am in the process of constructing a website that requires certain data on the page to be updated automatically. ...

Having trouble adjusting the height of <a> elements in the navbar

My navbar has an issue where the listed items do not fill the entire height of the navbar. I have tried adjusting padding and margin, but it doesn't seem to be affecting the layout. Any suggestions would be greatly appreciated. Thank you. #navbar { ...

The function is not defined, even though it is located within the same file

I've encountered an issue with my web page where I'm trying to read data from an HTML data table and update it on a PHP file using AJAX queries in JavaScript. Everything was working fine until recently, even though I didn't make any signific ...

Jquery solution for toggling multiple Divs individually

I'm currently working on a small application that has both a sidebar menu and a header menu. My goal is to have all items in these menus toggle the visibility of content in one main window or page. When a button is clicked, I want it to show the corre ...

Unable to execute a JavaScript function when triggered from an HTML form

This is the code for a text conversion tool in HTML: <html> <head> <title> Text Conversion Tool </title> <script type="text/javascript"> function testResults(form) { var str = form.stringn.value; var strArray = str.split(" ...

Tips for adjusting the value of a textbox up and down

I am facing an issue with my booking flight form that is supposed to take input from users regarding the number of travelers. I have three textboxes for Adult, Children, and Infants respectively, along with a main textbox to display the final result. Howev ...

What is the process for sending a request and obtaining a response from a REST API using JavaScript?

I have a private server set up on my computer with a built-in REST API. The base URL for the API is: . I am now looking to develop a client application for this server using the API. To authenticate on the server, the API endpoint is baseurl/login with the ...

Ensuring maximum length validation on input

I am having an issue with the function "checkNumbers()". I am attempting to validate if my input does not exceed 10 numbers; while "isAllowedSymbol()" is functioning correctly, the other one is not. What could be causing this problem? function isAllowe ...

Setting a button next to an input field in Bootstrap 4: A step-by-step guide

I have a form group with a label and input field. My goal is to position the button next to the input field on the same line. Here's an example of my code: <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstra ...

submitting two contact forms using ajax

Looking to enhance my knowledge of form design... Hello there! I'm currently working with two contact forms on the same page, both utilizing ajax for submissions. Each form functions properly when standalone, but as soon as I integrate them togethe ...