Images in the center with a div overlay on top

I'm attempting to place a div or span on top of an image that is positioned above another image. Both images are centered and resized to fit. The height of my images is greater than the width, but they are not set at a fixed size. I want the div to perfectly match the dimensions and placement of the higherImg even when scaling occurs.

As a CSS novice, all I have been able to accomplish so far is stacking the images like this:

.imgbox {
  text-align: center;
  position: relative;
}

.lowerImg {
  max-width: 100%;
  max-height: 85vh;
  z-index: 3;
}

.higherImg {
  max-width: 100%;
  max-height: 60vh;
  padding-right: 5px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  z-index: 4;
}

.topDiv {
  background: $pink-color;
  height: 100%;
  width: 100%;
  opacity: 0.5;
  top: 0;
  left: 0;
  position: absolute;
  padding-right: 5px;
  transition: opacity .5s;
  z-index: 10;
  cursor: crosshair;
}
<div>
  <div class="imgbox">
    <img class="lowerImg" />
    <img class="higherImg" />
    <div class="topDiv"></div>
  </div>
</div>

Answer №1

It seems like you're looking for a way to stack a div and an image on top of each other:

Would something along these lines work for you?

.imgbox {
  text-align: center;
  position: relative;
  width: 100vw;
  height: 100vh;
  background-color: red;
  cursor: default;
}

.topDiv {
  height: 50%;
  width: 50%;
  opacity: 0.8;
  position: absolute;
  border: 2px solid red;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

.lowerImg {
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  position: absolute;
  border: 1px solid black;
  max-height: 100%;
  max-width: 100%;
  cursor: default;
}

.higherImg {
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  position: absolute;
  max-width: 90%;
  max-height: 90%;
  border: 1px solid red;
  cursor: default;
  transition: opacity .8s;
  cursor: crosshair;
}
<div>
  <div class="imgbox">
    <div class="topDiv">
      <img class="lowerImg" src="http://placekitten.com/301/301" />
      <img class="higherImg" src="http://placekitten.com/201/201" />
    </div>
  </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

Is there a way to determine the color of an element when it is in a hover state?

I recently started using the Chosen plugin and noticed that the color for the :hover on the <li> elements is a bright blue. I want to change it to a bold red color instead. https://i.stack.imgur.com/mcdHY.png After inspecting it with the Chrome too ...

Changing the Color of Links in Bootstrap CSS

I'm completely new to coding and I've decided to dive into the world of Bootstrap. However, I'm facing a hurdle: I can't figure out how to customize the default Bootstrap styles with my own CSS. After researching online, here are the c ...

Desktop users can enjoy the off-canvas menu feature, while mobile users will have access to the accordion menu option with Foundation 6

My goal is to customize my off-canvas menu in Foundation 6.3.1 so that it appears on desktop while displaying an accordion menu on mobile devices. On desktop, I want the off-canvas menu always visible (see example). For mobile, when the hamburger icon is ...

Different CSS values can be applied for specific versions of Internet Explorer by using conditional comments

I am dealing with a CSS class named "myclass" that requires a z-index of 5 specifically for IE8. I have attempted to achieve this using the following methods: .myclass{ z-index: 5\9; } ---> Interestingly, this method applies from IE10 onwards a ...

What is the best way to display rows in alternating red and green colors?

Currently, I am implementing the react table in my React project. I found valuable resources on how to use it at these links: https://github.com/tannerlinsley/react-table/tree/v6#codesandbox-examples and also here: https://www.npmjs.com/package/react-tab ...

Discover the chosen image displayed within an ng-repeat showcased in a separate container

I am facing an issue with a table that uses ng-repeat to display data. One of the columns contains photos. When I edit a specific entry in the table, I want the corresponding photo to be shown. How can I achieve this? The code for my table looks like this ...

Why does Node.js exclusively acknowledge absolute paths?

After creating a file named nodes and initializing it with npm init, the main js file was named main.js. Along with that, index.html and index.css were also created. To render index.html using Node.js, the following code was added to main.js: const http = ...

Using JavaScript, aim for a specific element by its anchor headline

I'm looking to make some changes to my navigation menu, specifically hiding the home menu item unless the mobile navigation menu is toggled. Is there a way for me to target the "home" anchor title and apply the active class to it when toggled, similar ...

Encountering a problem with loading the stylesheet

I keep encountering a 404 error when trying to load the CSS in my web application. The HTML content looks like this: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>PSL Inter App</title> <meta nam ...

Unable to remove div element

Currently, I am working on writing a code for a webpage with specific functionalities. I need to display a button and an embedded YouTube video. When the user clicks on the video, they should be redirected to a different link (not YouTube) and the video sh ...

Disabling the final grid cell(s)

I am currently working on a 4 column grid in ReactJS. The grid includes various elements with images and text. I want to ensure that if there are five elements filled, the last three should be inactive so that each row always has four columns without any e ...

Creating uniform height for Definition Terms (DT) and Definition Descriptions (

Looking to creatively style a description list <dl> in a way that showcases two columns, even when there are multiple <dd>s following the <dt>. The challenge lies in wanting this design to be dynamic without resorting to floating the < ...

Troubleshooting: Issue with JQUERY and Select box not syncing with database

I have a set of 3 checkboxes that are dynamically populated from a PHP database. I need help with implementing JQUERY functionality so that when any of the checkboxes are changed, the selected value is sent to a PHP file for processing and a response is re ...

Hover effect for child element not activating CSS3 Transition

I need my list item to activate a css3 transition on its child element .pusher when it is hovered over. I usually achieve this using JS, but I want to try implementing it with css3 transitions. After reading some other questions on SO, I attempted to do it ...

Why is the image cut in half on mobile devices but displaying correctly on computer screens? Could it be an issue with

There seems to be an issue on mobile screens that does not occur on computer screens. When the user clicks on the image, it disappears, and when they click another button, it reappears. However, there is a problem with how the image appears - it is cut off ...

Issues with Youtube Subscription Functionality

I implemented the code below to embed a YouTube subscribe button on my website: <script src="https://apis.google.com/js/platform.js"></script> <div class="g-ytsubscribe" data-channel="GoogleDevelopers" data-layout="default" data-count="def ...

Prevent automatic jumping to input fields

Can someone help me with a problem related to the html input tag or primefaces p:input? I am facing an issue where the cursor always automatically jumps into the input field. The height of my page is such that scrolling is required, and the input field i ...

The issue with Bootstrap 4 Carousel caption not displaying below 768px is still unresolved

I am currently attempting to customize the appearance of my carousel caption, but I have noticed that when I minimize the screen, the caption disappears. I came across a solution in another forum post suggesting to remove the "d-block d-m-block" classes, b ...

The hover effect does not seem to be functioning properly within the <th>

I've been working on a tooltip feature that displays data in a message box when hovering over an icon based on its attribute. The following code is implemented for the mouseenter event. <span class='csTip fa fa-info-circle' csTipTerm=&a ...

How to access specific values from a deserialized JSON string using Javascript

Within the nested for loops, I have the following row: document.getElementById("sm" + i + "b" + j).innerHTML = jsonval.values.sm1b1; ^^^^^^ In order to change the va ...