Sticky top div with absolute positioning

后端 未结 6 1526
温柔的废话
温柔的废话 2020-12-30 00:48

I\'m using absolute positioning to have a div fill up the entire browser window. However, I wan\'t to combine this with a sticky div that sometimes is there and sometimes no

相关标签:
6条回答
  • 2020-12-30 01:17

    2018-6-18

    I choose the CSS way with position: sticky.

    that https://github.com/abouolia/sticky-sidebar .
    doesn't work for me (I am using Vue.js 2.0 SPA with vue-router & vuex)

    I also want the element position: absolute first,
    and then position: sticky

    Solution

    1. parent HTML element use position: absolute to have the right position.

    (don't forget to set height for parent. for example height:100%)

    1. child HTML element position: sticky

    work for me.

    0 讨论(0)
  • 2020-12-30 01:17

    Use float: left; and width: 0; and you can use transform: translateX(xxx); for set left position.

    Problem solved :)

    0 讨论(0)
  • 2020-12-30 01:26

    The alert scrolls with the content because it's inside the content-area which has overflow-y: auto.

    Move it out of the content-area (put it in between subheader and content-area), and remove the position: absolute (and top/left/right/bottom) attributes from the content. In that example I see no reason for content to be absolute-positioned, normal flow will put it where it wants to be.

    0 讨论(0)
  • 2020-12-30 01:29

    edit
    update with some enhancements
    http://jsfiddle.net/aDdRS/11/


    first post
    Why not scroll just the .content and not the .content-area

    http://jsfiddle.net/aDdRS/8/

    0 讨论(0)
  • 2020-12-30 01:34
    • Add fixed height & width 100% to alert + position:fixed
    • Add padding-top to content
    • Only downfall is of course the extra padding if there is no alert...

    See http://jsfiddle.net/aDdRS/5/

    0 讨论(0)
  • 2020-12-30 01:38

    I chose to go with a JavaScript approach after all. Would have preferred a pure CSS approach but my need for IE8 support stood in the way. This answer by Myles Gray is pretty much what I did - https://stackoverflow.com/a/4933509/940517

    0 讨论(0)
提交回复
热议问题