Is it possible to keep the width of the parent element when position: fixed is applied?

前端 未结 8 1865
没有蜡笔的小新
没有蜡笔的小新 2021-02-01 12:16

When we apply position:fixed to an element, it\'s taken out of the normal flow of the document, therefore it doesn\'t respect it\'s parent\'s element width. Are the

8条回答
  •  难免孤独
    2021-02-01 13:04

    This is likely because of some default margin or padding on the or element. When it's static, it sizes based on the 's width at the time, but when it changes to position:fixed it's sized in respect to the viewport.

    As such, removing that default margin/padding should fix the problem (in my experience body { margin:0; } fixes it) as should changing the sizing when it is fixed (like width:calc(n% - 5px);).

提交回复
热议问题