Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

415
Views
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 answers
Answer question

0

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

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!