border curved css - 圆端弯曲

border curved css - circle with curved end

我正在建立一个网站,我很难在CSS

中做一个细节

我需要制作一个末端弯曲的圆形边框,为了让您更好地理解,我将展示照片和 post 我的代码

我需要什么 (Photoshop)

我想要一个 CSS 解决方案,但我做不到。

这是我实际拥有的:

.bottom-bar {
  background: #29a7e8;
  position: absolute;
  bottom: 0;
  width: 100%;
  height: 50px;
  text-align: center;
}

.circle {
  display: inline-block;
  position: relative;
  top: -10px;
  border-radius: 100%;
  background: #29a7e8;
  width: 60px;
  height: 60px;
  margin: 0 1rem;
}
<div class="bottom-bar">
      <div class="circle"></div>
      <div class="circle"></div>
      <div class="circle"></div>
    </div>

您可以使用 SVG 作为背景来执行此操作:

.bottom-bar {
  background: #29a7e8;
  position: absolute;
  bottom: 0;
  width: 100%;
  height: 50px;
  text-align: center;
}

.circle {
  display: inline-block;
  position: relative;
  top: -28px;
  border-radius: 100%;
  background: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='10 10 45 15'  width='64' height='64' fill='%2329a7e8'><path d='M12 24 L52 24 L52 16 C40 16 42 10 32 10 C20 10 22 16 12 16 Z' /></svg>") 0 0/100% 100% no-repeat;
  width: 60px;
  height: 60px;
  margin: 0 1rem;
}
<div class="bottom-bar">
  <div class="circle"></div>
  <div class="circle"></div>
  <div class="circle"></div>
</div>


<svg xmlns='http://www.w3.org/2000/svg'
  viewBox='10 10 45 15'
  width='64' height='64'
  fill='#29a7e8'>
  <path d='M12 24 L52 24 L52 16 C40 16 42 10 32 10 C20 10 22 16 12 16 Z' />
</svg>

对于 CSS 唯一的解决方案,您可以考虑结合径向渐变来创建曲线:

.bottom-bar {
  background: #29a7e8;
  position: absolute;
  bottom: 0;
  width: 100%;
  height: 50px;
  text-align: center;
}

.circle {
  display: inline-block;
  position: relative;
  top: -30px;
  background:
  radial-gradient(circle at top right,transparent 50%,#29a7e8 51%)100% 21px/12px 10px no-repeat,
  radial-gradient(circle at top left,transparent 50%,#29a7e8 51%)0 21px/12px 10px no-repeat,
  radial-gradient(circle at center,#29a7e8 55%, transparent 56%);
  width: 60px;
  height: 60px;
  margin: 0 1rem;
}
<div class="bottom-bar">
  <div class="circle"></div>
  <div class="circle"></div>
  <div class="circle"></div>
</div>

一般来说,有多种方法可以创建这种形状,从简单到复杂:

  • radial-gradient 添加 2 个伪元素。

    最简单且得到良好支持的解决方案。可能是我会用的那种。

  • mask-image 添加 2 个伪元素(同上,但支持更差)。

    在代码方面与预览版非常相似,但具有 really bad support(支持它的浏览器需要前缀)。

    如果您想检查它们的相似程度,请查看其他类似问题:

  • 添加 2 个带有 border-radiusbox-shadowbackground: transparent 的伪元素。

    需要更多的代码,但看起来更流畅一些,至少在 Chrome Version 78.0.3904.108 上是这样,尽管差异很小。在任何情况下,您可以做的形状都不能像以前的选择一样复杂,特别是如果您想使用椭圆而不是圆形,就像您的情况一样。

  • 使用 SVG。

    我认为 SVG 解决方案在这里不值得,但对于更复杂的形状或 animated/transitioning 形状,它是一个很好的替代方案。无论如何,Temani Afif 已经添加了使用 SVG 的解决方案。

需要记住的一点是,由于它看起来像导航栏,因此每个按钮的 hoverable/clickable 区域应尽可能与用户实际看到的相匹配,而 Temani Afif的解决方案。

这是使用 radial-gradient:

时的样子

body {
  margin: 0;
  font-family: monospace;
  background: #DDD;
}

.bar {
  background: white;
  position: fixed;
  bottom: 0;
  width: 100%;
  height: 60px;
  text-align: center;
}

.circle {
  position: relative;
  top: -10px;
  border-radius: 100%;
  background: white;
  width: 60px;
  height: 60px;
  margin: 0 16px;
  cursor: pointer;
  display: inline-flex;
  justify-content: center;
  align-items: center;
  font-size: 24px;
  font-weight: bold;
  color: black;
  transition: box-shadow ease-in 150ms;
  background: white;
  border: 0;
  outline: none;
}

.circle:hover {
  box-shadow: 0 16px 16px 0 rgba(0, 0, 0, .125);
}

.circle:active {
  box-shadow: 0 8px 8px 0 rgba(0, 0, 0, .125);
}

.circle::before,
.circle::after {
  content: "";
  position: absolute;
  top: 4px;
  width: 32px;
  height: 6px;
  background: white;
  z-index: 1;
}

.circle::before {
  left: -18px;
  background: radial-gradient(ellipse at 0% -25%, transparent 0, transparent 70%, white 70%, white 100%);
}

.circle::after {
  right: -18px;
  background: radial-gradient(ellipse at 100% -25%, transparent 0, transparent 70%, white 70%, white 100%);
}
<nav class="bar">
  <button class="circle"></button>
  <button class="circle"></button>
  <button class="circle"></button>
</nav>

如果您想查看类似的问题和所有替代方案,请查看: