首页 > 解决方案 > 将绝对定位的粘性元素与兄弟内部的 div 对齐?

问题描述

假设我有以下博客文章设置:

    <div>
        <div> //this div has some margin left & right, so the blog post is centered
            <div>Title</div>
            <div>Body</div>
        </div>

        <ContainerAbsolute> //inside the right margin, i absolutely position this container & the sticky side bar inside it.
            <StickySideBar />
        </ContainerAbsolute>
    </div>

我希望我的 StickySideBar 位于右侧并沿着博客文章向下。我用position:sticky它。我已将它放入一个容器中,该容器绝对定位,并延伸到整个页面 ( height: 100%)。现在可以了。但现在我希望我的 StickySideBar 被卡在我的 Text 开始的位置,而不是最上面。我可以做top: 500px类似的事情来近似这个,但我怎样才能将它与Bodydiv 完美对齐?

标签: javascriptcssreactjscss-selectors

解决方案


推荐阅读