When the left/top/right/bottom properties are not specified, using position:fixed produces the desired results in Firefox and Internet Explorer, but not in Safari

It's unfortunate that I have to bring up another question related to position:fixed, but after going through various other questions and forum threads, I'm still not clear on this particular issue.

The code below is a simple illustration of how I've been using position:fixed in a project so far. My original understanding was that position:fixed fixes the position relative to the first parent container with a set position, and then remains fixed regardless of viewport scrolling. However, I've now learned that it actually positions relative to the outermost container (i.e., the html tag), and position:absolute positions relative to the first parent container with a non-static position.

From reading other posts on SO, it seems that the effect I was trying to achieve with position:fixed is one that many others have attempted, only to find out that it cannot be done with CSS alone: That is, positioning an element relative to a container and keeping it in place relative to the viewport when scrolling.

What puzzles me is that what I believed to have achieved - at least in FF and IE8. In the code example below, the "fixed right pane content" starts positioned next to the red "centre scrollable content" box and aligns vertically with the top of the center content. The center content can be scrolled, but the right-hand content stays fixed in its position as if it were initially static in the normal flow of the document but then remains fixed to the viewport.

I now realize that this works in IE8 and FF simply because I did not specify top/bottom/left/right attributes for the fixed element. If I do provide those attributes, the fixed element immediately becomes positioned relative to the viewport.

Until now, I assumed - perhaps naively - that if no relative positions are specified, position:fixed would default to placing the element where it would normally be statically placed. At least FF and IE8 seem to be doing just that.

However, testing in Safari reveals that Safari positions the fixed element to the left of its container. In other words, without any positioning, my position:fixed element is neither where it would be when statically placed nor positioned at 0,0 relative to the viewport.

Have I been relying on poorly defined behavior so far, and should I consider using a JavaScript solution to achieve this fixed positioning? Or, is this behavior well-defined for IE/FF; and could anyone explain why Safari places it as they do?

<style type="text/css">
  #content-centre {
    margin: 0 auto;
    width: 900px;
  }
  
  #header {
    height: 55px;
    position: fixed;
    top: 0;
    width: 990px;
  }
  
  #left-pane {
    position: fixed;
    top: 12px;
    border: 1px green solid;
  }
  
  #main-pane {
    left: 200px;
    position: relative;
    top: 66px;
    width: 760px;
    border: 1px yellow solid;
  }
  
  #container-1 {
    border-top: 1px solid #FFFFFF;
    float: right;
    min-width: 130px;
    padding-left: 20px;
    border: 1px blue solid;
  }
  
  #container-0 {
    margin-right: 20px;
    min-height: 470px;
    overflow: auto;
    padding: 10px 0;
    position: relative;
    border: 1px red solid;
  }
  
  .side-info-list-fixer {
    position: fixed;
  }
</style>

<div id="content-centre">

  <div id="header">
  </div>

  <div id="left-pane">
    Fixed left pane content
  </div>


  <div id="main-pane">
    <div id="page-module-containers">

      <div id="container-1">
        <div class="side-info-list-fixer"> Fixed right pane content </div>
      </div>

      <div id="container-0">
        <p>Centre scrollable content</p>
        <p>Centre scrollable content</p>
        <p>Centre scrollable content</p>
        <p>Centre scrollable content</p>
        <p>Centre scrollable content</p>
        <p>Centre scrollable content</p>
        <p>Centre scrollable content</p>
        <p>Centre scrollable content</p>
        <p>Centre scrollable content</p>
        ...
      </div>

    </div>
  </div>

</div>

Answer №1

It seems like you are in search of a particular answer...

An element's position does not default to 0,0. By default, its position is relative to the containing element. For instance, in your scenario, "#container-1" has a padding-left: 20px while the rest of the padding is set to 0, resulting in the "default" position of the element being 20px to the left of #container-1's wall and 0px from the top of #container-1.

The default position of this element without any changes and with my viewport unscrolled would be 63px from the top, with the left position varying based on browser width. If top and left values are not overridden, they are typically defined as top:63px and left:893px by the rendering engine.

Upon window resize, the position gets adjusted relative to the viewport to maintain fixed positioning as you scroll down.

