Resizing the window causes divs to be positioned relative to the image (almost functional)

In my coding project, I've encountered a situation where I have 3 "pins" positioned within a div called #outerdiv. This div has a background-image and the pins are supposed to remain in the same spot on the background image no matter how the window is resized. Interestingly, this works perfectly fine with #pin1, but not with #pin2 and #pin3, despite them having similar code. The key distinction seems to be that #pin1 comes first in the HTML structure.

UPDATE: You can view the JSFIDDLE for better clarity.

Upon resizing the window, it's evident that while #pin1 maintains its position flawlessly on the background image, #pin2, along with subsequent pins, gets slightly displaced downwards, causing them to lose alignment with the background image. However, they still seem to be correctly aligned horizontally.

$(document).ready(function(){
    updateProductBg();
});
$(window).resize(function() {
    updateProductBg();
});
function updateProductBg(){
    if($(window).width()/$(window).height() >=  2600/1450) {
        $('#outerdiv').css({'height':'100%','width':$(window).height()*(2600/1450)+'px', 'margin-left':-$('#outerdiv').width()/2+'px', 'left':'50%', 'top':'0', 'margin-top':0});
    } else {
        $('#outerdiv').css({'height':$(window).width()*(1450/2600)+'px', 'width':'100%', 'margin-left':0, 'left':0, 'top':'50%', 'margin-top': -$('#outerdiv').height()/2+'px'});
    }
}
#outerdiv {
    background-image: url(https://upload.wikimedia.org/wikipedia/commons/6/6a/Luftaufnahmen_Nordseekueste_2013_05_by-RaBoe_314.jpg);
    background-size: contain;
    background-repeat: no-repeat;
    background-position: 0 0;
    position: absolute;
    bottom: 0;
    width: 100%;
    height: 100%;
}
.pin {
    background-image: url(https://upload.wikimedia.org/wikipedia/commons/1/16/WP_SOPA_sm_icon_identica_ffffff.png);
    width: 32px;
    height: 33px;
    position: relative;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<div id="outerdiv">
    <div id="pin1" class="pin" style="top:21%; left:38%;"></div>
    <div id="pin2" class="pin" style="top:13%; left:24.5%;"></div>
    <div id="pin3" class="pin" style="top:36%; left:26%;"></div>
</div>

updateProductBg() function is utilized to ensure that #outerdiv matches the dimensions of the background-image. To test it out yourself, simply substitute the 2600 and 1450 values with your actual image width and height.

Answer №1

.pin currently has a style of position: relative;, which means that the pins will be offset from where they are originally painted on the page when using properties like top and left. To position them relative to the container #outerdiv, you should change their positioning to position: absolute;.

In the initial version, #pin1 appears correctly because it is initially placed at the top left corner of #outerdiv before any adjustments are made. The other two pins are positioned incorrectly because their placement is affected by the previous pins.

$(document).ready(function(){
    updateProductBg();
});
$(window).resize(function() {
    updateProductBg();
});
function updateProductBg(){
    if($(window).width()/$(window).height() >=  2600/1450) {
        $('#outerdiv').css({'height':'100%','width':$(window).height()*(2600/1450)+'px', 'margin-left':-$('#outerdiv').width()/2+'px', 'left':'50%', 'top':'0', 'margin-top':0});
    } else {
        $('#outerdiv').css({'height':$(window).width()*(1450/2600)+'px', 'width':'100%', 'margin-left':0, 'left':0, 'top':'50%', 'margin-top': -$('#outerdiv').height()/2+'px'});
    }
}
#outerdiv {
    background-image: url(https://upload.wikimedia.org/wikipedia/commons/6/6a/Luftaufnahmen_Nordseekueste_2013_05_by-RaBoe_314.jpg);
    background-size: contain;
    background-repeat: no-repeat;
    background-position: 0 0;
    position: absolute;
    bottom: 0;
    width: 100%;
    height: 100%;
}
.pin {
    background-image: url(https://upload.wikimedia.org/wikipedia/commons/1/16/WP_SOPA_sm_icon_identica_ffffff.png);
    width: 32px;
    height: 33px;
    position: absolute;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<div id="outerdiv">
    <div id="pin1" class="pin" style="top:21%; left:38%;"></div>
    <div id="pin2" class="pin" style="top:13%; left:24.5%;"></div>
    <div id="pin3" class="pin" style="top:36%; left:26%;"></div>
</div>

Answer №2

Have you attempted modifying the position property in the CSS rule for #outerdiv to position:relative?

I believe this change will resolve the issue. I am unable to replicate the problem on my mobile device at the moment, likely due to the absence of a working code sample. :-)

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

How do you use CSS to replace an HTML "rules" attribute?

When publishing to HTML using the DITA Open Toolkit, certain inline table attributes are automatically applied such as frame="border" and rules="all". I am facing an issue where I need to override the "rules" attribute for table cells using CSS styles. Wh ...

How to transform HTML content into plain text format while retaining the HTML tags

transform this into something like this2 I'm attempting to convert text containing html tags (p, ol, b) into plain text format (similar to the outcome of running a code snippet) - I've experimented with the following code in .net core but it only ...

How can I center <text> within a button using Vue Native?

Is there a way to position the "login" text in the center of the button? I've attempted using align-items: center; and justify-content: center;, but it doesn't seem to be working <template> <view class="container"> <vi ...

Completing the regex properly

When using my editor, I am able to paste a video URL which is then converted by regex into an embed code. The URL in the WYSIWYG-editor looks like this: Once converted, the output HTML appears as: <p>http://emedia.is.ed.ac.uk:8080/JW/wsconfig.xml& ...

Setting up an image DIV for a dynamic overlay effect

Here is a JSFiddle that works correctly with fixed height and width: http://jsfiddle.net/69td39ha/2/. The animation activates correctly when hovered over. I attempted to adapt the above example to be responsive without fixed dimensions. However, I'm ...

What is the process for implementing filtered HTML attributes in a directive?

I’ve created a custom directive that generates a popup menu by extracting data from HTML content. The purpose is to transform a group of Bootstrap carousel-compatible elements into a structured list. However, each .item element contains an attribute with ...

What could be causing the issue with the focus not being activated when clicking on the input field in Vue?

I am facing an issue where I have to click twice in order to focus on a specific input field, and I'm having trouble setting the cursor at the end of the input. I attempted to use $refs, but it seems like there may be a deeper underlying problem. Any ...

Remove hidden data upon moving cursor over table rows after a delay

Hey there! I'm in need of a little assistance, so I hope you can lend me a hand. Here's the scenario: Within my category table, there are numerous rows, each containing a hidden textbox with an empty value and a unique ID. As users navigate t ...

Concealing the Footer on Mobile Websites in Muse UI When the Keyboard Appears

In this project, I am using vue.js along with muse.ui and only incorporating JavaScript and CSS without the jQuery library. Currently, the footer position always ends up on top of the keyboard whenever an input field is focused. Is there a way to ensure th ...

How to retrieve information from a pre-populated <textarea> using Google Maps script in AngularJS

Is there a way to transfer data from a prefilled <textarea> that is populated by accessing the Google Maps API in a JavaScript script, into an AngularJS controller? $scope.Add=function(msg){ $log.log(msg) } <div ng-app=""> <div ng-contro ...

What is the best way to combine a string with a variable in sass?

Is there a way to merge a string and a variable in Sass to form a variable name that is already present? $size: "sm"; $button-sm: 1rem; $buttom-md: 1.5rem; body { font-size: $button-#{$size}; } The desired output is: body { font-size: 1r ...

Adjust image size based on the size of the screen

I am facing an issue with two images that are created for a 1024 x 768 resolution but I need to use them in a higher resolution. The challenge is to align these two images so that the circle from the first image falls between the second image. Although I ...

Non-functioning Tooltips on Google Charts

Currently, I am working on integrating Google Chart with ASP.NET and linking it to a SQL Server database. I have encountered an issue while attempting to customize the tool tip. Below is the Header Code: <script src="js/jquery/jquery-1.10.2.js" type=" ...

Using a background image in CSS for horizontal rules can override other styling rules

Encountered an unusual CSS bug: After using background-image to style hr, none of the subsequent rules or selectors are displaying on the page: hr { border: 0; height: 1px; background-image: -webkit-linear-gradient(left, rgba(0,0,0,0), rgba(0,0,0,0 ...

Div scrolling allows for parallel viewing of PDFs in a side by side arrangement

I have two scrollable elements on my webpage: one positioned on the left side and the other on the right side. The left element is a regular div, while the right element is an object containing an embedded PDF. <object width="100%" height=&quo ...

Using jQuery Backstretch to generate a stunning image slideshow from the src attributes of images

I am utilizing the slideshow feature of jQuery Backstretch on my website. My plan is to use this functionality on multiple pages, each with its own unique set of images. To streamline the process, I believe it would be more efficient to extract the image s ...

Hide in certain zones, contextual menu

I recently discovered how to create my own context menu with links based on the div clicked (check out here for more details). However, I am facing an issue now. I am struggling to prevent the context menu from appearing above the specific div boxes where ...

Does running npm install automatically compile the library code as well?

I have a query regarding npm and its functionality. I also posted the same question on Reddit, but haven't received a satisfying answer yet. Let's use the jQuery npm package as a case study. Upon running the command npm install jquery, I notic ...

I'm struggling with my project called "Number TSP" and I can't seem to figure out why it's not working properly

Upon reaching the end of my code, I am encountering an issue where instead of seeing the expected output of "Done!", it displays undefined. This is the code in question: const container = document.querySelector(".container") const table = document.querySe ...

What is the equivalent of preg_replace in PHP using jquery/javascript replace?

One useful feature of the preg_replace function in PHP is its ability to limit the number of replacements made. This means that you can specify certain occurrences to be replaced while leaving others untouched. For example, you could replace the first occu ...