I'm encountering an issue with a fixed navbar in twitter-bootstrap-3 that I can't quite find an explanation or solution for.
When I scroll past the top of the page there is different behaviour in different browsers:
In Chrome (MacOS), the whole content scrolls down, like this:
In Safari (also Mac), the navbar stays fixed on top, and the background appears below it:
Ideally, I would prefer the behaviour as it is in Chrome. I have tried numerous approaches with css using overflow
, position
, but I can't figure it out. Can I correct this behaviour with CSS (or perhaps Javascript/jQuery?)
Here's the bare bones of my code (the !important
s are just here to override Bootstrap styling):
body {
background-color: #eee !important;
padding-top: 54px !important;
}
.navbar {
min-height: 54px !important;
border-bottom: 2px solid orange !important;
margin-bottom: 0 !important;
}
.navbar-default {
background: #fff !important;
.navbar-brand {
color: #000 !important;
}
}
.usp-bar {
background-color: orange;
color: #fff;
height: 30px;
width: 100%;
}
.content {
height: 800px;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<body>
<div id="app">
<nav class="navbar navbar-default navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" href="#">
Navbar
</a>
</div>
</div>
</nav>
<div class="usp-bar">
orange bar
</div>
<div class="content">
Content
</div>
</div>
</body>
Hacks
See puresamari's gist "overscroll background-color on ios" that places a fixed
container in the negative space above the page. Doesn't work in Safari 11 or mobile Safari.
Overscroll-Behavior
Chrome 63 introduced the overscroll-behavior
property that lets you disable scroll chaining or overscrolling altogether. However, still not supported in Safari ☹
Final Advice
The most reliable method is setting a background color on html
and another on body
, i.e. http://peter.coffee/htmls-background-color.
It's not yet possible to control how browsers overscroll, so instead set the correct colors and leave negative space at the top of the page for the fixed header.
Interestingly that's what StackOverflow does:
.top-bar {
height: 50px;
}
.top-bar._fixed {
position: fixed;
min-width: auto;
}
.top-bar ~ .container, .top-bar ~ #announcement-banner {
margin-top: 50px;
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments