L
L
lavezzi12015-10-19 03:56:07
css
lavezzi1, 2015-10-19 03:56:07

Border adds width to element, how to avoid?

Hello!
There is a grid of blocks, on click I add a class with border: 2px to highlight the selected object. Naturally, the frame adds these two pixels from all sides and the selected object pushes nearby objects away, which makes the grid look crooked.
Are there solutions to fix it?

Answer the question

In order to leave comments, you need to log in

5 answer(s)
M
Mikhail, 2015-10-19
@lavezzi1

I usually used Box-shadow , and everything works without problems, the box-sizing property solves your problem, well then you need to initially set border: 2px solid transparent for all blocks, and already change the selected element to color, border-color: #333; for example, well, I would advise the shadow, there are no problems with the location on top, bottom or left or right, of course you decide.

A
Alexander Taratin, 2015-10-19
@Taraflex

box-sizing:border-box (+ don't forget the prefixes)

A
archelon, 2015-10-19
@archelon

use the css property box-sizing: border-box;
htmlbook.ru/css/box-sizing

V
Vladislav Startsev, 2015-10-19
@esvlad

Use outline A generic
property that simultaneously sets the color, style, and thickness of the outer border on all four sides of an element. Unlike the line specified through the border, the outline property does not affect the position of the box and its width.
more htmlbook.ru/css/outline

S
Stalker_RED, 2015-10-19
@Stalker_RED

Another option is to make a border for all blocks, and only change the color.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question