Maintaining CSS elements in position

Hello everyone,

I have a project at work where I need to create a map with specific cities pinpointed. I've completed it on my computer and now I'm trying to ensure that when I transfer it to another device, like a laptop, the points remain in their correct positions. This is my first time posting here, so I hope this information gives you an idea of what I am trying to achieve. Any help or advice would be greatly appreciated.

Here is a snippet of the code:

<html>
<head>
<meta charset="utf-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>States/Cities</title>
</head>
<style media="screen">
  html,body{
  background-color: #AADAFE;
  } 
  .Vegasdot{
  height: 10px;
  width: 10px;
  border-radius: 50%;
  background-color: black;
  top: 246px;
  left: 306px;
  position: absolute;
  z-index: 2;
  }

  .SaltLakedot{
  height: 10px;
  width: 10px;
  border-radius: 50%;
  background-color: black;
  top: 88px;
  left: 395px;
  position: absolute;
  z-index: 2;
  }

  .Phoenixdot{
  height: 10px;
  width: 10px;
  border-radius: 50%;
  background-color: black;
  top: 333px;
  left: 386px;
  position: absolute;
  z-index: 2;
  }

  .statesMap{
  padding-top:3%;
  padding-left:12%;
  position: absolute;
  z-index: 1;
  }

<img src="StatesGIMP.png" alt="States Map"
class="statesMap" usemap="#States">

<span class="Vegasdot"></span>
<span class="SaltLakedot"></span>
<span class="Phoenixdot"></span>

Answer №1

Several key elements are at play in this scenario.

To begin with, the position: absolute; property is dependent on its parent container. By enclosing your image and dots within a wrapper div, you can position the dots in relation to this .wrapper. The .wrapper must have position: relative; applied to serve as the relative parent for the position: absolute; children.

Secondly, it's essential to use percentages when positioning the dots so that they adjust proportionally with the image.

Thirdly, incorporating transform: translate(-50%,-50%) establishes the point of reference at the center of each dot, ensuring that as the image scales, the central point of the dot remains constant.

Moreover, consolidating common styling rules into a shared class like .dot enhances the efficiency of your CSS code.

Here is an example showcasing these principles: https://codepen.io/tylerfowle/pen/MrwreX

HTML:

<div class="wrapper">
  <img src="http://ontheworldmap.com/usa/usa-states-map.jpg" alt="States Map"
       class="statesMap" usemap="#States">
  <span class="Vegasdot dot"></span>
  <span class="SaltLakedot dot"></span>
  <span class="Phoenixdot dot"></span>
</div>

CSS:

html,body{
  background-color: #AADAFE;
}

.wrapper {
  position: relative;
}

.dot {
  height: 10px;
  width: 10px;
  border-radius: 50%;
  background: black;
  z-index: 2;
  position: absolute;
  transform: translate(-50%,-50%);
}
.Vegasdot{
  top: 48%;
  left: 17%;
}

.SaltLakedot{
  top: 37%;
  left: 24.5%;
}

.Phoenixdot{
  top: 60%;
  left: 22%;
}

.statesMap{
  width: 100%;
  z-index: 1;
}

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

Safely render user-generated HTML content within a React application to prevent cross-site scripting vulnerabilities

A unique situation has arisen in our React app where a user inputs a string that is displayed to other users. The challenge lies in searching for specific characters within the string and replacing them with HTML elements. For example, transforming the wor ...

Sentence following the original passage

