CSS has two most important basic attributes that front-end developers must master: display
and position
.
display
The attribute specifies the layout of the web page. Two important layouts, flexible layoutflex
and grid layoutgrid
.
This article describes very useful position
properties. I hope that through 10 minutes of reading, it will help you to easily grasp the positioning of the webpage and explain how the browser calculates the position of the webpage elements, especially the newly introduced sticky
positioning.
First, the role of the position attribute
position
Attributes are used to specify the position of an element on a web page. There are five positioning methods, that is, the position
attribute has five main values.
static
relative
fixed
absolute
sticky
The five values are introduced in turn below. The last one sticky
is only supported by the browser in 2017, this article will focus on introduction.
Second, the static attribute value
static
Is position
the default value of the attribute. If the position
attribute is omitted , the browser considers the element to be static
positioned.
At this time, the browser will determine the position of each element in the order of the source code, which is called "normal flow" (normal flow).
Each block-level element occupies its own block, and there is no overlap between elements. This position is the default position of the element.
Note that the static
element position as a result of positioning, the browser is discretionary, so when top
, bottom
, left
, right
these four properties is invalid.
三、relative,absolute,fixed
relative
, absolute
, fixed
These three properties have one thing in common, are positioned with respect to a certain point, the only difference lies in the different basis points. Therefore, as long as you understand what their basic points are, it is easy to grasp the three attribute values.
None of these three positions will affect the position of other elements, so there may be overlap between elements.
3.1 relative attribute value
relative
Represents, with respect to a default position (i.e., static
position) is offset, i.e., the default position of the positioning element is a base point.
It must be used with top
, bottom
, left
, right
for use with these four attributes, to specify the direction and distance of the offset.
div { position: relative; top: 20px; }
In the above code, the div
element is offset downward from the default position 20px
(that is, from the top 20px
).
3.2 absolute property value
absolute
Indicates that the offset is relative to the parent element (usually the parent element), that is, the positioning base point is the parent element.
It has an important limitation: the positioning base (generally the parent element) cannot be the static
positioning, otherwise the positioning base will become the root element of the entire web page html
. In addition, absolute
the positioning must match top
, bottom
, left
, right
used with these four properties.
/* The HTML code is as follows <div id="father"> <div id="son"></div> </div> */ #father { positon: relative; } #son { position: absolute; top: 20px; }
In the above code, the parent element is relative
positioning, and the child element is absolute
positioning, so the positioning base point of the child element is the parent element, which is offset downward relative to the top of the parent element 20px
. If the parent element is static
positioned, the child element in the above example is offset downward from the top of the page 20px
.
Note that the absolute
positioned element will be ignored by the "normal page flow", that is, in the "normal page flow", the space occupied by the element is zero, and the surrounding elements are not affected.
3.3 fixed attribute value
fixed
Indicates that the offset is relative to the viewport (viewport, browser window), that is, the positioning base point is the browser window. This causes the position of the element to not change as the page scrolls, as if it were fixed on the page.
If it's with top
, bottom
, left
, right
use these four attributes together, it represents the initial position of the element is calculated based on the viewport, otherwise the default position is the initial position of the element.
div { position: fixed; top: 0; }
In the above code, the div
element is always at the top of the viewport and does not change as the page scrolls.
Four, sticky attribute value
sticky
With the first four values are not the same attribute, it will generate a dynamic effect, like relative
and fixed
combination: for some time is relative
positioned (self-positioning point is the default position), when the other becomes automatic fixed
positioning (positioning point is the viewport).
Therefore, it can form a "dynamic fixation" effect. For example, the search toolbar of a web page is initially at its default location ( relative
positioning) when it is loaded .
When the page scrolls down, the toolbar becomes a fixed position, always staying at the head of the page ( fixed
positioning).
When the page scrolls back up to its original position again, the toolbar will return to the default position.
sticky
The premise is to take effect, must be used with top
, bottom
, left
, right
together, these four attributes, can not be omitted, otherwise identical to the relative
position, does not produce "dynamic stabilization" effect. The reason is that these four attributes are used to define the "offset distance", which the browser considers as sticky
the effective threshold.
Its specific rule is that when the page scrolls and the parent element starts to leave the viewport (that is, partially invisible), as long as sticky
the distance to the element reaches the effective threshold, the relative
positioning automatically switches to fixed
positioning; when the parent element completely leaves the viewport (that is, completely Invisible), fixed
positioning automatically switches back to relative
positioning.
Please see the sample code below. (Note that all browsers except IE, which has been eliminated, are currently supported sticky
. However, the Safari browser needs to be prefixed with a browser -webkit-
.)
#toolbar { position : -webkit-sticky ; / * safari browser * / position : sticky ; / * other browsers * / top : 20px ; }
In the above code, when the page scrolls down, #toolbar
the parent element starts to leave the viewport. Once #toolbar
the distance between the top of the viewport and the 20px
threshold is less than (the threshold), #toolbar
it will automatically become fixed
positioned to maintain 20px
the distance from the top of the viewport .
The page continues to scroll down, the parent element completely leaves the viewport (that is, the entire parent element is completely invisible), and #toolbar
returns to the relative
positioning.
Five, sticky application
sticky
Positioning can achieve some very useful effects. In addition to the "dynamic fixation" effect mentioned above, here are two more.
5.1 Stacking effect
Stacking means that when the page scrolls, the elements below cover the elements above. The following is an example of a picture stack. The picture below will scroll with the page, covering the picture above (see demo ).
The HTML code is just a few pictures.
<div><img src="pic1.jpg"></div> <div><img src="pic2.jpg"></div> <div><img src="pic3.jpg"></div>
The CSS code is extremely simple, as long as two lines.
div { position: sticky; top: 0; }
Its principle is that when the page scrolls down, each picture will become a fixed
positioning, resulting in the next picture overlapping the previous picture. A detailed explanation can be found here .
5.2 Table header lock
When scrolling a large table, the table header is always fixed, and it can also be sticky
implemented (see demo ).
The CSS code is also very simple.
th { position: sticky; top: 0; }
It should be noted that it sticky
must be set on the <th>
element, not on the <thead>
and <tr>
element, because these two elements are not relative
positioned, so there is no sticky
effect. A detailed explanation can be found here .