Only in Safari - position:fixed child cut off when parent is position:fixed and overflow:hidden
Asked Answered
S

2

33

I'm only seeing this issue in Safari (6.1 os x)

When a parent element is set to position:fixed; overflow:hidden and a child element is set to position:fixed and part of it overflows the parent, it gets cut off.

Check out this jsfiddle in Chrome and Safari to see what I mean: http://jsfiddle.net/y2dg65y7/3/

<div class="wrapper">
  <div class="inner">
    Why is cut off in Safari?       
  </div>
</div>

.wrapper {
  position: fixed;
  overflow: hidden;
  width: 200px;
  height: 400px;
  background-color: red;
}

.inner {
  position: fixed;
  top: 50px;
  left: 40px;
  width: 400px;
  height: 200px;
  padding: 20px;
  background-color: silver;
}

Is this a bug in Safari? Any ideas? Workarounds?

Steadfast answered 2/11, 2014 at 21:55 Comment(7)
Seems like a bug in Safari. The fixed position is always relative to the viewport so it should never be cut off by a parent.Inceptive
Drat - guess I'll bust out some .js to hack this into working. Thanks.Steadfast
After reading this yuiblog.com/blog/2010/09/27/… I am not too sure if my other comment was correct. The workaround would be to give the parent a different position but that depends on what you are trying to achieve.Inceptive
Thanks for checking. But I need both the outer and inner div to have a fixed position. Was able to make it work using .js to move the inner div in/out of the outer div depending on the screen size.Steadfast
For future Googlers, this is still an open issue. rolls eyes See bugs.webkit.org/show_bug.cgi?id=160953Continental
2020 update: It's still an open issue. Seems like Safari is the new IEThread
Year 2022 update: Safari is still broken after 6 years: bugs.webkit.org/show_bug.cgi?id=160953 – Safari is definitely the new MSIE and unfortunately all Apple mobile customers are forced to use this broken browser!Warmongering
S
11

I found a solution that's working for me in Safari 13.0.2 on macOS Catalina 10.15.

The trick was to split position: fixed and overflow: hidden across two divs, like so:

<div class="wrapper">
  <div class="wrap2">
    <div class="inner">
        Great success in safari 13.0.2 on MacOS Catalina 10.15       
    </div>
  </div>
</div>
.wrapper{
    background-color: red;
    padding: 40px;
    width: 200px;
    height: 400px;
    position: fixed;
    top: 0;
    left: 0;
}

.wrap2{
  background-color: yellow;
  overflow: hidden;
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
}

.inner{
    background-color: silver;
    padding: 20px;
    width: 400px;
    height: 200px;
    position: fixed !important;
    top: 50px;
    left: 40px;
}

And a JS fiddle: https://jsfiddle.net/jxmallett/gsyb326v/1/

Edit: Confirmed this works in Safari on iOS 12.3.1 on an iPad.

Schacker answered 9/1, 2020 at 1:55 Comment(3)
Thanks. This worked for me. I also found this interesting blog which helped me figure out what the actual issue is miketaylr.com/posts/2015/06/position-fixed-overflow-hidden.htmlThebes
Separating position: fixed; and overflow: hidden; into 2 divs worked for me in Safari, thanks!Thread
This is the bug you're looking for: bugs.webkit.org/show_bug.cgi?id=160953 – and I don't expect that to be fixed any time soon. Apple has been aware about that bug since 2016 and it's still unfixed.Warmongering
H
-1

not sure if this is what you wanted but this works

overflow: visible;

.wrapper{ background-color: red; width: 200px; overflow: visible; height: 400px; position: fixed; }

Hearty answered 19/8, 2015 at 0:32 Comment(1)
How is this a solution? An element with overflow: hidden should not hide children with position: fixed. There is a reason if the user set the element to overflow: hidden, and you're telling him to remove this.Duffel

© 2022 - 2024 — McMap. All rights reserved.