center aligning a fixed position div

前端 未结 13 646
走了就别回头了
走了就别回头了 2020-11-29 15:55

I\'m trying to get a div that has position:fixed center aligned on my page.

I\'ve always been able to do it with absolutely positioned divs using this \

13条回答
  •  隐瞒了意图╮
    2020-11-29 16:12

    You could use flexbox for this as well.

    .wrapper {
      position: fixed;
      top: 0;
      left: 0;
      height: 100%;
      width: 100%;
      
      /* this is what centers your element in the fixed wrapper*/
      display: flex;
      flex-flow: column nowrap;
      justify-content: center; /* aligns on vertical for column */
      align-items: center; /* aligns on horizontal for column */
      
      /* just for styling to see the limits */
      border: 2px dashed red;
      box-sizing: border-box;
    }
    
    .element {
      width: 200px;
      height: 80px;
    
      /* Just for styling */
      background-color: lightyellow;
      border: 2px dashed purple;
    }
    Your element

提交回复
热议问题