Center inline-block div in parent while ignoring floating elements

后端 未结 2 1153
小鲜肉
小鲜肉 2021-02-01 15:31

I am trying to center an inline div in its parent. The parent element also has a child div that floats to the right. Because of the right aligned

相关标签:
2条回答
  • 2021-02-01 15:54

    position:absolute is the only way

    DEMO http://jsfiddle.net/kevinPHPkevin/u4FWr/1/

    .center {
        display:inline-block;
        position: absolute;
        left:0;
        right:0;
    }
    

    EDITED

    not sure if this has already been suggested

    Or you can absolute: position; the right div instead

    DEMO http://jsfiddle.net/kevinPHPkevin/u4FWr/3/

    .right {
        display:inline-block;
        position: absolute;
        right:0;
    }
    
    0 讨论(0)
  • 2021-02-01 16:02

    set the right div css

    position:absolute;
    right:0;
    

    relative to the parent div

    #parent {
    position:relative;
    }
    
    0 讨论(0)
提交回复
热议问题