Here are two distinct questions to consider:
- Understanding the behavior of
position: fixed;
.
- Determining the placement of absolute and fixed positioned items when no top/left/bottom/right coordinates are specified.
Your explanation of position: fixed;
is accurate — the fixed object is removed from the page flow and affixed to the window border. When you provide it with starting coordinates using left/right/top/bottom, they are calculated relative to the window object.
But what happens if you apply position: fixed;
without specifying coordinates or only providing coordinates in one dimension?
In such cases, the object's initial position is calculated based on its location prior to being removed from the page flow.
Let's explore some simple examples involving position: absolute;
(which behaves similarly to fixed in this context).
You can find the jsFiddle examples here.
Example with position: static;
:
`<div>First sentence. <mark>Second sentence.</mark> third sentence. </div>`
In this example, the second sentence remains within the page flow, positioning itself between the first and third sentences.
Example with position: absolute;
:
<div>First sentence. <mark style="position: absolute;">Second sentence.</mark> third sentence.</div>
Here, the second sentence is taken out of the page flow without specific coordinates. It is placed after the first sentence (where it was before removal), causing the third sentence to move underneath due to the change in flow.
Example with position: absolute;
and; left: 0;`:
<div>First sentence. <mark style="position: absolute; left: 0;">Second sentence.</mark> third sentence.</div>
By adding a starting point with left: 0;
for the second sentence, it not only occupies no space but also shifts to the left side of the container while maintaining its vertical position. (No top
or right
coordinates were added.)
position: fixed;
operates similarly. In the original post, the author only fixed the top coordinate, leaving the object horizontally aligned with its original location in the page flow.