CSS Fixed position with Auto Margin

后端 未结 3 1054
感动是毒
感动是毒 2020-12-05 14:00

I want a component that keeps horizontal center of the page (two-columns), and I have a sub-component (right column) that I want its position to be fixed, so the sub-compone

3条回答
  •  情话喂你
    2020-12-05 14:59

    You can use margin: 0 auto with position: fixed if you set left and right.

    .wrapper {
        position:fixed;
        top: 0;
        left: 0;
        right: 0;
        width: 500px;
        margin: 0 auto;
    }
    

    This also works with position: absolute; and vertically.

    Demo: http://codepen.io/pstenstrm/pen/myaWVJ

提交回复
热议问题