0

Remove right and left border of selected box with css without javascript is that possible ? min 2 boxes, max 5 or more boxes

for example;

I have 2 boxes. Only first box has right border. I want to do; whichever is selected or hover, border must disappeared. look my image example image

  • previous element can't be selected with CSS - following sibilings can be... – kukkuz Feb 03 '17 at 11:13
  • I would suggest you to search for `CSS previous sibling selector`. There's no real way to do with CSS but there's some tricks out there that you may adapt to your own code. – user7393973 Feb 03 '17 at 11:13
  • Yes as a trick I currently use flex with order.. But I wondered if there could be a better way.. thanks –  Feb 03 '17 at 11:16

1 Answers1

0

You can play with borders of hover'ed boxes. Idea is when you hover on any box, you do:

  1. Left border color set the same as the background color: border-left: 1px solid orange;
  2. set negative left margin equal to border width, that makes box to shift left over previous element and hide previous border: margin-left: -1px;
  3. To prevent all the next boxes a notable shift, you need to increase active box width to its width + border width: width: calc(20% + 1px);
  4. If padding is set inside the box, you need to add border width to left padding value.

Here is a solution (suggest a border-right width = 1px):

.cont {
  width: 100%;
  height: 100px;
}
.box {
  width: 20%;
  height: 100px;
  float: left;
  background: orange;
  display: inline-block;
  box-sizing: border-box;
}
.box:not(:last-child) {
  border-right: 1px solid black;
}
.box:not(:first-child):hover {
  border-left: 1px solid orange;
  box-sizing: padding-box;
  margin-left: -1px;
  width: calc(20% + 1px);
}
.box:hover {
  background: lightblue;
  border-right: 1px solid lightblue;
}
<div class="cont">
<div class="box">box1</div>
<div class="box">box2</div>
<div class="box">box3</div>
<div class="box">box4</div>
<div class="box">box5</div>
</div>
Banzay
  • 9,310
  • 2
  • 27
  • 46