Tips for positioning a div relative to another div while controlling its z-index

Hey there, take a look at the image below https://i.sstatic.net/zYiaY.png

The issue I'm facing is quite common - I have div 1 and div 2 visible in a loop, but divs 3 are hidden. When div 2 is clicked on, everything is great so far. However, what I want is for div 3 to adjust itself to the best available position, whether that be to the right, top, or bottom if there's no space on the left side. Essentially, I want it to auto-adjust like the title of something in default settings.

https://i.sstatic.net/ihVSr.png

Please disregard the colors. What I've attempted is something like this:

<div> // div1
    <div> //div2
        <div class='div3'> //div 3
        </div>
    </div>
</div>

CSS

.div3 {position:absolute;right:0px;}

I would appreciate any suggestions or insights on this matter.

Answer №1

It seems like you might be facing this issue, right?
The gray tiles are positioned behind the black ones. Take a look:

.div1 {
  position: relative;
  display: inline-block;
  width: 200px;
  height: 200px;
  background-color: #222;
}
.div2 {
  position: absolute;
  display: inline-block;
  width: 50px;
  height: 50px;
  background-color: #ee2;
  top: 60%;
  left: 60%;
}
.div3 {
  position: absolute;
  width: 50%;
  height: 50%;
  display: inline-block;
  background-color: gray;
  left: 100%;
  top: 80%;
}
<div class="div1">
  <div class="div2"></div>
  <div class="div3"></div>
</div>
<div class="div1">
  <div class="div2"></div>
  <div class="div3"></div>
</div>

<div class="div1">
  <div class="div2"></div>
  <div class="div3"></div>
</div>

The solution is quite simple: If you include a z-index greater than 1, it will be resolved.
I have set a z-index of 5.

.div1 {
  position: relative;
  display: inline-block;
  width: 200px;
  height: 200px;
  background-color: #222;
}
.div2 {
  position: absolute;
  display: inline-block;
  width: 50px;
  height: 50px;
  background-color: #ee2;
  top: 60%;
  left: 60%;
}
.div3 {
  position: absolute;
  width: 50%;
  height: 50%;
  display: inline-block;
  background-color: gray;
  left: 100%;
  top: 80%;
  z-index: 5;
}
<div class="div1">
  <div class="div2"></div>
  <div class="div3"></div>
</div>
<div class="div1">
  <div class="div2"></div>
  <div class="div3"></div>
</div>

<div class="div1">
  <div class="div2"></div>
  <div class="div3"></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 process for sending in individual rows?

I am facing an issue where only the first row is being submitted when I click on the submit button. How can I ensure that each looped row is executed individually? screenshot: https://ibb.co/x8y6X94 // my form <form action=""> &l ...

Concerns with the performance of Leaflet's polyline

