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 use Flex layout to achieve scrolling of fixed content area in the head

The fixed layout of the page header was previousl...

Using puppeteer to implement webpage screenshot function on linux (centos)

You may encounter the following problems when ins...

Test and solution for MySQL's large memory usage and high CPU usage

After the changes: innodb_buffer_pool_size=576M -...

I have sorted out some domestic design websites that I think are good.

<br />I have compiled some domestic design w...

Illustration of the process of using FileZilla to connect to the FTP server

When I first started setting up an ftp server on ...

js implements a simple English-Chinese dictionary

This article shares the specific code of js to im...

Vue+node realizes audio recording and playback function

Result: The main part is to implement the code lo...

DIV background semi-transparent text non-translucent style

DIV background is semi-transparent, but the words ...

js to achieve simple product screening function

This article example shares the specific code of ...

VMware vSphere 6.7 (ESXI 6.7) graphic installation steps

Environment: VMware VCSA 6.7 (VMware-VCSA-all-6.7...

About the pitfalls of implementing specified encoding in MySQL

Written in front Environment: MySQL 5.7+, MySQL d...

Implementing search box function with search icon based on html css

Preface Let me share with you how to make a searc...

In html table, set different colors and widths for each cell

It is recommended that you do not set the width, h...

MySQL installation diagram summary

MySQL 5.5 installation and configuration method g...