eeeeeeve
V2EX  ›  CSS

absolute position 布局中,可否不通过 JS 做到让子元素在竖直方向上居中呢?

  •  
  •   eeeeeeve · Jun 24, 2015 · 4053 views
    This topic created in 3979 days ago, the information mentioned may be changed or developed.
    可以用css解决这个问题么,如何解决?
    lifang
        1
    lifang  
       Jun 24, 2015
    position:absolute;
    top:50%;
    left:50%;
    margin-top: -子元素一半高度;
    margin-left: -子元素一半宽度;


    这样是水平垂直方向都居中了
    eeeeeeve
        2
    eeeeeeve  
    OP
       Jun 24, 2015
    @lifang top:50%没有考虑子元素尺寸啊, 在css中是否可以把子元素的中线和页面中部重合呢?
    top 50% 子元素上部和页面中部重合,不是子元素中部和页面中部重合啊
    ThomasChan
        3
    ThomasChan  
       Jun 25, 2015
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2860 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 61ms · UTC 12:23 · PVG 20:23 · LAX 05:23 · JFK 08:23
    ♥ Do have faith in what you're doing.