Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

css hover creating border but pushing content

Tags:

css

layout

hover

Situation

I'm currently building a site and desire to have some elements create a border/outline upon hovering the mouse over them. This is simple enough to make work. For reference, please see the staging site at Stagin area link. I'm using the grid part of the latest bootstrap and the box-sizing model.

Issue

I find that upon hovering, the content below that which is being hovered gets "pushed" far down below the next element. Using the stagin area as reference, I can change the behaviour through CSS to fix this on the left hand side or the right hand side but, not both at the same time.

Code

Here is a snippet of the CSS I use to make the effect:

.hover-border:hover {
   border: 3px solid #3A3A3A;
   display: block;
}

Using this method, anything but the first element behaves as expected. If I try this next snippet, the first element works but, then the others break:

.hover-border:hover {
    border: 3px solid #3A3A3A;
    display: block;
    margin-top: -6px;
}

For the sake of clarification with regard to properties inherited, I have set the margin/padding on the elements in question to '0 !important' for standard behaviour until hover

Problem

How can I stop the element below from being pushed?

like image 421
Simon Avatar asked Sep 19 '13 05:09

Simon


People also ask

How do I apply a border to the element on a mouse hover without affecting the layout in CSS?

Answer: Use the negative CSS margin If you apply the border around an element on mouse hover it will move the surrounding elements from its original position, this is the default behavior.

How do you add a border on hover?

add margin:-1px; which reduces 1px to each side. or if you need only for side you can do margin-left:-1px etc. That was the best solution for me because, in my case, I set a 1px border to the orignal element and want to get, on hover, a thicker border (3px). Using margin: -2px; indeed works.

What is difference between outline and border?

Note: Outline differs from borders! Unlike border, the outline is drawn outside the element's border, and may overlap other content. Also, the outline is NOT a part of the element's dimensions; the element's total width and height is not affected by the width of the outline.

What is outline offset?

The outline-offset property adds space between the outline and the edge or border of an element. The space between an element and its outline is transparent. Outlines differ from borders in three ways: An outline is a line drawn around elements, outside the border edge. An outline does not take up space.


1 Answers

Personally - I go with something along the lines of:

.hover-border {
  border: 3px solid transparent;
  display: block;
}

.hover-border:hover {
  border: 3px solid #3A3A3A;
}
like image 57
Shylo Hana Avatar answered Sep 21 '22 14:09

Shylo Hana