Absolute position but relative to parent-PHP Chinese Network Q&A
Absolute position but relative to parent
P粉395056196
P粉395056196 2023-10-11 12:04:14
0
2
579

I have two divs inside another div and I want to use css to position one child div to the top right corner of the parent div and the other child div to the bottom of the parent div. I.e. I want to use absolute positioning for the two child divs, but position them relative to the parent div instead of the page. How can I do this?

Example html:


P粉395056196
P粉395056196

reply all (2)
P粉509383150
div#father { position: relative; } div#son1 { position: absolute; /* put your coords here */ } div#son2 { position: absolute; /* put your coords here */ }
    P粉567281015
    #father { position: relative; } #son1 { position: absolute; top: 0; } #son2 { position: absolute; bottom: 0; }

    This is valid becauseposition:absolutemeans "usetop,right,bottom,leftPosition yourself relative to a nearest ancestor withposition:absoluteorposition:relative."

    So we let#fatherhaveposition:relativeand the children haveposition:absolute, and then usetop code> andbottomto locate subkeys.

      Latest Downloads
      More>
      Web Effects
      Website Source Code
      Website Materials
      Front End Template
      About us Disclaimer Sitemap
      php.cn:Public welfare online PHP training,Help PHP learners grow quickly!