0

I am trying to add border on hover for an element. But, it feels like jumps a little bit inside.

Note: I know I can add outline to achieve this, but outline doesn't have border-radius.

How can I do this?

<!DOCTYPE html>
<html>
<head>
<style>
p{
width: 130px;
min-height: 30px;
padding: 10px;
border-radius: 12px;
outline: 1px solid green;
background: yellow;
}
p:hover{
border: 1px solid black;
}
</style>
</head>

<body>

<p style="color:red;">A red paragraph.</p>

</body>
</html>
amar
  • 443
  • 4
  • 15

2 Answers2

0

Give it a standard yellow border and change the color on hover.

p {
  width: 130px;
  min-height: 30px;
  padding: 10px;
  border-radius: 12px;
  background: yellow;
  border: 1px solid yellow;
}

p:hover {
  border: 1px solid black;
}
<p style="color:red;">A red paragraph.</p>
Gerard
  • 15,418
  • 5
  • 30
  • 52
0

Border does affect the width of the container, so if you don't want the jump, add a transparent border to start with of the same width and then change the color on hover.

<!DOCTYPE html>
<html>
<head>
<style>
p{
width: 130px;
min-height: 30px;
padding: 10px;
border-radius: 12px;
outline: 1px solid green;
background: yellow;
border: 1px solid transparent;
}
p:hover{
border: 1px solid black;
}
</style>
</head>

<body>

<p style="color:red;">A red paragraph.</p>

</body>
</html>
Amyth
  • 1,367
  • 1
  • 9
  • 15