A brief discussion on how to set CSS position absolute relative to the parent element

A brief discussion on how to set CSS position absolute relative to the parent element

As we all know, the CSS position absolute is set according to the document by default. For example, if you set left:0 and top:0 after position:absolute, the element will be displayed in the upper left corner of the page.

Sometimes we need to set a relative absolute position within the container of the parent element

To achieve this, you need to set the position attribute of the parent element to relative. After setting it to relative, do not set the left and top attributes. At this time, although the parent element is relative, it still remains in the original position. Then set the position of the child element to absolute, and set its left, top, right, and bottom attributes, so that it is the absolute position relative to the parent element.

The following is the HTML sample code:

<!doctype html>
<html>
    <style type="text/css">
    #father {
       position: relative;
       width:600px;
       margin:auto;
       height:400px;
       border:1px solid red;
    }

    #son1 {
       position: absolute;
       top: 0;
       background:#f0f0f0;
    }

    #son2 {
       position: absolute;
       bottom: 0;
       background:blue;
    }
    </style>
    <body>
    <div id="father">
        <div id="son1">I am son1</div>
        <div id="son2">I am son2</div>
    </div>
    </body>
</html>

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

<<:  Create a screen recording function with JS

>>:  Detailed explanation of transaction isolation levels in MySql study notes

Recommend

How to implement web stress testing through Apache Bench

1. Introduction to Apache Bench ApacheBench is a ...

MySQL 8.0.17 installation and simple configuration tutorial under macOS

If you don’t understand what I wrote, there may b...

Vue.js manages the encapsulation of background table components

Table of contents Problem Analysis Why encapsulat...

Detailed graphic tutorial on how to enable remote secure access with Docker

1. Edit the docker.service file vi /usr/lib/syste...

A brief analysis of SQL examples for finding uncommitted transactions in MySQL

A long time ago, I summarized a blog post titled ...

Summary of 6 Linux log viewing methods

As a backend programmer, you deal with Linux in m...

MySql8 WITH RECURSIVE recursive query parent-child collection method

background When developing a feature similar to c...

Use of MySQL query rewrite plugin

Query Rewrite Plugin As of MySQL 5.7.6, MySQL Ser...

Build a server virtual machine in VMware Workstation Pro (graphic tutorial)

The VMware Workstation Pro version I use is: 1. F...

View the frequently used SQL statements in MySQL (detailed explanation)

#mysql -uroot -p Enter password mysql> show fu...

Example of how to optimize MySQL insert performance

MySQL Performance Optimization MySQL performance ...

Node.js+express message board function implementation example

Table of contents Message Board Required librarie...

Use pictures to realize personalized underline of hyperlinks

Don't be surprised if you see some kind of und...