Stack one image on top of another while maintaining their original proportions using HTML and CSS

I have two images, one is used as a background (A) and the other (B) is positioned over a portion of the background. The issue I am facing is that when the resolution changes, the image B moves and is no longer in the desired position. https://i.sstatic.net/PiLcl.png

To achieve this, I have the following HTML code:

<!DOCTYPE html>
<html>
<!-- Our Custom CSS -->
<link rel="stylesheet" href="main.css">
<body>

<div class="parent">
  <!-- Background image source -->
  <img class="imageBack" src="./back.png" />
  <!-- Overlaid image -->
   <img class="imageOver" src="./over.png" />
</div>

</body>
</html>

And the accompanying CSS:

.parent {
  position: relative;
 -webkit-box-align:center;
 -webkit-box-pack:center;
 display:-webkit-box;
}
.imageBack {
  position: relative;
  top: 0;
  left: 0;
}
.imageOver {
  position: absolute;
  top: 2%;
  left:17%;
}

Can someone help me understand what I'm doing wrong and why the image is shifting when the resolution changes?

Answer №1

When aligning the second image using a percentage, keep in mind that percentages are calculated based on the parent element's properties.

Let's say the window width is 1200px. The parent div, being a block element, will expand to fit this width, making the parent width also 1200px. If the image is set to have a left position of 25%, it will be 25% of 1200px, which equals 300px. Therefore, for a window width of 1200px, the image will be positioned 300px from its parent's left side.

If you resize the window to 900px, the parent div will also be 900px wide. The image will then have a left property of 25% of 900px, which is 225px. So resizing the window from 1200px to 900px will shift the image's position from 300px to 225px from the left.

To maintain the image position regardless of window size, use fixed units like pixels (px) to define the left and top properties.

For example:

.imageOver {
    position: absolute;
    top: 50px;
    left: 300px;
}

Adjust the values to suit your specific situation and ensure the image fits perfectly.

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

Customizing the underlineFocusStyle of a material-ui TextField component using regular CSS styling

When working with Material-UI components, you have the option to provide a style object for the component itself within the JSX tag. However, in certain cases like the underlineFocusStyle of a TextField component, you need to use a separate style object. ...

Place the image at the center of the div element

I'm having a hard time aligning this image in the center of my divs. No matter what I do, the image ends up either slightly off-center or stuck to the edges of the page. I've experimented with placing it within a row using Bootstrap's grid ...

Moving the mouse over a div will alter the heading of another div

Trying to find a solution for a unique problem. I have 5 boxes and need to style one box with a different heading color. As I hover over the other 4 boxes, I want the active box heading to change to a new color. The color should remain even after moving th ...

Should comments come before <!DOCTYPE HTML>?

When organizing my files, I always start with a comment about the content. However, I've been told that it's not recommended to put anything before the declaration. Is it acceptable to include comments before this or should I reserve the top of m ...

Ways to break up the loop for showing 2 items on each slide and 1 item every 3rd slide

I have a database with approximately 19 images that I want to display in slides. The goal is to have 9 slides with 2 images each and the 10th slide with just 1 image. Additionally, every fifth item should be displayed individually. For example: Slide 1 - ...

unable to load the ajax file

While setting up Ajax, I encountered an error message in IE that reads: Description: An issue occurred during the processing of a configuration file needed to handle this request. Please check the specific error details below and adjust your configuration ...

What is the best way to search for a CSS selector that includes an attribute beginning with the symbol '@'?

Whenever I want to target an element with a click event, I usually use the following jQuery selector: $('div[@click="login()"]') <div @click="login()"> Login </div> However, when I tried this approach, it resulted ...

Guide on validating an Australian phone number with the HTML pattern

When it comes to PHP, I have found it quite simple to validate Australian phone numbers from input using PHP Regex. Here is the regex pattern I am currently using: /^\({0,1}((0|\+61)(2|4|3|7|8)){0,1}\){0,1}(\ |-){0,1}[0-9]{2}(\ | ...

Having trouble getting my absolute div to center?

Check out this problem on my server at THIS LINK .login-div{ background: #fff none repeat scroll 0 0; height: 500px; left: 50%; width: 500px; right: 0; z-index: 99999; top: 20%; position: ...

Experiencing a lack of desired outcome in Outlook when using simple HTML with table elements

When creating a template for Outlook, I encountered an issue where the logo appeared on the right and the link text was on the left, which is the opposite of what I wanted. How can this be resolved? <center> <table width="600px;" style="margin:au ...

The `forEach` method cannot be called on an undefined node.js

I have been developing a small study website but encountered an issue with the title. Despite extensive internet research, I have not been able to find a solution. The system I am using is Ubuntu with node.js, express, and mysql. app.js var fs = requir ...

Tips for aligning a div within a flexbox to the bottom position

I'm having trouble aligning the div at the bottom of the flex box. I attempted to use align-content: flex-end; but it didn't work. Is there a way to keep the <.div class="parameters"> at the bottom of the row? Especially when the ...

Creating a dynamic menu layout using CSS

I've experimented with various methods to create a fluid menu, but none have been successful. Currently, I have an interactive menu that sometimes displays 6 items and other times 7 items. When I have 7 items, the menu aligns perfectly across the wid ...

Tips for eliminating unnecessary white space using CSS

https://jsfiddle.net/38h8acaq/ Is there a way to eliminate the white space on the left of the first tab and between the tabs and the content? Despite adding several margin:0px; lines to the CSS, none seem to be effective in removing the unwanted whitespac ...

Retrieve data from a JSON gist by parsing it as a query string

I have a JavaScript-based application with three key files: index.html app.js input.json The app.js file references input.json multiple times to populate content in div elements within index.html. My goal is to enhance the functionality so that when acc ...

Automate the login process for a website and programmatically fill out and submit a form

I am currently trying to automate the login process on Skype's website using Selenium in C#. Everything goes smoothly until I reach the password field, which seems to be elusive to Selenium. I have tried various methods to locate the password field bu ...

Manipulating Objects with CSS Transform in Global/Local Coordinates

If we take a closer look at our setup: <div id="outside"> <div id="inside">Foo</div> </div> and apply a rotation to the outer element - let's say turning it 45 degrees clockwise: <div id="outside" style="transform: ro ...

Is it better to have JavaScript and HTML in separate files or combined into a single HTML file?

Do you notice any difference when coding in both HTML and JavaScript within a single .html file compared to separating HTML code in a .html file and JavaScript code in a .js file? Does the functionality remain the same in both scenarios? For example, in t ...

I'm experimenting with using jQuery toggle to switch between text and images on my webpage

I attempted to use jquery "toggle" in order to hide the div containing text and display an image instead, and vice versa. However, when I click on the button, it does not work as expected. The button is from a bootstrap example - could that be the source o ...

Finding the correct tablet browser resolution for optimal web design

Is there a way to accurately determine the browser resolution for CSS on Samsung Galaxy Tab A 10.1 or other tablets? Despite the specifications stating that the resolution is 1920 x 1200, I am having trouble finding the correct resolution online. As I hav ...