By simply adding "position:fixed;", your properties (from the browser's perspective) would be:

position:fixed;
top:63px; // default value set during browser rendering
left:893px; // default value set during browser rendering

It's also worth noting that IE6 and similar browsers do not support position:fixed at all. http://fiddle.jshell.net/uaE4g/4/show/

I trust this information proves helpful!

Answer №2

While testing in different browsers, I've noticed a discrepancy between Safari 5 and Safari 6.

It appears that the behavior is consistent across browsers in this scenario:

If a fixed element doesn't have all position properties defined, it defaults to behaving like a static element. After the page loads, it continues to act as if it were static. For example, fixing an element vertically but not horizontally allows for vertical scrolling while the element remains fixed, but horizontal scrolling causes the element to move with the page.

In Safari 5, however, if the fixed element is contained within another element with positioning, it acts differently: initially behaving like a static element, but then staying fixed in place after loading.

The 'side-info-list-fixer' is located within 'main-pane', which has position:relative. Removing this may result in Safari 6-like behavior seen in most other browsers.

It's unclear which behavior is correct or reliable to depend on...

Regards, *-pike

Answer №3

When I needed to keep an element in its original `static` position relative to the viewport and make it `fixed`, I came up with a solution using a custom `jQuery` function.

function MaintainFixedPositioning(element) {
    var initialOffset = $(element).offset();
    $(element).css("position", "fixed");
    $(element).offset(initialOffset);           
}

To implement this, simply call the function like so:

MaintainFixedPosition($("#specific-element"));

In my scenario, I had a secondary navigation menu that initially flowed with the content before becoming fixed in place.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is the background color not being implemented?

I'm struggling with an issue - I can't seem to apply a background color to my top bar. I've tried multiple times but it's still not working. Could you please help me fix this error? Below is the code for my simple top bar: topbar.htm ...

Create waypoints on multiple elements

I am working on implementing a unique feature using a div tag with the class "dipper." <div class = "dipper"> <p>Peekaboo</p> </div> As part of this implementation, I have included a script that triggers the display of the "dipper ...

Overlapping input elements occur when Twitter bootstrap prepended input elements are positioned side by side

I ran into an issue while attempting to place two input elements side by side, with a prefixed item on the first input, using display:table styles. Unfortunately, the second input element ends up overlapping the first. I tried using box-sizing: border-box ...

Generate dynamic lines with evolving hues using CSS3

While working on a fresh web project, I came across the need to emphasize my headers (h1, h2, h3, h4) with underlines. My goal is to have these underlines grow and change color dynamically, similar to what can be seen here: . Is there a way to achieve thi ...

Implementing a Jquery check based on a checkbox

Hey, I'm having an issue with a condition. When I uncheck the checkbox, it doesn't uncheck. I've tried to make a block display, but the JavaScript isn't working. I attempted to add: document.getElementById("Reload").style.display = "b ...

"Utilizing the ng-class directive in AngularJS to apply multiple

I have been attempting to utilize the ng-class directive in AngularJS based on a certain condition: if a JSON value is 1, it should display as green; if it's 2, red; and if it's 0, white. However, I am unable to achieve the desired result for som ...

A step-by-step guide on crafting a triangular-shaped div connected to another div

After working with HTML elements and trying to incorporate 2 divs, I realized that I couldn't achieve the triangle-shaped div that I actually needed. I have included an image for reference. Below is the HTML code snippet: <div style="background-c ...

Positioning Images in Tailwind Modals

I'm currently working on building a modal using Tailwind in Vue, but I've run into some challenges with aligning the elements inside the modal as desired. I've experimented with removing certain Tailwind classes and have tried implementing ...

Exploring the varying treatment of the noscript tag across different web browsers

I've been searching everywhere, but I can't find any information on this topic. What I really want to understand is what browsers do with the content inside a noscript tag when JavaScript is enabled. Let's consider an example: According t ...

Tips for effectively placing a page scope block using BEM

Here is the current structure of my header. The page component is assumed to be the root. Currently, the geometry of the social-links block is being controlled by the header__social-links mix (positioned absolutely relative to the header). How can I prop ...

Ways to implement a delay in a function?

I'm looking for a way to introduce a delay in my function. Should I enclose the function within a delay function, or is there a different method to ensure that the animation doesn't trigger until 5 seconds after the page has loaded? var textTo ...

instructions on implementing Arial Black font style in LESS

Using less-1.7.0.min.js, I encountered an issue when setting Arial Black as the font. @fontFamily: Arial Black,Arial Black,Gadget,sans-serif; Upon generating the CSS, it resulted in: p, span { font-size: 14px; font-family: Arial #000000, Arial #000000, ...

The upper margin is ineffective

Apologies, I am new to CSS. Here is the HTML code I have: <div class="box-A" >Box A content goes here</div> <div class="box-B" >Box B content goes here</div> I attempted to apply the following CSS to it: .box-A{ background-c ...

multiple divisions in the center

I am struggling with centering 2 divs, each containing 3 nested divs wrapped around a big main div. The code I have written is as follows: <div stye="margin-left:auto; margin-right:auto; width: 1210px;"> <div id="left" style=" float:left; width ...

Is it possible to utilize the ternary operator to handle classnames when working with CSS modules?

I am experiencing difficulty implementing a styling feature using the ternary operator within an element's className. My goal is to have three buttons that render a specific component when clicked. Additionally, I want to change the background color ...

The hover feature on my website is making the picture flicker

I am experiencing an issue with a map on my website that contains four colored squares. When I hover over one of the squares, the image of the map changes to show the route corresponding to that color. However, this causes the image to shift position and i ...

The jQuery DataTable with footer is lacking responsiveness

Why is my table not responsive when I add a tfoot? I tried using the example found at https://datatables.net/examples/api/multi_filter.html Interestingly, when the footer contains only text, the table is responsive. However, if there's an input fie ...

The uncomplicated bar graph and its corresponding axis line up perfectly beside each other in an SVG diagram

I'm currently working on a stacked bar chart in my CodePen project found here: https://codepen.io/a166617/pen/qBXvzQd However, I am facing an issue where the graph is not aligned properly with the x-axis and y-axis lines. The barchart seems to be sli ...

Enhancing the functionality of angular-messages is impacting the overall design of

Displaying here is the current state of my login form: However, subsequent to upgrading angular-message to version 1.4 and higher, the layout transforms into this: Sharing my source code for reference: <ion-view view-title="Login"> <ion-hea ...

Creating HTML tabs using jQuery with input[type="radio"] tutorial for beginners

I'm having trouble creating a tab with input[type="radio"] using Jquery. The tab works fine on the first click, but it doesn't work on the second click. I can't figure out where the issue is in the Jquery code. Can someone assist m ...