The difference between layout and positioning: Layout is on the screen plane, positioning is perpendicular to the screen.

  • The hierarchy of a DIV

As you can see from the image, the floating element is floating out of the document flow, just a little bit. The text content is covered according to the order in which it appears.

  • Position attribute
  1. Static defaults to stay in the document stream.
  2. Relative Refers to a position that rises but does not depart from the document flow.
  3. Absolut is the non-static element of the ancestor.
  4. Fixed Indicates the fixed location. The positioning base is viewport.
  5. Sticky sticky positioning.
  • Some lessons:
  1. If you write absolute, you have to write relative.
  2. If you write absolute or fixed, be sure to fill in top and left.
  3. Sticky has poor compatibility.

1. position: relative

Usage Scenarios:

  1. It’s used for displacement.
  2. Used as a reference element for absolute.
  3. Used with z-index: The default z-index value is auto. A larger value indicates a higher layer.

2. position: absolute

Usage Scenarios:

  1. Move away from the original position and create another layer, such as the close button of a dialog box
  2. Mouse prompt (as shown in figure: when the mouse hover button appears prompt message)
  3. This parameter is used with z-index.

3. position: fixed

Usage Scenarios:

  1. Annoying ads
  2. Back to top button
  3. This parameter is used with z-index.

— — — — — — — — — — — — — — — — — — — –

When an element adds a non-static positioning attribute, its level is higher than other elements. The schematic diagram is as follows:

  • Cascading context

Metaphor: Each cascading context is a new small world (scope). The Z-index in this small world has nothing to do with the outside world. Only the Z-index in the same small world can be compared. Negative Z-index can’t escape the small world.

What non-orthogonal attributes can create a cascading context? Answer: developer.mozilla.org/zh-CN/docs/…

Z-index, flex, opacity and Transform are required to be memorized.