Interesting behavior with border-radius and background-color
I have the following html:
<div class="block">
<div class="button">
hover me, baby!
</div>
</div>
and CSS:
.block {
background-color: red;
border-radius: 8px;
width: 120px;
text-align: center;
}
.button {
width: 100%;
border-radius: inherit;
}
.button:hover {
background-color: white;
}
when i hover over element i see this:
result
is that the expected behavior? And please help me how to prevent this. I dont want to see red borders on hover.
here’s the code codepen
2
Answers
That is happening because the border-radius on your button is the same value as the border-radius of your block element with the red background. You can resolve this by removing the border-radius of the button or adding the hover selector to the block element.
Option 1:
Option 2:
since I don’t know your motive for your design and I am assuming you want only a rounded border for your button, and there is some border around the corner that is left out.
I think it is due to the fact that
button
is a child of theblock
class and:hover
only changes the color of thebutton
class. This means thebackground-color
that you have set for theblock
class will still apply and is causing this unexpected behavior.Depending on your design choices you can do any one from the following :
background-color:red;
tobutton
class and remove it fromblock
classborder-radius:inherit
frombutton
class (Don’t removebackground-color
fromblock
class).