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

后端 未结 18 1657
情话喂你
情话喂你 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条回答
  •  余生分开走
    2020-12-07 15:57

    Having used many of these solutions, I find using the trick of setting border-color: transparent to be the most flexible and widely-supported:

    .some-element {
        border: solid 1px transparent;
    }
    
    .some-element-selected {
        border: solid 1px black;
    }
    

    Why it's better:

    • No need to to hard-code the element's width
    • Great cross-browser support (only IE6 missed)
    • Unlike with outline, you can still specify, e.g., top and bottom borders separately
    • Unlike setting border color to be that of the background, you don't need to update this if you change the background, and it's compatible with non-solid colored backgrounds.

提交回复
热议问题