CSS: fixed to bottom and centered

前端 未结 9 1301
南笙
南笙 2020-11-28 04:33

I need my footer to be fixed to the bottom of the page and to center it. The contents of the footer may change at all time so I can\'t just center it via margin-left: xxpx;

9条回答
  •  北海茫月
    2020-11-28 05:10

    The problem lies in position: static. Static means don't do anyting at all with the position. position: absolute is what you want. Centering the element is still tricky though. The following should work:

    #whatever {
      position: absolute;
      bottom: 0px;
      margin-right: auto;
      margin-left: auto;
      left: 0px;
      right: 0px;
    }
    

    or

    #whatever {
      position: absolute;
      bottom: 0px;
      margin-right: auto;
      margin-left: auto;
      left: 50%;
      transform: translate(-50%, 0);
    }
    

    But I recommend the first method. I used centering techniques from this answer: How to center absolutely positioned element in div?

提交回复
热议问题