Home > Web Front-end > HTML Tutorial > Introduction and use of relative positioning and absolute positioning in css

Introduction and use of relative positioning and absolute positioning in css

零下一度
Release: 2017-07-20 13:21:43
Original
2174 people have browsed it

1. The four values ​​of position: static, relative, absolute, fixed.

Absolute positioning: absolute and fixed are collectively called absolute positioning

Relative positioning: relative

Default value: static

2. The difference between relative positioning and absolute positioning

Example:

HTML code:

css code:

1.position: relative; If an element is positioned relatively, first it will appear at its location. Then move the element "relative to" its original starting point by setting a vertical or horizontal position. (One more point, when relatively positioned, the element still occupies the original space regardless of whether it is moved. Therefore, moving the element will cause it to cover other boxes) For example, #mybox{position: relativ; left: 20px; top: 20px;} The effect is Make this layer countryside and move it 20px to the left.

2. Position: absolute; indicates absolute positioning. The position will be calculated based on 0 point in the upper left corner of the browser. Absolute positioning makes the element independent of the document flow, so it does not occupy space. Elements in normal document flow are laid out as if absolutely positioned elements were not present. It is positioned relative to the nearest positioned ancestor, or, if the element has no positioned ancestors, its position is relative to the earliest containing block. (Because absolutely positioned boxes are independent of document flow, they can cover other elements on the page and the stacking order of these boxes can be controlled by z-index. The higher the value of z-index, the further the box is positioned in the heap. High. )

Now most of the web pages we make are centered, so I need this element to follow the position of a certain element in the web page, regardless of the resolution, its position is always within the page. A certain element, then simply absolute will not work. The correct solution is to define the parent element of the element as position: relative; (this can be a grandparent, or position: absolute;) and the element that needs absolute positioning is set to position: absolute; and then set top, right , bottom, and left values ​​are sufficient, so that the reference standard for positioning is the upper left side of the parent's upper left corner padding.

The above is the detailed content of Introduction and use of relative positioning and absolute positioning in css. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template