如何在背景上方和边框后面的按钮的右上角添加圆圈?

我想通过使用CSS达到以下结果:

如何在背景上方和边框后面的按钮的右上角添加圆圈?

因此,基本上,我希望圆位于按钮背景的顶部,但位于其边框的后面,并且按钮位于背景的顶部

使用以下代码,我可以绘制一个类似的按钮:

.container {
  margin-top: 30px;
}

button {
  font-size: 20px;
  border: 2px solid black;
  padding: 8px 20px;
  position: relative;
}

.container .circle {
  position: absolute;
  top: -21px;
  right: -21px;
  width: 40px;
  height: 40px;
  -webkit-border-radius: 25px;
  -moz-border-radius: 25px;
  border-radius: 25px;
  background: #4da6ff;
}
<div class="container">
  <button>Test Button
        <span class="circle"></span>
      </button>
</div>

结果:

如何在背景上方和边框后面的按钮的右上角添加圆圈?

这里的问题是圆圈在按钮的顶部,但也在其边框的顶部。

ingkao84 回答:如何在背景上方和边框后面的按钮的右上角添加圆圈?

使用伪元素(::after)在圆上方绘制边框:

.container {
  margin-top: 30px;
}

button {
  position: relative;
  font-size: 20px;
  border: none;
  padding: 8px 20px;
}

button::before {
  position: absolute;
  top: -20px;
  right: -20px;
  width: 40px;
  height: 40px;
  border-radius: 25px;
  background: #4da6ff;
  content: '';
}

button::after {
  position: absolute;
  top: -2px;
  right: -2px;
  bottom: -2px;
  left: -2px;
  border: 2px solid black;
  content: '';
}
<div class="container">
  <button>Test Button</button>
</div>

,

一个想法是将缺失的边框整合到圆内

.container {
  margin-top: 30px;
}

button {
  font-size: 20px;
  border: 2px solid black;
  padding: 8px 20px;
  position: relative;
}

button:before {
  content: "";
  position: absolute;
  top: -1px;
  right: -1px;
  transform:translate(50%,-50%);
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: 
   linear-gradient(black,black) left  /50% 2px,linear-gradient(black,black) bottom/2px 50%,#4da6ff;
 background-repeat:no-repeat;
}
<div class="container">
  <button>Test Button</button>
</div>

或者您可以简单地考虑mix-blend-mode。您必须注意所使用的值,因为它取决于颜色的组合。在这种情况下,合适的是darken

.container {
  margin-top: 30px;
}

button {
  font-size: 20px;
  border: 2px solid black;
  padding: 8px 20px;
  position: relative;
}

button:before {
  content: "";
  position: absolute;
  top: -1px;
  right: -1px;
  transform:translate(50%,-50%);
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: #4da6ff;
  mix-blend-mode:darken;
}
<div class="container">
  <button>Test Button</button>
</div>

仅使用背景的第三种方法:

button {
  font-size: 20px;
  border:0 solid transparent;
  border-top-width:24px;
  border-right-width:24px;
  padding: 8px 20px;
  background:
     linear-gradient(black,black) top   /100% 2px,black) bottom/100% 2px,black) left  /2px 100%,black) right /2px 100%,radial-gradient(circle,#4da6ff 19px,transparent 20px) left bottom/200% 200% padding-box border-box,#e2e2e6 padding-box;
  background-repeat:no-repeat;
}
<div class="container">
  <button>Test Button</button>
</div>

另一个想法是将圆放置在元素后面并切割背景:

.container {
  margin-top: 30px;
  position:relative;
  z-index:0;
}

button {
  font-size: 20px;
  border: 2px solid black;
  padding: 8px 20px;
  position: relative;
  background:radial-gradient(circle at top right,transparent 19px,#e2e2e6 20px);
}

button:before {
  content: "";
  position: absolute;
  z-index:-1;
  top: -1px;
  right: -1px;
  transform:translate(50%,-50%);
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background:#4da6ff;
}
<div class="container">
  <button>Test Button</button>
</div>

本文链接:https://www.f2er.com/3163963.html

大家都在问