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

412
Vistas
Make fixed page elements "honor" the body right padding?

My fixed button triggers a menu. While menu is opened, the body has overflow-y: hidden and a padding-right: 17px (actually this value is computed, omitted for brevity). The button (position: fixed) is shifted.

The HTML (the <p> is only needed to show the scrollbar):

<body>
  <button class="btn">
    Menu
  </button>
  <p style="height: 5000px;"></p>
</body>

To the CSS... note that .menu-opened is added when you click the button, via JavaScript:

body {
  background-color: blue;
}

body.menu-opened {
  padding-right: 17px;
  overflow-y: hidden;
}

/* Useless styles */
.btn {
  background-color: red;
  position: fixed;
  padding: 1rem;
  top: 0;
  right: 0;
  margin-right: 2rem;
  margin-top: 2rem;
}

Example pen here

As soon as the scrollbars are disabled, button is shifted because fixed elements doesn't honor the body padding (which is added to compensate the absence of the scrollbar). Any idea on how to solve this, without touching all fixed elements?

I think this is a common problem, so there should be an easy solution, but I can't get it.

Fixed elements shifted

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

0

Take a look at scrollbar-gutter: MDN: scrollbar-gutter

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