The precise placement of DIVs with a background image

Is there a way to properly position DIVs on top of a background image without them shifting when the screen size changes? Media Query hasn't been successful for me. Any suggestions?

Here is my code:

<div class="div-bg" style="background-image:url('https://image.ibb.co/f1qio5/insights_indiamap.jpg')">
  <div class="cities Delhi"></div>
  <div class="cities Bangalore"></div>
</div>

CSS:

.div-bg {
    height: 85vh;
    min-height: 500px;
    background-size: contain;
    background-repeat: no-repeat;
    background-position: 50% 50%;
    position: relative;
}
.cities {
    border: 1px solid red;
    width: 10px;
    height: 10px;
    border-radius: 50%;
    background-color: red;
}
.cities.Delhi {
    position: absolute;
    top: 150px;
    left: 175px;
}
.cities.Bangalore {
    position: absolute;
    top: 250px;
    left: 275px;
}

Check out the JSFIDDLE DEMO

Answer №1

By applying a specific width to the container element

.container{ width:800px;}

your problem should be resolved

Answer №2

The red dots remain static; it's the background image within div-bg that shifts. To observe this, resize and inspect the div element. To prevent this behavior, consider assigning fixed dimensions to the div. See the updated version on this fiddle.

width: 500px;

Answer №3

.background-div{
   width:555px;
}

Apply the above CSS to your project.

Answer №4

Utilize vmin units for the image dimensions as they will adjust smoothly to the viewport size.

Additionally, position the cities using percentages.

.div-bg {
    height: 100vmin;
    width: 100vmin;
    background-size: contain;
    background-repeat: no-repeat;
    background-position: 50% 50%;
    position: relative;
}
.cities {
    border: 1px solid red;
    width: 10px;
    height: 10px;
    border-radius: 50%;
    background-color: red;
}
.cities.Delhi {
    position: absolute;
    top: 27%;
    left: 30%;
}
.cities.Bangalore {
    position: absolute;
    top: 85%;
    left: 33%;
}
<div class="div-bg" style="background-image:url('https://image.ibb.co/f1qio5/insights_indiamap.jpg')">
  <div class="cities Delhi"></div>
  <div class="cities Bangalore"></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

Enhancing react-to-print functionality for optimal performance across various browsers and mobile platforms

Currently, I am developing a sample react-to-print resume template to be included in a larger portfolio website. While testing the page on Chrome and Edge browsers on a laptop, everything seems optimized. However, there are issues when using Firefox; the b ...

The background color is not displaying correctly on the <div> element

body { background-color: #FFFDEC; } .header { position: fixed; top: 0; left: 0; height: 50px; width: 100%; background-color: #04A7A6; margin: 0; display: block; z-index: 10; } .headermenu { xposition: fixed; ...

Using an onClick event along with jQuery to change the CSS class style of another div

After searching extensively without success, I decided to register and ask my first question here. Hopefully, someone can provide a solution: My goal is to create a set of five buttons (divs) with onClick events that will show five different divs. I' ...

What are the reasons behind my item not triggering an alert even after I have created it, saved it, and attempted to alert it?

I am currently working on a code that allows users to input information which is then stored in an object. However, despite my efforts, when I attempt to retrieve the saved object by alerting one of its values, it indicates that the object does not exist. ...

Pictures failing to appear in css/jquery slider

I have a total of 12 divs that are configured to display a thumbnail along with some text dynamically retrieved from a database query. When displayed individually, they appear fine in a column format with a border around them showcasing the thumbnail and t ...

I would like my division to split into two halves, each with a width of 50%, and be aligned next to each other using fxLayout

<div fxLayout="row" fxLayoutAlign="space-between" style="background-color: blue;"> <div fxLayout="column" style="width: 50%;">1stOne </div> <div fxLayout="column" styl ...

If the HTML attribute "dir" is set to "rtl", then add a class to the body

I have a website that supports both English and Arabic languages. When the language is changed to Arabic, the "dir" attribute with a value of "rtl" is added to the HTML <html dir="rtl">. I want to add a class to the body element when the language i ...

Tap on the screen to initiate a phone call using dompdf

I recently came across a recommendation to include the following code snippet to enable click-to-call functionality: <a href="tel:555-555-5555">555-555-5555</a> However, I encountered an issue when trying to implement this in dompdf. ...

Stopping the spread of popup alerts: A comprehensive guide

I'm having trouble explaining this in English;-) Whenever I select an option, an alert pops up for each choice and I don't know how to stop it. Step 1: Choose the "aaaa" option, for example 1111 alert ... Step 2: Choose the "bbbb" option, for ...

position an element at a higher level than the #top-layer

I developed a custom tool and utilized z-index to position it above all other elements: .customTool { position: fixed; z-index: 99999; } However, this functionality stops working when a modal <dialog /> element is opened in modal mode. As a res ...

What is the best way to fit the text into a DIV row as efficiently as possible?

Looking for a solution to prevent a span from dropping down to the next line when text is too long within a fixed width and height row. The row consists of three elements, two with fixed widths and the third containing a span and text. Constraints include ...

Issue with variable passing during image upload

I am attempting to include an upload button alongside each recipe on my website. In order to save the image path next to each recipe in my database, I need to pass the recipe ID value during the image upload process. However, I keep encountering the same e ...

`Resolving CORS error when setting up an AWS Lambda function with API Gateway on AWS`

In the AWS lambda function provided below, I have set it up to call an API that lists all country names. The function is connected to an API Gateway and works as expected when tested with Postman. Here is the code snippet: import axios from 'axios&apo ...

Is it possible for links to remain clickable even if they pass underneath a div

I have implemented a shadow using a div element to cover some content beneath it. However, I am facing an issue where the div that is under the shadow cannot be interacted with. Is there any solution to this problem? Thank you ...

Gather image origins from a website, even if img tags are inserted through javascript or other methods during the page's rendering

I am looking to extract the URLs of all the images from a web page using C# and asp.net. Currently, I am utilizing: WebClient client = new WebClient(); string mainSource = client.DownloadString(URL); Afterwards, I am scanning the mainSource string for i ...

I am attempting to format an HTML form with proper indentation, but it seems to be eluding me

Struggling to design a custom registration form template, I'm facing an issue where paragraph spaces or indents are not being recognized by the program, even in the preview pane. Seeking assistance with this problem. I am attempting to position this ...

Using JQuery to Evaluate HTML into an Element

When working with PHP pages, I often need to fetch data from another source and populate an input or textarea field. However, I encounter issues when dealing with HTML tags and apostrophes. In my experience, passing around HTML content using JavaScript ca ...

Changing the display of elements using Javascript in IE6 by modifying class

Currently, I am facing an issue at work that involves a piece of code. The code functions correctly in Firefox 3.6 where clicking on a row changes its class name and should also change the properties of the child elements. However, in IE6 and possibly othe ...

Content move to the left with Material UI's shifting capabilities

Is anyone experiencing an issue with Material UI dropdown Select and Modals causing the content to shift to the left? I attempted using position: absolute on both the Select element and MenuItems, but unfortunately the content still shifted. Has anyone e ...

Exploring the ins and outs of flexbox and utilizing overflow:auto

In my current situation, I have successfully resolved all cases but am now seeking assistance in debugging my mental model. Specifically, I am focusing solely on Chrome for the sake of simplicity. Within my nested "holy grail-ish" flexbox layouts, I have ...