I want to achieve a similar look as the example below: Here is what I have done so far: h2:after { content: ""; display: inline-block; height: 0.5em; vertical-align: bottom; width: 48px; margin-right: -100%; margin-left: 10px; border-bo ...

Having trouble with the "Corrupted @import" error during grunt build?

As I embark on my journey to create my very first Angular application, I have encountered a roadblock. Using Yeoman and angular-generator, everything seemed to be running smoothly with "grunt serve." However, when I attempted to execute "grunt build," the ...

Designing dynamic SVG elements that maintain uniform stroke widths and rounded edges

I'm currently tackling a project that involves creating SVG shapes with strokes that adjust responsively to the size of their parent container. My aim is for these shapes to consistently fill the width and height of the parent container, and I intend ...

Discover the concealed_elem annotations through the power of JavaScript

As I work on my new website, I am struggling with narrowing down the web code. I came across a solution that seems fitting for what I need, but unfortunately, I can't seem to make it work: I attempted the non-jQuery solution, however, I must be missi ...

Chrome does not recognize the 'position: fixed' style attribute

Encountered an issue in Chrome where the position:fixed property does not behave as expected. While this code works fine in Firefox and Safari, it seems to have a glitch in Chrome (69.0.3497.100). When scrolling, the blue div moves and reveals the green ba ...

Click the button in Javascript to add new content

I am currently experimenting with JavaScript to dynamically add new content upon clicking a button. Although I have successfully implemented the JavaScript code to work when the button is clicked once, I would like it to produce a new 'hello world&ap ...

"Transitioning from jQuery to Vanilla Javascript: Mastering Scroll Animations

I'm seeking guidance on how to convert this jQuery code into pure Javascript. $('.revealedBox').each(function() { if ($(window).scrollTop() + $(window).height() > $(this).offset().top + $(this).outerHeight()) { $(this).addCla ...

What is the best way to conceal floated elements with overflow in CSS?

How can I ensure that my block container element only displays the content of block elements, cutting off any floated elements that are taller than the parent container? Applying overflow: hidden seemed like a simple solution, but it created a new block f ...

Improve the Popup to seamlessly elevate

In my project, I have implemented a pop-up dialog box that rises up from the left bottom corner as the user scrolls down the page. You can view it live at this link- However, I am facing an issue where the initial lift up of the dialog box is not smooth, ...

Using input masking to restrict user input to only numbers and English alphabet characters

My input field is masked as "999999999" and functions correctly with an English keyboard. However, I am able to enter Japanese/Chinese characters into it, which breaks the masking. Is there a way to limit input to English keyboard numerics only? <inpu ...

The synergy between HTML and JavaScript: A beginner's guide to frontend coding

I have primarily focused on server-side development of enterprise applications (Java EE, Spring framework). However, I am now exploring client-side technologies for better understanding (not necessarily to become an expert). I have studied HTML and CSS th ...

What are some ways to adjust the size of the option field in a select menu?

In my ionic popup, I have a HTML property called select with nested options. Below is the code snippet along with the applied CSS. My query is how can I set the white space to occupy the entire area of the select? https://i.stack.imgur.com/iPqAa.png http ...

If a specific class is identified, add a border to the div when clicked using JavaScript

Is there a way to use javascript/jquery to add a border to a selected div? I have multiple rows with columns, and I want only one column per row to be highlighted with a border when clicked. Each row should have one column with a border, so it's clear ...

When I click on a tab section to expand it, the carat arrows all point upwards. Only the arrows corresponding to the selected section should

click here for imageIt appears that there are four tabs, each with a click function on the carat icon. When I expand one tab, all carats point upwards instead of only the selected one appearing. accountSelection(account) { if (!this.selectedAccoun ...

Tips for accessing the names of subdirectories and files on a website

If we have the URL, is it possible to access the names of the files within the parent directory? Similar to how we can do in DOS by using the command "DIR" to view the list of files inside a folder. For example, if we have a URL like , I am curious to kno ...

Leveraging strings as URLs to embed PDFs in Wordpress using PDF Embedder

I'm encountering an issue related to a Wordpress plugin called PDF Embedder, as well as concatenating/using a string with document.write. My goal is to make this code work: <script src="http://nooze.org/wp-content/uploads/scripts/dateGetter.js"> ...

Looking to implement a CSS banner image for seamless customization across multiple pages

Is it possible to change the banner image easily in the CSS without having to update every HTML page? I don't want the image source to be directly on the HTML pages. How can this be achieved? Additionally, there are already two references in the CSS: ...

Ways to navigate to a different page using HTML response from an AJAX request

After receiving an HTML document as a response from an AJAX call, I need to create a redirection page using this HTML response. Can anyone provide guidance on how to achieve this? ...

What is causing this image to be off-center both vertically and horizontally on the table?

Currently, I have a small 20px by 20px image displaying in the top left corner of an empty table: <table width="100px"> <tr> <td width='100%' align='center' valign='center'> <img i ...