Currently using Leaflet version 1.0.3 and encountering an issue with GPS positions on my map. Within a for loop, I am creating circle markers for each GPS position: var position = new L.latLng(lat, lng); coordinates.push(position); L.circle([lat, lng], ...

Learn the Method Used by Digg to Eliminate "&x=0&y=0" from their Search Results URL

Instead of using a regular submit button, I have implemented an image as the submit button for my search form: <input id="search" type="image" alt="Search" src="/images/searchButton.png" name="" /> However, I encountered an issue in Chrome and Fire ...

Learn how to style inline HTML elements, such as headings, labels, input fields, and buttons, which are typically displayed as block elements

Title. For my dictionary chrome extension, I am looking to inline a label, input field, and button all in one line. This is what I want to achieve: Define:[ INPUT ] DEFINE The brackets represent the input field and "DEFINE" is the ...

What is the best way to send an HTML form variable to a Perl script using AJAX?

My goal is to pass the HTML variable from the list menu to the Perl script using POST. However, when I try to do this, the jQuery ajax() function executes the Perl script without including the value obtained from document.getElementById. Below is the sni ...

Ensure the navbar includes the .container class and insert an extra dropdown right next to the container without causing any misalignment

Desired output: Achieve two rows with a .container div, centered in the viewport and aligned with each other. The second row should contain the navbar within a .container. Additionally, add a non-collapsing dropdown at the end of the navbar row, outside th ...

Having trouble with a JQuery selector not functioning properly when trying to select a class in the HTML that contains a

Looking for help with a JQuery selector to target the title of a YouTube video. Here's the HTML snippet: <div class="ytp-title-text"> <a class="ytp-title-link yt-uix-sessionlink" tabindex="13" target="_blank" ...

Tips for integrating CSS keyframes in MUI v5 (using emotion)

Hey there! I'm currently working on adding some spinning text, similar to a carousel, using keyframes in my React app. The setup involves MUI v5 with @emotion. Basically, I want a "box" element to show different words every few seconds with a rotating ...

Troublesome issue with the Focus() function in JavaScript

I'm having trouble setting the focus on a textbox with the id "txtCity." document.getElementById("txtCity").focus(); Any suggestions on how to make it work? ...

Guidelines for showcasing validation summary on an ASP.NET webpage with the help of Javascript

My asp.net form contains multiple required fields that need validation. I am looking to display a summary of all validations at the end of the form. Valid input controls have already been checked, now I just need the summary. Here is an example code s ...

What is the best way to determine which section of a promise chain is responsible for an error in Javascript?

(Please excuse any errors in my English) I am currently studying JavaScript promises. Below is a simple JavaScript code snippet for node.js (using node.js version v10.0.0) that asynchronously reads and parses a JSON file using promise chaining. const fs ...

When utilizing custom modules in Node.js, is it more effective to require the module from within a function or at the top of the script?

I have developed a custom script that includes all of my common settings and functions. One specific function within this script takes a timestamp as input and outputs a human-readable date with a customized format using Moment.js. The custom script is st ...

Can AJAX code be applied to elements with identical IDs and checked individually?

I have created multiple instances of the input field <input type="text" class="solution" name="answer" /> along with an image on my view. Users are required to guess what the image depicts. Upon entering their answer in the textbox and clicking or ta ...

Simple HTML and CSS exercise for beginners to grasp the concept

I'm looking to modify the layout of a specific webpage at based on the following instructions: First, insert this link within the <head> <link rel="stylesheet" href="https://trafficbalance.io/static/css/sdk/sdk.css"> -next, add th ...

What techniques can be employed to minimize the volume of text within my HTML code?

Currently, I am facing the challenge of composing a page containing over 1000 words, such as news articles, reports, and blog posts. The issue arises when I exceed 75 words between a pair of <p> tags. This not only makes the file messy and hard to r ...

I require varying numbers of columns on each row based on the breakpoint in Bootstrap4

After realizing it was easier than expected, I simply consolidated all columns into one row and the responsiveness now works perfectly. Thank you to everyone who helped. https://i.sstatic.net/4Sx41.png I am utilizing Bootstrap 4 grid system to structure 3 ...

JavaScript queries in Selenium WebDriver

Lately, I have been using the selenium webdriver (nodejs module) along with mocha for writing automation tests, and encountered a few challenges along the way. Issue with Scrolling driver.findElement() seems to return false as it is unable to find the e ...

Remove CSS Styling from DataFrames in Pandas

My data frame consists of records that have this structure: Untitledp { margin-top: 0px;margin-bottom: 0px;line-height: 1.15; } body { font-family: 'Times New Roman';font-style: Normal;font-weight: normal;font-size: 13.3333333333333px; } .Normal ...

Starting the Android System Magnifier with a Click: A Step-by-Step Guide

Is there a way to incorporate a magnifying glass into an HTML page using jQuery or within an Android app? Ideally, it would be for a single picture. In my application, I am displaying an HTML file from my assets in a webview. The HTML page utilizes jQuery ...

What could be causing the Bootstrap collapse feature to malfunction?

<nav id="na-vrh" class="navbar navbar-expand-lg navbar navbar-dark bg-dark"> <div class="container-fluid"> <a class="navbar-brand" href="#">Navbar</a> < ...