Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

161
Vistas
Creating responsive 'Image Map' for elements to match elements of background image (JS/CSS) (React)

I'm trying to create a map that responsively aligns hyperlinks over an (always) full-screen background image.

I've tried SVG's, but that didn't work for this reason.

So now I'm trying to use absolute positioning, so that the x/y AND height/width of the container (and therefore the little 'truffle' buttons) match the respective parts on the background image.

Below is the image, with the 'truffle box' that needs to align above the entire truffle tin (upper screen included)

enter image description here

Instead, what I've got is this (you can see, the viewport might be changing, but since the image isn't yet decreasing in size, neither should the 'truffle box' element): enter image description here

Below is the code I'm using for the TruffleTin, to make it responsively maintain the x/y position, but I have nothing for it's height/width.

import React, { useEffect, useRef } from 'react';
import {
  TruffleTinContainer,
  TruffleTinOutside,
} from '../truffle-tin/styledTruffleTin';

export default function TruffleTin({ backgroundRef }) {
  const truffleTinRef = useRef();
  const initialPos = { x: 150, y: 150 };
  const padding = 25;
  let truffleBoxWidth = 0;
  let truffleBoxHeight = 0;

  useEffect(() => {
    truffleBoxWidth = truffleTinRef.current.offsetWidth;
    truffleBoxHeight = truffleTinRef.current.offsetHeight;
    resize();
  }, []);

  function resize() {
    let backgroundDivRect = backgroundRef.current.getBoundingClientRect();
    truffleTinRef.current.style.left =
      (initialPos.x / truffleBoxWidth) * backgroundDivRect.width -
      padding +
      'px';
    truffleTinRef.current.style.top =
      (initialPos.y / truffleBoxHeight) * backgroundDivRect.height -
      padding +
      'px';
  }

  useEffect(() => {
    window.addEventListener('resize', resize);
    return () => window.removeEventListener('resize', resize);
  });

  return (
    <TruffleTinContainer ref={truffleTinRef}>
      I'm the truffle tin
    </TruffleTinContainer>
  );
}

And here is what the image is, it's a div with a background image:

export const BackgroundLightsOn = styled.div`
  position: absolute;
  background: url(${BgLightsOn});
  background-position: center center;
  background-repeat: no-repeat;
  background-size: cover;
  height: 100%;
  width: 100%;
  border: 1px solid red;
`

Any help would be greatly appreciated. I've looked at countless 'suggestions' for this, but nothing has worked thus far. I'm kind of under the gun here, so the most straightforward and effective answer would be the best! Thanks very much.

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda