JavaScript Variable height div undesirably changing size on hover because of border

There are two conditions:

  • The div (and the divs inside) must have a variable height.
  • The divs inside must add a border on hover over them.

There is a problem:

The border of the hover adds height to the div, therefore making undesirable height changes.

How to overcome this without changing the variable height to fixed height??

Answer:1

Try applying border-box to all the elements:

* {
    box-sizing:border-box;
}
Answer:2

Transition not the border itself but the border-color property from transparent to the desired value. The point is that the border is defined along with its border-width by default with border-color: transparent, so on hover only the changing colour produces the effect of the border appearing.

Answer:3

Use 2 divs. Place a new div in your current div and set the border to the new div and set its height to 100%.

Example: https://codepen.io/danieldilly/pen/brbzbZ

Answer:4

There are three ways you can do this.

  1. Leave it as is (No!)
  2. Create a transparent border that matches the :hover border.
  3. Use Use box-sizing

Examine the snippet below to see what works best for you.

div {
  width: 100px;
  height: 100px;
  background: black;
  margin: 1em;
  display: inline-block;
  color: white;
  text-align: center;
}

div:hover {
  border: 2px solid green;
}

.one {
  /* ...Nothing here...move along...*/
}

.two {
  border: 2px solid transparent
}

.three {
  box-sizing: border-box
}
<div class="one">default</div>
<div class="two">transparent</div>
<div class="three">box-sizing</div>
Answer:5

I'm trying to dynamically populate a ion-select dropdown with a json object. My html component looks like this: <ion-item [hidden]="editOptions.isEditing"> <ion-note stacked color="...

I'm trying to dynamically populate a ion-select dropdown with a json object. My html component looks like this: <ion-item [hidden]="editOptions.isEditing"> <ion-note stacked color="...

  1. ionic populate select
  2. ionic 3 populate select
  3. ionic 2 populate select

There is an imaginary square which I want to draw as two halves, i.e. two triangles. Whilst they should perfectly fit together and make a square, a tiny line caused by anti-aliasing appears. These ...

There is an imaginary square which I want to draw as two halves, i.e. two triangles. Whilst they should perfectly fit together and make a square, a tiny line caused by anti-aliasing appears. These ...

  1. html5 video anti aliasing
  2. canvas html5 anti aliasing
  3. html5 canvas no anti aliasing
  4. anti aliasing html5

imagine your wife asks you to go buy some Campion flowers. as it’s a rare kind of flower you’re not sure if you can find it the flower shop next door. so you’ll have to check every single flower shop ...

imagine your wife asks you to go buy some Campion flowers. as it’s a rare kind of flower you’re not sure if you can find it the flower shop next door. so you’ll have to check every single flower shop ...

  1. on higher order thinking skills
  2. on higher order derivatives
  3. questions about higher order thinking
  4. problems about higher order derivatives

I visit this site all the time to search for solutions, but I couldn't find an answer to my question. I want to add multiple audio files to a page. So I used the following coding. What this does is ...

I visit this site all the time to search for solutions, but I couldn't find an answer to my question. I want to add multiple audio files to a page. So I used the following coding. What this does is ...

  1. html multiple onclick actions
  2. html multiple onclick functions
  3. html multiple onclick
  4. html multiple onclick events
  5. html onclick multiple parameters
  6. html onclick multiple lines
  7. html onclick multiple function call
  8. html button multiple onclick events
  9. html select multiple onclick
  10. html button onclick multiple functions
  11. html button onclick multiple actions
  12. html input onclick multiple functions
  13. html button onclick multiple
  14. html javascript multiple functions onclick
  15. html onclick multiple statements
  16. html select multiple onclick deselect