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

432
Visualizações
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 Respostas
Responde à pergunta

0

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

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