0

I have a button with a hover effect that fills the background from left to right:

Current HTML

<a href="#" class="button">Mehr erfahren</a>

Current CSS

body {
  background-color: #f6f6f6;
}

a {
  text-decoration: none;
  color: black;
}

a:hover {
  text-decoration: none;
}

.button {
  font-size: 20px;
  color: black;
  background-color: white;
  padding: 20px;
  border-radius: 50px;
  display: inline-block;
  background-image: radial-gradient(circle at left, black 50%, black 50%);
  background-size: 0 100%;
  background-repeat: no-repeat;
  transition: 0.4s;
}

.button:hover {
  color: white;
  background-size: 100% 100% !important;
}

.button::before {
  content: "";
  display: inline-block;
  width: 15px;
  height: 15px;
  -moz-border-radius: 7.5px;
  -webkit-border-radius: 7.5px;
  border-radius: 7.5px;
  background-color: white;
  border: 1px solid black;
  margin-right: 15px;
}

Find it also here on codepen

Goal is that the right side of the fill color has rounded corners like here: See desired behaviour

Any ideas? Thanks in advance

Florian
  • 39
  • 5

1 Answers1

1

As mentioned in this other SO question, there is no way to add border-radius directly to the background-image. If you are open to another option to get your desired result using a pseudo background element and adding a <span> around your text for z-index adjustment, then you can reference my example below.

body {
  background-color: #f6f6f6;
}

a {
  text-decoration: none;
  color: black;
}

a:hover,
a:focus {
  text-decoration: none;
}

.button {
  font-size: 20px;
  color: black;
  background-color: white;
  padding: 20px;
  border-radius: 50px;
  display: inline-block;
  transition: 0.4s;
}

.button:hover,
.button:focus {
  color: white;
}


/* New CSS */

.button {
  position: relative;
  overflow: hidden;
  -webkit-mask-image: -webkit-radial-gradient(white, black);
}

.button:hover::after,
.button:focus::after {
  transform: translateX(0);
}

.button::after {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  background: #000;
  width: 100%;
  height: 100%;
  border-radius: 50px;
  transform: translateX(-100%);
  z-index: 0;
  transition: 0.4s;
}

.button span {
  position: relative;
  z-index: 1;
}

.button span::before {
  content: "";
  display: inline-block;
  width: 15px;
  height: 15px;
  -moz-border-radius: 7.5px;
  -webkit-border-radius: 7.5px;
  border-radius: 7.5px;
  background-color: white;
  border: 1px solid black;
  margin-right: 15px;
}
<a href="#" class="button"><span>Mehr erfahren</span></a>
wouch
  • 1,097
  • 4
  • 6
  • Awesome, thank you so much. Of course, I am open to other options, I just ran out of ways how to approach that issue. – Florian Jan 06 '23 at 19:57
  • I just encountered a weird bug with this solution in safari. It starts from a straight vertical line. I don't know if you can test it with safari, but if so, do you know how to fix this? Thanks again! – Florian Feb 07 '23 at 19:11
  • @Florian I found in this [other SO question](https://stackoverflow.com/questions/49066011/overflow-hidden-with-border-radius-not-working-on-safari) that is a known issue. I added the fix from that to my code snippet – wouch Feb 08 '23 at 16:17