How to give border to any element using css without adding border-width to the whole width of element?

前端 未结 11 1902
礼貌的吻别
礼貌的吻别 2020-12-02 10:19

How to give border to any element using css without adding border-width to the whole width of element?

Like in Photoshop we can give stroke- Inside , center and out

11条回答
  •  醉酒成梦
    2020-12-02 10:55

    Use box-sizing: border-box in order to create a border INSIDE a div without modifying div width.

    Use outline to create a border OUTSIDE a div without modifying div width.

    Here an example: https://jsfiddle.net/4000cae9/1/

    Notes: border-box currently it is not supported by IE

    Support:

    http://caniuse.com/#feat=outline

    http://caniuse.com/#search=border-box

    #test, #test2 {
        width: 100px;
        height:100px;
        background-color:yellow;
    }
    #test {
        -webkit-box-sizing: border-box;
        -moz-box-sizing: border-box;
        box-sizing: border-box;
        border: 10px dashed blue;
    }
    #test2 {
        outline: 10px dashed red;
    }
    
    
    

    Use box-sizing: border-box to create a border INSIDE a div without modifying div width.

    border-box

    Use outline to create a border OUTSIDE a div without modifying div width.

    outline

提交回复
热议问题