CSS Why left rule is given precedence over right rule when there’s a width?

后端 未结 5 589
生来不讨喜
生来不讨喜 2020-12-21 11:52

I have this CSS code:

#div1{
 height:200px;
 width:200px;
 background-color:red;
 position:absolute;
 right:30px !important; 
 left:0px;
 }

5条回答
  •  爱一瞬间的悲伤
    2020-12-21 12:40

    Just to show that the browser is w3c compliant:

    If the values are over-constrained, ignore the value for ‘left’ (in case the ‘direction’ property of the containing block is ‘rtl’) or ‘right’ (in case ‘direction’ is ‘ltr’) and solve for that value.

    So, if we set direction right to left

    body  {
        direction: rtl;
    }
    
    #div1{
        height:200px;
        width:200px;
        background-color:red;
        position:absolute;
        right:30px; 
        left:0px;
    }
    

    Now left is ignored:

    fiddle

提交回复
热议问题