• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Evaluaciones
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

103
Vistas
How to avoid jumping when click on anchor link

I want to show/hide content using purely CSS without JavaScript. I reached on this solution.

When I click on the link it jump down in page. I need pure CSS solution, how to stop it to jump?

p[id^="detailView-"] {
  display: none;
}

p[id^="detailView-"]:target {
  display: block;
}
<a href="#detailView-1">Show View1</a>
<p id="detailView-1">View1</p>

<a href="#detailView-2">Show View2</a>
<p id="detailView-2">View2</p>

almost 3 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Just arrange your html so the anchor tags are at the top like so

p[id^="detailView-"] {
  display: none;
}

p[id^="detailView-"]:target {
  display: block;
}
<a href="#detailView-1">Show View1</a>
<a href="#detailView-2">Show View2</a>

<p id="detailView-1">View1</p>
<p id="detailView-2">View2</p>

almost 3 years ago · Juan Pablo Isaza Denunciar

0

In this situation, instead of toggling between:

  • display: none
  • display: block

you can toggle between:

  • visibility: hidden
  • visibility: visible

The difference betwen the two properties is that:

  • visibility maintains page-space for an element, even when that element is invisible
  • display: none will actually remove the element from the page and collapse the space that it was occupying

Working Example:

p[id^="detailView-"] {
  visibility: hidden;
}

p[id^="detailView-"]:target {
  visibility: visible;
}
<a href="#detailView-1">Show View1</a>
<p id="detailView-1">View1</p>

<a href="#detailView-2">Show View2</a>
<p id="detailView-2">View2</p>


Further Reading:

  • https://developer.mozilla.org/en-US/docs/Web/CSS/visibility
almost 3 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 Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda