How can I make the element stay in its place with CSS when hovering

user11077805

I have an element and when I want to hover over it. It shows the border as I want, but, what it does is that it scales the text and actually moves it...

Like so:-

BEFORE TRANSITION

Before the Transition

AFTER TRANSITION

enter image description here

As you can see in the images the "About Us" moved left a bit after hovering and that's because of the border. How can I make it stay in its place? This is my code

&--dropdown {
      list-style: none;
      position: absolute;
      width: 25rem;
      height: 14rem;
      background-color: $color-additional;
      overflow: hidden;

      &-item {
        position: relative;
        width: 34rem;
        padding: 1rem 0 1rem 2rem;
        transition: all ease 0.2s;
        &:not(:first-of-type) {
          border-top: 1px solid $color-navigation-border;
        }
        &:hover {
          background-color: $color-white;
          border-left: 5px solid $color-primary;
        }
        &-link {
          color: $color-black;
          text-decoration: none;
        }
      }
    }
Aaron3219

Don't use an outline for this. Instead, lower the left padding by the amount the border adds.

li {
  width: 300px;
  padding: 20px 30px;
  list-style: none;
  background: yellow;
  border-bottom: 1px solid black;
}

li:hover {
  border-left: 5px solid red;
}
<li>Test</li>
<li>Test2</li>

This is how you have it right now. The padding-left is 30px and on :hover the border-left adds 5px. So just subtract the border-left width from the padding-left width:

li {
  width: 300px;
  padding: 20px 30px;
  list-style: none;
  background: yellow;
  border-bottom: 1px solid black;
}

li:hover {
  border-left: 5px solid red;
  padding-left: 25px;
}
<li>Test</li>
<li>Test2</li>

Edit:

Since you provided some code now, just change it to this:

&--dropdown {
      list-style: none;
      position: absolute;
      width: 25rem;
      height: 14rem;
      background-color: $color-additional;
      overflow: hidden;

      &-item {
        position: relative;
        width: 34rem;
        padding: 1rem 0 1rem 2rem;
        transition: all ease 0.2s;
        &:not(:first-of-type) {
          border-top: 1px solid $color-navigation-border;
        }
        &:hover {
          background-color: $color-white;
          border-left: 5px solid $color-primary;
          padding-left: calc(2rem - 5px); //<-- I added this line of code
        }
        &-link {
          color: $color-black;
          text-decoration: none;
        }
      }
    }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to make element stay in the same position using css?

How do I make children elements stay within the parent element when view height/width is changed?

How do I make an image fade out but stay in place on scroll?

How can I make these images stay in a "grid"?

How can I make an image enlarge on a javascript event when its size is set through css?

How can I avoid CSS flickering when hovering over areas?

How can I make a <p> element display on the hover of its parent <div> using CSS?

How can I make it so the dropdown menu stays there when I'm hovering on it?

How can I get an element to stay within the browser's window width when its content is wider?

How can I make text appear when hovering a picture that has already the hovering for transform?

How can I make windows stay opaque when inactive?

How can I make the input stay as it is in the hover mode, when it contains any text?

How to effectively make div scrollable on hover only, and have it stay in that position when not hovering?

How can i force cursor to stay in place on window when dragging an Undecorated Stage

How can I make a favicon stay static?

How can I make the top and bottom elements of my view controller stay in place, while the middle has elements to vertically scroll through?

How to make elements stay in place when using transitions on hover

How can I make a left side popup box when hovering element in HTML?

How can I make an element place itself, depending on its value, at the top of a bar?

How can I get make element visible by hovering other element

How do i make <label> with <input> and its text stay active when i click on it with

how to make an element stay in place when position changes from absolute to fixed

How can I make the text invisible when hovering over a tex and pressing the mouse button?

How can I make a flex element be the width of its content?

How to make elements to stay under a sticky element css?

How can I make a element show when hovering over another element, and hide it if not. And make it take up space the entire time

Draggable HTML div doesn't stay in place when zooming in/out. How can I fix this?

How can I make the element appearing when checkbox is checked when this element is outside its tag with CSS ? Symfony

How can I change one svg path ID element when hovering over another element