When 1 px border is added to div, Div size increases, Don't want to do that

后端 未结 18 1620
情话喂你
情话喂你 2020-12-07 15:09

On click I am adding, 1px border to div, so Div size increases by 2px X 2px. I dont want to get div size increased. Is there any simple way to do so?

18条回答
  •  萌比男神i
    2020-12-07 15:55

    Sometimes you don't want height or width to be affected without explicitly setting either. In that case, I find it helpful to use pseudo elements.

    .border-me {
        position: relative;
    }
    
    .border-me::after {
        content: "";
        position: absolute;
        width: 100%;
        height: 100%;
        top: 0;
        left: 0;
        border: solid 1px black;
    }
    

    You can also do a lot more with the pseudo element so this is a pretty powerful pattern.

提交回复
热议问题