Search code examples
javascripthtmlcsswebvs-web-site-project

How to fix div only inside my another div?


**Fix only with another div **

Here is the code that I used... HTML code,

   <div class="container">
        <div class="fix">
            Here fixed content
        </div>
   </div>

CSS code,

.container{
    display:flexbox;
    height: 40em;
    background-color: black;
}
.fix{
    position: fixed;
    width: 25em;
    height: 30em;
    background-color: beige;
}

Solution

  • Make the element's parent container have position: relative

    <div class="parent">
      <div class="child"></div>
    </div>
    
    .parent {
      position: relative;
    }
    .child {
      position: fixed;
      margin-top: 30px;
      margin-left: 30px;
    }