Fixed position but relative to container

后端 未结 25 1668
独厮守ぢ
独厮守ぢ 2020-11-21 23:52

I am trying to fix a div so it always sticks to the top of the screen, using:

position: fixed;
top: 0px;
right: 0px;

However,

25条回答
  •  闹比i
    闹比i (楼主)
    2020-11-22 00:45

    The answer is yes, as long as you don't set left: 0 or right: 0 after you set the div position to fixed.

    http://jsfiddle.net/T2PL5/85/

    Checkout the sidebar div. It is fixed, but related to the parent, not to the window view point.

    body {
      background: #ccc;
    }
    
    .wrapper {
      margin: 0 auto;
      height: 1400px;
      width: 650px;
      background: green;
    }
    
    .sidebar {
      background-color: #ffffd;
      float: left;
      width: 300px;
      height: 100px;
      position: fixed;
    }
    
    .main {
      float: right;
      background-color: yellow;
      width: 300px;
      height: 1400px;
    }
    wrapper
    main

提交回复
热议问题