Summary on Positioning in CSS

Summary on Positioning in CSS

There are four types of positioning in CSS, which have different effects in different scenarios. Here I will briefly introduce these four types of positioning.

Static positioning: There are not many application scenarios, so we will not introduce them here.

The following mainly introduces three other common positioning

1. position: relative positioning: As the name implies, relative positioning is to make corresponding changes relative to the original position. It should be noted that the element will occupy the original position after moving (this is the most important point of relative positioning)

The following is the code demonstration part:

<style>
        * {
            margin: 0;
            padding: 0;
            /* Clear all margins and padding here,
            No real meaning, just for convenience of observation*/
        }
        
        .pink {
            width: 80px;
            height: 80px;
            background-color: pink;
        }
        
        .purple {
            width: 80px;
            height: 80px;
            background-color: purple;
        }
        
        .green {
            width: 80px;
            height: 80px;
            background-color: greenyellow
        }
    </style>

Here three divs are defined and given corresponding colors. The running results are as follows

insert image description here

When the child box is given the following positioning attributes

 .purple {
            position: relative;
            top: 0;
            left: 80px;
            /* Relative positioning is added to this box above,
            And move it 80px relative to its original self*/
            width: 80px;
            height: 80px;
            background-color: purple;
        } 

insert image description here

The page becomes like this, which also verifies the most important point - the element will occupy the original position after moving, otherwise the green box will be on top

2.Position:absolute absolute positioning: Absolute positioning is the position change made by its own parent element. If the parent element has a position attribute, the corresponding movement is made based on the parent element. If the parent element does not have a position attribute (or there is no parent element), the corresponding movement is made based on the browser. It should be noted that the element will not occupy the original position after moving.
The following is a code demonstration

.purple {
            position: absolute;
            top: 160;
            left: 80px;
            /* The purple box here has no parent element so it is positioned based on the browser*/
            width: 80px;
            height: 80px;
            background-color: purple;
        } 

insert image description here

Obviously the green box is pushed up, and this result verifies absolute positioning: the element will not occupy the original position after moving.

3. Fixed positioning: The position of fixed positioning is relative to the entire page, regardless of whether there is a parent element. Similarly, fixed positioning will not retain the position

Summarize

This concludes this article about the summary of positioning in CSS. For more content related to positioning in CSS, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope that everyone will support 123WORDPRESS.COM in the future!

<<:  Detailed explanation of MySQL multi-version concurrency control mechanism (MVCC) source code

>>:  Detailed explanation of Vue's ref attribute

Recommend

How to use fdisk to partition disk in Linux

Commonly used commands for Linux partitions: fdis...

Install mysql offline using rpm under centos 6.4

Use the rpm installation package to install mysql...

Detailed explanation of vue page state persistence

Table of contents Code: Replenish: Summarize Requ...

How to implement the @person function through Vue

This article uses vue, and adds mouse click event...

SQL implementation LeetCode (176. Second highest salary)

[LeetCode] 176. Second Highest Salary Write a SQL...

How to use indexes to optimize MySQL ORDER BY statements

Create table & create index create table tbl1...

Detailed explanation of the use of Vue3 state management

Table of contents background Provide / Inject Ext...

How to install elasticsearch and kibana in docker

Elasticsearch is very popular now, and many compa...

TypeScript enumeration basics and examples

Table of contents Preface What are enums in TypeS...

Install and configure ssh in CentOS7

1. Install openssh-server yum install -y openssl ...

How to configure user role permissions in Jenkins

Jenkins configuration of user role permissions re...

Unzipped version of MYSQL installation and encountered errors and solutions

1 Installation Download the corresponding unzippe...

Detailed example of IOS database upgrade data migration

Detailed example of IOS database upgrade data mig...