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

178
Vistas
HTML closed details summary - how to scroll to anchor inside
<html><body>
<details open><summary>This is the summary</summary>
  <a name="a1"></a>
  <p>Here are all the details.</p>
  </details>
<p>Imagine more than a page of text here.</p>
<p><a href="#a1">This</a> is a link pointing inside the details.</p>
</body></html>

I'm opening my above HTML code with the Firefox browser. When I click on the "This" link Firefox correctly scrolls to the page to the details.

However, when I click the triangle left to the title "This is the summary", and the details (including the line with the anchor) get closed (disappear), clicking on the link Firefox does not scroll to the anchor any more.

I would have expected Firefox at least to scroll to the visible summary that hides the anchor even when the details are closed, but ideally also to open up the details and scroll exactly to the anchor.

Is there a way (HTML? css? using which browser? other?) that enables scrolling to anchors in closed (folded) details of the details-summary block?

My question is related to Scroll to anchor when expanding details/summary?, but I would prefer a javascript-free solution if one exists.

To test your solution please substitute the "more than a page of text here" with actually more than one page of text.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

The problem is that anchor tags only scroll to a local element if it matches an id, not name, attribute. You must have an element that matches the given id in the a href to scroll to it. Here is the fixed code:

<html>
<body>
  <details>
    <summary>This is the summary</summary>
    <a id="a1">Scroll here</a>
    <p>
      Here are all the details. Imagine more than a page of text.
    </p>
  </details>
  <p>
    <a href="#a1">This</a>
    is a link pointing inside the details.
  </p>
</body>
</html>

Edit: Also, the reason it does not scroll to the elements within the details when it is closed is because those elements are not actually rendered. The link only scrolls to elements that are currently being rendered. You can easily preview this by adding style="padding-bottom: 100vh" to the details tag and clicking the a tag on the bottom. If you do want it to still scroll to it, then you must add some JS to first open the details tag when the given a tag is clicked or simply add the a1 id to the details tag.

about 4 years ago · Juan Pablo Isaza Denunciar

0

A draft step towards the solution:

var details = [...document.getElementsByTagName("details")];
document.addEventListener('click', function(e) {
  details.forEach( (D,_,A) => D.contains(e.target) ? D.addAttribute('open') : '')
})

However, instead of e.target somehow the targeted anchor should be used.

Iterating over all details has the advantage that in case the anchor is inside multiple closed blocks they will all open up.

about 4 years ago · Juan Pablo Isaza Denunciar
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