Detailed explanation of the usage of position attribute in HTML (four types)

Detailed explanation of the usage of position attribute in HTML (four types)

The four property values ​​of position are:

1.relative
2.absolute
3.fixed
4.static

These four attributes are described below.

<div id="parent">
     <div id="sub1">sub1</div>
     <div id="sub2">sub2</div>
</div>

1. relative

The relative property is relatively simple. We need to figure out which object it is offset relative to. The answer is its location itself. In the above code, sub1 and sub2 are siblings. If you set a relative attribute for sub1, for example, set the following CSS code:

#sub1
{
    position: relative;
    padding: 5px;
    top: 5px;
    left: 5px;
}

We can understand it this way: if the relative attribute is not set, the position of sub1 should be at a certain position according to the normal document flow. But when the position of sub1 is set to relative, it will be offset according to the values ​​of top, right, bottom, and left according to where it should be. The "relative" meaning of relative is reflected here.

For this, you just need to remember where sub1 should be if relative is not set, and once it is set, offset it according to where it should be.

The next question is, where is the location of sub2? The answer is that it is where it was originally, and its position will not change because sub1 has added the position attribute.

What will happen if the position of sub2 is also set to relative? At this time, it is still the same as sub1, and it is offset according to its original position.

Note that the relative offset is based on the top left side of the object's margin.

2. absolute

This attribute is always misleading. It is wrong to say that when the position attribute is set to absolute, it is always positioned according to the browser window. In fact, this is the characteristic of the fixed attribute.

When the position of sub1 is set to absolute, who is the object for offset? There are two situations here:

(1) When the parent object (or great-grandfather, as long as it is a parent object) of sub1 also sets the position attribute, and the position attribute value is absolute or relative, that is, it is not the default value, then sub1 is positioned according to this parent.

Note that although the object has been determined, there are some details that require your attention, that is, which anchor point of the parent should we use for positioning? If parent sets properties such as margin, border, padding, etc., then this anchor point will ignore padding and will be positioned from the beginning of padding (that is, only from the upper left corner of padding), that is, padding will be ignored, but margin and border will not be ignored.

The next question is, where is the location of sub2? Because when position is set to absolute, sub1 will overflow the normal document flow, just like it does not belong to the parent, it floats up. In DreamWeaver, it is called a "layer", which actually means the same thing. At this point, sub2 will obtain the position of sub1, and its document flow is no longer based on sub1, but starts directly from parent.

(2) If sub1 does not have a parent object with a position attribute, then the body will be used as the positioning object and the positioning will be performed according to the browser window. This is relatively easy to understand.

3. fixed

fixed is a special absolute, that is, fixed always takes body as the positioning object and is positioned according to the browser window. Even if you drag the scroll bar, its position will not change. Similar to background-attachment:fixed

Of course, there seems to be no support under Dreamweaver

4. static

The default value of position, when the position attribute is not set, is to arrange the elements according to the normal document flow.

Summarize

The above is the usage of position in HTML introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website!

<<: 

>>:  MySQL-8.0.26 Configuration Graphics Tutorial

Recommend

Example code for implementing photo stacking effect with CSS

Achieve results step 1. Initial index.html To bui...

mysql5.7 installation and configuration tutorial under Centos7.3

This article shares the MySQL 5.7 installation an...

Quick solution for forgetting MySQL8 password

Preface When we forget the MySQL database passwor...

Example analysis of mysql non-primary key self-increment usage

This article uses an example to illustrate the us...

How to use docker to deploy front-end applications

Docker is becoming more and more popular. It can ...

Examples of using the ES6 spread operator

Table of contents What are spread and rest operat...

MySQL 5.7.20 zip installation tutorial

MySQL 5.7.20 zip installation, the specific conte...

How to build Git service based on http protocol on VMware+centOS 8

Table of contents 1. Cause 2. Equipment Informati...

Nginx Location directive URI matching rules detailed summary

1. Introduction The location instruction is the c...

React implements a highly adaptive virtual list

Table of contents Before transformation: After tr...

Steps to deploy multiple tomcat services using DockerFile on Docker container

1. [admin@JD ~]$ cd opt #Enter opt in the root di...

Complete steps to implement face recognition login in Ubuntu

1. Install Howdy: howdy project address sudo add-...

Solution to the problem "Table mysql.plugin doesn't exist" when deploying MySQL

Today I deployed the free-installation version of...

mysql add, delete, modify and query basic statements

grammar Here is the generic SQL syntax for INSERT...