代码之家  ›  专栏  ›  技术社区  ›  Francesco

绝对div中心和响应?[副本]

css
  •  0
  • Francesco  · 技术社区  · 8 年前

    我有一个弹出div,需要居中 (流体),但我使用的是公共代码,左边是50%,左边是负数

    width: 960px;
    margin-left: -480px;    
    position: absolute;
    top: 20px;
    left: 50%;
    z-index: 999;
    

    但是我能让它有反应吗,比如说如果我想

    max-width: 960px 
    

    而不是绝对值?

    使现代化 :可能我不清楚,但我看不出与“在div中垂直和水平居中文本”问题有任何关系

    3 回复  |  直到 8 年前
        1
  •  4
  •   will    8 年前

    在绝对元素上,可以使用

    left: 0
    right: 0
    margin: 0 auto
    

        2
  •  -1
  •   justDan    8 年前

    .popupContainer{
      width:100%;
      height:100%;
      position:absolute;
      left:0;
      top:0;
      display:flex;
      align-items:center;
      justify-content:center;
      z-index:999;
    }
    .popup{
      background-color:#ccc;
      width:300px;
      height:300px;
    }
    <div class="popupContainer">
      <div class="popup">
    
      </div>
    </div>

    你可以利用 flexbox 对于此代码笔: https://codepen.io/andrasadam93/pen/YrrORq

    .popupContainer{
      width:100%;
      height:100%;
      position:absolute;
      left:0;
      top:0;
      display:flex;
      align-items:center;
      justify-content:center;
      z-index:999;
    }
    .popup{
      background-color:#ccc;
      width:300px;
      height:300px;
    }
    
    <div class="popupContainer">
      <div class="popup">
    
      </div>
    </div>
    
        3
  •  -1
  •   Luís P. A.    8 年前

    使用变换:平移

    CSS

    width: 960px;  
    position: absolute;
    top: 20px;
    left: 50%;
    transform:translateX{-50%}
    z-index: 999