Home  >  Article  >  Web Front-end  >  Detailed explanation of relative positioning, absolute positioning and fixed positioning

Detailed explanation of relative positioning, absolute positioning and fixed positioning

零下一度
零下一度Original
2017-07-02 09:40:116404browse

The position of the element in the document flow is determined by the position of the element in (X)HTML. This is the most primitive ordinary flow. The float mentioned earlierCSS Study Notes 08 FloatYou can change the position of the element in the document flow. In addition to this, we can also re-determine the position of the element in the document flow by using the position property of CSS.

position attribute value

  • static: The default document flow layout method. Block-level elements generate a rectangular box as part of the document flow. Inline elements will Creates one or more line boxes, placed within their parent element. (Ignore top, bottom, left, right or z-index declarations).

  • relative: Offset relative to the original position. The completion process is to first generate an element in static (float) mode. Relative to the previous position, the movement direction and amplitude are determined by left , right, top, bottom attributes are determined, the space it originally occupied is still retained.

  • absolute: The element box is completely deleted from the document flow, and the space originally occupied by the element in the normal document flow will be closed,As if the element does not exist originally, positioning is performed according to the parent container (must be a non-static positioned container). The element generates a block-level box after positioning, regardless of what type of box it originally generated in the normal flow.

  • fixed: Fixed at a certain location in the browser and will not change when the browser scrolls.

static is the default layout method and will not be introduced here.

relative Relative positioning

 1  2  3  4      5     CSS相对定位 6  7     10 
11 12     
box1
13     
box2
14     
box3
15 16 

At this time, box1 and box2 are positioned on the page according to the static layout method. Now box2 Relative positioning

#The result is as follows, the position of box2 is offset (this offset is relative to the original position of box2), and does not affect box1 Position with box3

Note that when using relative positioning, the element still occupies the original space regardless of whether it is moved or not. Therefore, moving an element causes it to cover other boxes.

absolute Absolute positioning

Absolute positioning makes the position of the element independent of the document flow, so it does not occupy space. This is different from relative positioning, which is actually considered part of the normal flow positioning model because the element's position is relative to its position in the normal flow.

 1  2  3  4      5     CSS绝对定位 6  7     11 
12 13     
14         parent15         
16             child17         
18     
19 20 

Now also add a positioning attribute to parent

The effect is as follows, you can see The position of the child has changed

From the above phenomenon, one thing can be summarized: the position of an absolutely positioned element is relative to the nearest positioned ancestor If the element has no positioned ancestors, its position is relative to the original containing block (in this case, the body element and parent).

To use absolute positioning, there must be two conditions
 1. The parent must be To add positioning attributes to an element, it is generally recommended to use position:relative
2. Add absolute positioning to the child element position:absolute, and also add direction attributes (referring to left, right, top , bottom attribute)

fixed fixed positioning

is similar to the absolute positioning type, but its relative movement coordinates are the view (web page window within the screen) itself. Since the view itself is fixed, it will not change as the scroll bar of the browser window scrolls, unless you move the screen position of the browser window on the screen, or change the display size of the browser window, so fixedly positioned elements will always be in A position within the view within the browser window that is not affected by the flow of the document.

 1  2  3  4      5     CSS固定定位 6      9 
10 11     
回到顶部
    12     

段落1

13     

段落2

14     

段落3

15     ...16     

段落49

17     

段落50

18 19 

The effect is as follows. The div back to the top is always in the same position, and the on the blog page is also fixed positioning.

Summary

Absolute positioning is based on the parent element as the reference point. It will break away from the document flow and does not occupy the original position space.
Relative positioning is based on itself as the reference point, leaving the original position, but it will still occupy the original position space
Fixed positioning is based on the browser window as the reference point, it is always in the same position and will not move

The above is the detailed content of Detailed explanation of relative positioning, absolute positioning and fixed positioning. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn