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

What is the best way to adjust the dimensions of a textfield in Vuetify by altering its width and height?

Is there a way to adjust both the width and height of the <v-text-field>tag? I attempted to modify it using .css, but it seems to only affect certain parameters, leaving large white spaces on my page. This is the method I have tried so far: <te ...

SVGs are not resizing correctly in Internet Explorer and there is unnecessary blank space around them

Recently, I made the decision to switch to using SVG symbols for one of my projects. However, I encountered a challenge as I needed these SVGs to be responsive. My main objective was to minimize the number of HTTP requests, leading me to explore the option ...

Tips on assigning html actions/variables to a flask submit button

I am facing an issue with my Flask app where the form submission process takes a while for the backend to process. In order to prevent users from refreshing or resubmitting, I want to display a loading gif during this time. The current HTML code for my fl ...

Vue transition not functioning properly when hovering over text for changes

When you hover over the circular region on the website, the text and description in the red box will change. To add a fade animation effect when the text changes, I attempted to use <transition> as per the documentation provided in this link. Unfor ...

Iterate through an HTML table and transfer matching items along with their corresponding calculated amounts to a separate table

<html> <body> <div> <table border="1" id="topTable"> <thead> <th>Item</th> <th>Sold</th> </thead> <tbody id="topTableBody"> <tr> ...

Images in the JavaScript menu are not remaining fixed in place

Can someone help me with my website? I'm having trouble with the menu for different pages, it should stay gray for the active page. It was working fine before I switched to Wordpress, but now I'm not sure what the issue is. Could someone take a ...

Creating visually appealing websites is made easy with Bootstrap 5's innovative image and

I am working on a Bootstrap 5 website template and I want to add text with a white background in the center of an image. Instead of seeing just a white color over the image, I would like to have a white rectangle with a title and text centered on the imag ...

Trouble Viewing Fonts on Mobile Devices like iPhones

I am experiencing a frustrating issue on my website. While using Agency FB and Calibri fonts, they appear correctly on all desktop browsers. However, when accessed from my iPhone, a different standard font is displayed instead. Both the logo and text with ...

Converting HTML code to JSON using PHP scripting

Would appreciate your help in resolving a small issue. Although I came across this post, I am still encountering some errors: How can I convert HTML to JSON using PHP? I have created a PHP file that extracts a post from WordPress with the following forma ...

Is it possible to dynamically group by one column in a dataset without requiring a trigger function?

I've been working on a script that retrieves values from another page and organizes them into a table alphabetically by Name, City, and District. The current script is functioning well, but I now want to enhance it by grouping the values by city as we ...

Updates made to CSS are not reflecting on the error 404 page

Unable to Apply CSS Changes to 404 Error Page EDIT: page in question: Right from the start: I have ruled out any relative path issues. I have specified a base href in my header like this: <base href="https://hinnahackers.no/"> I am aware that thi ...

Browser comparison: Chrome and Firefox - peculiar ID name causing table width difference without CSS. Suspected AdBlocking interference

There seems to be an issue with a table I have in Chrome. Whenever I change the ID name, it either sets the width to zero or doesn't display at all. You can view it here: http://jsfiddle.net/kdubs/W6GTE/ The specific line causing trouble is: <ta ...

Is there a way I can implement a user-friendly playlist feature in my object-oriented HTML5 JS audio player that allows users

I have created a functional audio player using HTML5 and Javascript, along with some basic CSS. However, I am looking to enhance it by adding a clickable playlist feature. I want the name of the currently playing audio track to be displayed, and users shou ...

Tips for Including a Parallax Image Within a Parallax Section

Currently, I am working on implementing a parallax effect that involves having one image nested inside another, both of which will move at different speeds. My progress has been somewhat successful, however, the effect seems to only work on screens narrowe ...

Browser fails to display input value when altered

I have noticed that when the value of the Input element changes, the browser does not display the updated value. However, you can verify this change by inspecting the DevTools in the browser. I am curious as to why the actual value of the Input element is ...

Having a fixed footer in JQuery mobile does not adjust its position downwards as intended

I attempted to move the footer down using the provided code below, but it doesn't seem to be working. I even went ahead and removed all standard text from the CSS file to eliminate any potential issues. It should normally shift downward with <div ...

Incorporate JavaScript functionality with HTML dropdown lists

I am attempting to achieve the following: The user can choose between "Option One" or "Option Two". If they select "Option One", the result will be 66 + 45, and if they select "Option Two", the result will be 35 + 45. How can I make this work using a com ...

Steps for designing image animations with fade in and fade out effects

I have a task to enhance the current code provided below, which currently works with only two images. HTML: <div id="cf"> <img class="bottom" src="<?php bloginfo('template_directory') ?>/assets/img/image1" /> <img class ...

Apache causes HTML download tag to malfunction

I have an HTML file that includes the following code: <a href="/Library/WebServer/Documents/file.zip" download="file.zip"> Download here </a> When I test this HTML page on Chrome, it successfully allows me to download the file. However, when ...

CSS: Indication that the Text is Extending Beyond its Container

Seeking a solution to indicate overflow of text within its container, as demonstrated in this example. Essentially, a <span> with fixed dimensions. Looking for a visual cue when text exceeds boundaries. Attempted the use of text-overflow:ellipsis;, ...