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

CSS: Hover below the designated target to reveal an expanding dropdown menu

My initial solution involved toggling the visibility on and off when hovering. However, this approach is not optimal as the menu should smoothly transition into view. When the visibility is toggled, the user does not experience the intended transition effe ...

Troubleshooting problem with scrollbar in HTML table within a div element

I'm utilizing Bootstrap for the majority of my CSS. I've split a section into two columns, with the left column displaying a table with data and the right side featuring a Google map. The issue arises when the table row grows, as it doesn't ...

Controling attributes during the design process

Experimenting with a basic User Control in Visual Studio 2008: I've created a Panel called Wrapper with various controls inside. Will Visual Studio be able to handle this during the design phase? public partial class TestControl : System.Web.UI.UserC ...

Ways to eliminate extra space from the edges of a container

Trying to wrap my page in a Material UI container component, but there's this persistent whitespace on the outside that no matter how much I tweak the margin and padding, it just won't disappear. Any CSS wizard out there with a workaround to make ...

Error: Syntax error detected. Unexpected blank space found. Expecting either "-" symbol, identifier, or variable

Error: Syntax error: unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting '-' or identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in C:\xampp\htdocs\Source Code\displaysalesdetailed.php on line ...

CSS: Element with overflowing content fails to display even with the overflow set to visible

I'm trying to enhance a fixed toolbar by adding a dropdown menu, but the .toolbar-dropdown-menu component isn't displaying correctly, as shown in this screenshot (tested on Google Chrome 80.0): https://i.stack.imgur.com/r5Cz0.png Initially, it ...

Issue with bootstrap 4 CDN not functioning on Windows 7 operating system

No matter what I do, the CDN for Bootstrap 4 just won't cooperate with Windows 7. Oddly enough, it works perfectly fine on Windows 8. Here is the CDN link that I'm using: <!doctype html> <html lang="en> <head> <!-- Req ...

Firefox not rendering responsive YouTube embed properly

This method of embedding seems to be functioning well on all browsers except for Firefox; I took advantage of a free trial at crossbrowsertesting.com to verify. I’m not using a direct iFrame embed, and all the solutions I’ve come across are related to ...

When a td element is clicked, the Textbox will also be clicked

Similar Question: Dealing with jQuery on() when clicking a div but not its child $(oTrPlanning).prev().children('td').each(function () { this.onclick = setCountClick; }); When a TD element is clicked, the function setCountClick() i ...

Is there a way to effortlessly generate a mini-website from Markdown files?

Can a tool be found that creates a mini-website from locally-stored Markdown files complete with automatically generated navigation? I'm hoping for a service that can sync with my Dropbox, analyze the file structure, read the Markdown files, and effo ...

CSS animations can make the navigation bar vanish into thin air

I recently started implementing CSS3 animations from the Animate.css library and I must say, they really enhance the look and feel of my website. The animations pair perfectly with WOW.js to create stunning effects. However, I have encountered a minor iss ...

Converting Apache POI Word documents to clean HTML stripping out styles and superfluous tags

I am currently working on converting Word documents to clean HTML. I have been using Apache POI, but it seems to create messy output similar to MS Word's own HTML saving method. What I really need is a solution like the one offered by . For instance, ...

Having difficulty transforming both scale and box-shadow at the same time

Is there a way to animate the circle's box-shadow while also scaling it down from 1.6x to 1 during the same transition period? I'm having trouble with the timing of the scale animation, as it seems to occur after the box-shadow animation is fini ...

I've encountered an issue when attempting to use innerHTML for DOM manipulation

I've been attempting to remove a specific list item <li> from the inner HTML by assigning them proper IDs. However, I'm encountering difficulties in deleting it. How can I delete these list items after clicking on the cross button? Feel fr ...

How can you transfer data from a jQuery function to a designated div element?

I'm struggling to transfer data from a function to a specific div, but I can't seem to make it work. I'm in the process of creating a gallery viewer and all I want is to pass the counter variable, which I use to display images, and the total ...

Enhance your web design with the mesmerizing jQuery UI drop effect combined

I'm attempting to create an animated toggle effect on a box using jQuery UI's drop feature. However, I've encountered some trouble due to the box having box-sizing: border-box applied which is causing issues with the animation. During the a ...

Utilizing JavaScript for manipulating arrays and displaying images

After asking this question previously without a satisfactory solution, I am hoping to provide better clarification. Imagine having an array with 3 items and it lands on 0 - a code is set up to display this in a div. Now, I want the image to be shown righ ...

Dropdown menu will appear when you click on one of the menu items

Can someone help me create a dropdown menu using a JavaScript function that toggles on click? I've attempted to do so with the following code, but it's not working as expected. Could you please point out where I might be going wrong? I'm loo ...

What is the best way to smoothly move a fixed-size div from one container to another during a re-render process?

Introduction Anticipated change Similar, though not identical to my scenario: Situation 0: Imagine you have a container (chessboard with divs for game pieces) and a dead-pieces-view (container for defeated pieces). The positioning of these containers i ...

Locate the checkbox label using CSS

Is there a way to replace the standard checkboxes with font-awesome icons without modifying the generated HTML or using jQuery? EDIT: The code includes JavaScript that prevents me from making changes. Also, note that the label text class can be -1 or -2 a ...