Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

207
Visualizações
Anchor link to div on the same page not working in Safari

I have a React based website that includes this list of links on the top of every page:

<ul>
    <li><a href="/">HOME</a></li>
    <li><a href="/about-us">ABOUT US</a></li>
    <li><a href="/#contactSection">CONTACT US</a></li>
</ul>

At the bottom of the home page I have this piece of HTML:

<section id="contactSection" className="contactSection ptb_100">
    <h2>Contact Us</h2>
    ...HTML FORM...
</section>

When I click the Contact Us link from the top of the home page, Chrome will scroll to the correct HTML identified by the contactSection, however, Safari will scroll to the top of the page. When I click the Contact Us link from another page on the site, or if I type the #contactSection into the address bar and hit enter, Safari correctly scrolls.

I have looked at threads suggesting to add the / to the href, but that didn't seem to work. Any suggestions?

UPDATE The cause of the issue was a sticky header on the top of the page that caused Safari to keep the page at the top. Removing the sticky header, solved the issue, although not sure why.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You shouldn't add "/" for same page id links

html {
  scroll-behavior: smooth;
}

#myDiv {
  /* To enable scroll */
  margin-top: 3000px;

  /* Extras (Not required) */
  padding-bottom: 2em;
  background: red;
}
<a href="#myDiv">
    Click mo to go to myDiv!
</a>

<div id="myDiv">This is myDiv</div>

about 4 years ago · Juan Pablo Isaza Relatório

0

Try this. In HTML

<li><span onclick="scrollToElement('#contactSection')">CONTACT US</span></li>

and in JS

function scrollToElement(id) {
  const el = document.querySelector(id);

  if (el) {
    el.scrollIntoView();
  }
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda