Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add border to image on hover with padding

Tags:

html

css

image

I just want to add a border for the image with padding also I need transition effect. It's working fine but has a few problems :

  1. it shows little movements in the image on hover(not fixed)
  2. Transitions not smooth.

I tried everything.

I applied box-sizing:border-box; and gave the image a margin of 2px and removed it on hover but still no luck.

See this example. It's perfectly fine and the image is not moving on hover.

Here is my code :

.home-item1 {
  height: 107px;
  width: 108px;
  cursor: pointer;
  box-sizing: border-box;
}

.home-item1 img {
  border-radius: 50%;
  margin: 2px;
  transition: 0.2s ease-in-out;
}

.home-item1 img:hover {
  border: 2px solid red;
  margin: 0px;
  padding: 2px;
}
<div class="home-item1">
  <img src="http://i64.tinypic.com/2s0ftrc.png" alt="">
</div>

What am I missing? Please check the fiddle and let me know.

jsfiddle

like image 965
Janath Avatar asked Jul 05 '18 06:07

Janath


People also ask

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.

Can you give a border padding?

Padding, margin and border are all parts of elements, you can't give a border padding or a margin a border.

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.

Can you add padding to a border CSS?

The CSS padding properties are used to generate space around an element's content, inside of any defined borders. With CSS, you have full control over the padding. There are properties for setting the padding for each side of an element (top, right, bottom, and left).


2 Answers

This will work for you:

I have just added border: 4px solid transparent; and removed the margin and compensated it with the border and on hover reset it. Fiddle

.home-item1 {
  height: 107px;
  width: 108px;
  cursor: pointer;
  box-sizing: border-box;
}

.home-item1 img{
  border: 4px solid transparent;
  border-radius: 50%;
  padding: 0px;
  transition:all 0.2s ease-in-out;
}

.home-item1 img:hover{
 border: 2px solid red;
 margin: 0px;
 padding: 2px;
}
<div class="home-item1">
  <img src="http://lorempixel.com/110/110/" alt="">
</div>

Hope this was helpfull.

like image 127
Jithin Raj P R Avatar answered Oct 08 '22 16:10

Jithin Raj P R


  • First, You need to add transparent border to image so that it will not move when hovered.
  • Second, Increase the duration of transition to get smooth effect

.home-item1 {
  height: 107px;
  width: 108px;
  cursor: pointer;
  box-sizing: border-box;
}

.home-item1 img{
  border-radius: 50%;
  margin: 2px;
  transition: border 0.5s ease-in-out;
  border: 2px solid transparent; /* Added */
}

.home-item1 img:hover{
border: 2px solid red;
 margin: 0px; padding: 2px; 
}
<div class="home-item1">
  <img src="http://via.placeholder.com/350x150" alt="">
</div>
like image 28
Zuber Avatar answered Oct 08 '22 14:10

Zuber