home page Site building tutorial web front end text

How to position: fixed in css to locate relative to the parent

In project development, we often use the position: fixed attribute, which is often applied when the navigation bar is fixed to the top when the scroll bar is pulled down, and the advertisement is fixed on both sides of the page or in the middle of the browser. When we use the position: fixed positioning attribute, it generates absolute positioning elements, which are positioned relative to the browser window, so we only need to define top, The bottom, left, and right attributes are OK, but how do we implement parent positioning. To achieve considerable

We often use it in project development position:fixed Property. It is often used to fix the navigation bar to the top when the scroll bar is pulled down, and to fix advertisements on both sides of the page or browser middle. When we use position: Fixed positioning Property, it generates Absolute positioning The element of is positioned relative to the browser window, so you only need to define top, The bottom, left, and right attributes are OK, but how do we implement parent positioning.

To achieve the equivalent of parent element positioning, you can do the following:
Do not set the top of the fixed element, bottom,left,right, Only set the margin to realize its offset position. In essence, the fixed element is equivalent to the window positioning, and the implementation effect is relative to the parent element positioning.
The codes are as follows:

 <! DOCTYPE  html > <html>     <head>         <meta charset="utf-8"> <title>How position: fixed can be positioned relative to the parent in css</title>         <style>             body{ margin: 60px;}             .parent{                  width: 200px;                   height: 300px;                 background: #66a2ff;                   position: relative;             }             .child{ position: fixed;                 width: 100px;                 height: 300px;                 background: #ffd266;                 margin-left: 210px;/* Left offset of parent 210px*/             }         </style>     </head>     <body>         <div class="parent">           <div class="child"></div>         </div>     </body> </html>
Reward
poster

Statement: Some of the resources on this site are original works on the site, and some are publicly shared and collated based on the Internet. The copyright belongs to the original author.
If it infringes your rights, please contact our website, and we will deal with it as soon as possible. Thank you. Please indicate the source of the transfer

Link to this article: https://www.umtheme.com/web/220.html

Related recommendations

 CSS cardioid winding rotation effect

CSS cardioid winding rotation effect

CSS heart-shaped winding and rotating effect is an interesting animation effect, which can bring romantic and dynamic atmosphere to web pages. This effect will rotate a heart-shaped pattern infinitely, and twist around a central point at the same time
web front end 2023.09.13 zero two thousand three hundred and forty-eight
 Create css3 animation effects commonly used in h5

Create css3 animation effects commonly used in h5

Css3 provides many powerful special effects that can be used to achieve a variety of effects. Here are some common css3 effects that I summarize, which can be directly referenced if necessary: 1. Zoom in when levitating:. one {transition: All 0.4s&nb
web front end 2023.09.13 zero two thousand two hundred and one
Post comments

Thank you for your support