Label the image with unique div elements that will remain attached to the image

I am attempting to tag some divs onto an image that remains in the correct position as the window size changes.

My current setup is somewhat functional, but the markers only stay aligned with the image until it loses height. How can I modify my code to ensure the markers always remain attached to the image?

You can clearly observe this behavior by running the code in full-screen mode.

.container-fluid {
  padding: 5%;
  background-color: #EDEDED;
}

img {
  display: block;
  width: 60%;
}

.marker {
  position: absolute;
  width: 35px;
  height: 35px;
  border-radius: 50%;
  color: white;
  background-color: rgba(0,0,0,.5);
  text-align: center;
  padding-top: 4px;
  display: table-cell;
  vertical-align: middle;
  &::after {
    padding: 5px 10px;
    position: absolute;
    top: -2px;
    left: -2px;
    border-radius: 10px;
    background-color: rgba(0,0,0);
    display: none;
    content: attr(data-after-content);
    width: 150px;
    color: white;
    font-family: "Zuric Light";
  }
  &:hover::after {
    display: block;
  }
}

.marker-one {
  left: 43%;
  top: 12%;
}

.marker-two {
  left: 61%;
  top: 39%;
}

.marker-three {
  left: 49%;
  top: 61%;
}

.marker-four {
  left: 55%;
  top: 77%;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container-fluid position-relative min-vh-100 d-flex justify-content-center">
  <img src="https://firebasestorage.googleapis.com/v0/b/projektarbeit-interstuhl.appspot.com/o/pure%2Fpure_back_full_red.png?alt=media&token=02cac375-3340-4bc6-bbed-6ada0614a3f9" alt="Pure" class="align-self-center">

  <div class="marker marker-one" data-after-content="Info">
    +
  </div>

  <div class="marker marker-two" data-after-content="Info">
    +
  </div>

  <div class="marker marker-three" data-after-content="Info">
    +
  </div>

  <div class="marker marker-four" data-after-content="Info">
    +
  </div>

</div>

Answer №1

If you're looking to keep a div on top of an image when the screen is resized, there's a simple solution. You just need to create a container around the image set to 60% width of the page container and position it as relative. This ensures that the hover buttons are positioned based on the total width of that container, not the entire page.

Try using this updated HTML:

<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container-fluid position-relative min-vh-100 d-flex justify-content-center">
  <div class="image-container">
    <img src="https://firebasestorage.googleapis.com/v0/b/projektarbeit-interstuhl.appspot.com/o/pure%2Fpure_back_full_red.png?alt=media&token=02cac375-3340-4bc6-bbed-6ada0614a3f9" alt="Pure" class="align-self-center">

    <div class="marker marker-one" data-after-content="Info">
      +
    </div>

    <div class="marker marker-two" data-after-content="Info">
      +
    </div>

    <div class="marker marker-three" data-after-content="Info">
      +
    </div>

    <div class="marker marker-four" data-after-content="Info">
      +
    </div>
</div>

</div>

Check out the Updated CSS below for styling:

.container-fluid {
  padding: 5%;
  background-color: #EDEDED;
}

img {
  display: block;
  width: 100%;
    max-width: 100%;
    max-height: fit-content !important;
}

.image-container {
    width: 60% !important;
    height: fit-content !important;
    margin: 0 auto;
    position: relative;
}

.marker {
  position: absolute;
  width: 35px;
  height: 35px;
  border-radius: 50%;
  color: white;
  background-color: rgba(0,0,0,.5);
  text-align: center;
  padding-top: 4px;
  &::after {
    padding: 5px 10px;
    position: absolute;
    top: -2px;
    left: -2px;
    border-radius: 10px;
    background-color: rgba(0,0,0,0);
    display: none;
    content: attr(data-after-content);
    width: 150px;
    color: white;
    font-family: "Zuric Light";
  }
  &:hover::after {
    display: block;
  }
}

.marker-one {
  left: 38%;
  top: 5%;
}

.marker-two {
  left: 70%;
  top: 36%;
}

.marker-three {
  left: 46%;
  top: 61%;
}

.marker-four {
  left: 55%;
  top: 80%;
}

With these adjustments, your layout should now function properly! Feel free to give it a try!

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

Reveal a concealed div in a modal form depending on the value present in the input field

After spending the past 4 hours scouring through Google and SO, I finally stumbled upon an answer HERE that seemed to be somewhat close to what I was looking for. However, in my scenario, the <div class="fhlbinfo"> elements are not displaying or hi ...

Creating a div that expands to fill up the remaining height within a flex-child

I'm facing a challenge with a flex container that contains flex children. Within each flex child, there are two stacked divs, the first of which has an unknown height. My goal is to make the second div fill the remaining height available. I've be ...

When the 'Show More' button is clicked, one Div will smoothly slide over another Div

I've been struggling for hours to find a way to make one DIV slide over another DIV below it instead of pushing it down. The setup is quite straightforward. I have a script that reveals more text when the 'Show More' button is clicked. Desp ...

What is the best way to assign a URL to an image source using a JavaScript variable?

I am working on a script that displays different image URLs based on the time of day using an if-else statement. I want to dynamically load these images in an img src tag so that a different picture is loaded for each time of day. I have defined variables ...

Was unable to maintain the default state of the button as clicked

When the website loads, I expected the navbar to have a certain design. However, I encountered an issue where I couldn't set the button's state as clicked. If you are curious about the output I achieved, you can watch it in video format here: H ...

What is the best way to transition a div from the top to the bottom in mobile view?

Currently, I am utilizing Bootstrap 4 framework; however, if it operates effectively on version 3, then it should work seamlessly on v4. Within a single column, I have incorporated 2 div elements as follows: <div class="row"> <div class="col ...

Achieve centered alignment for a website with floated elements while displaying the complete container background color

I am currently working on a website that consists of the basic elements like Container, Header, Content, and Footer. The container has a background-color that should cover the entire content of the site, including the header, content, and footer. However, ...

Ways to show div1 when hovering over div2 and make div1 fade out when the mouse moves away?

I'm looking to create an effect where div1 appears when hovering over div2, and only disappears when neither div1 nor div2 are being hovered over. I attempted to achieve this using CSS and jQuery, but encountered an issue where div1 would disappear i ...

Ways to move upward from a lower position

I am trying to create a hover effect where the title and content move to the right position upon hovering. The issue I am facing is that the transition on the `Title` class is not working, and as a result, the hover effect is also not functioning properly ...

Unable to relocate CSS Loader Container

Can someone help me with adjusting the placement of my container? I'm struggling to maintain the styles while getting rid of the position: absolute; on the .dot class. Every attempt I make is resulting in the dots moving erratically! To clarify, I w ...

rearranging elements using CSS 2D transformations

While W3Schools offers a plethora of CSS3 information, I found there to be lacking in details about the placement of values within a 2D Transform matrix. Could someone please provide clarification on how each of the six parameters should be assigned? div ...

Is there a way to invoke a function in an iframe from its parent?

How can I call a function that is in an iframe from the parent document? If the function were in the parent, I could simply use parent.func(); but since it's within the iframe, how can I still call it successfully? JavaScript keeps saying it can' ...

Issue with Bootstrap dropdown-menu alignment when image_tag contains the "center-block" class

<ul class="logo"> <li class="navtabs dropdown"> <a class="dropdown-toggle" id="dropdownMenu2" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true"> <%= image_tag('Thumbnailimagestufffurr ...

Leveraging the power of HTML5's details element for optimized printing

The <details> HTML5 element is used to provide a simple show/hide functionality. While this feature works well on-screen, it poses an issue when printing as any closed <details> elements disappear in the printed version. I attempted to fix th ...

Creating a webpage with a left panel and draggable elements using JavaScript/jQuery

As someone new to Javascript/jQuery, I have been given the task of creating a web page with elements in a "pane" on the left side that can be dragged into a "droppable" div area on the right side. The entire right side will act as a droppable zone. I am u ...

Strangely Bizarre CSS Quirk

At this website, there seems to be an issue with how the footer displays on Internet Explorer and certain older versions of Firefox. However, it appears perfectly fine on Firefox 3.6.13. Any suggestions on what could be causing this discrepancy? ...

Using JavaFX to create a TreeTableView with nodes of varying sizes

I need assistance with styling TreeTableViews and their rows. I have set up a treetableview showcasing objects named Component, divided into classes A, B, and C. In my setup, B objects are nested within A, and C objects are nested within B objects. While I ...

Conflicting CSS selection elements in WordPress causing theme theme selection conflicts

Despite trying various edits to my custom CSS, such as including ::selection{color: white; background: #2f3f58;}, copying and pasting code from sites like W3Schools and stack overflow, nothing seemes to work. I am using the Hueman theme from , which has a ...

Generating various fields within a single row

I am in the process of creating a dynamic form that should generate two new fields in the same line when the user clicks on the plus icon. Currently, the code I have only creates a single field. I attempted to duplicate the code within the function, but i ...

Picture placed outside div within infobubble

I'm currently working with Google Maps to display an InfoWindow using the InfoBubble library. The issue I'm encountering is that when I try to show an image outside of a div using CSS properties, only half of the image displays within the div. B ...