【问题标题】:Create a rounded arrow创建一个圆形箭头
【发布时间】:2016-02-21 20:35:52
【问题描述】:

我正在尝试制作一个如下所示的箭头:

但是,这是我能做到的最接近的:

.button {
  margin: 4em 0;
  padding: 2em;
  width: 15%;
  margin: 0 auto;
  text-align: center;
  background-color: #000000;
  color: #ffffff;
  display: -moz-box;
  display: -ms-flexbox;
  display: -webkit-flex;
  display: flex;
  -moz-flex-flow: row wrap;
  -webkit-flex-flow: row wrap;
  -ms-flex-flow: row wrap;
  flex-flow: row wrap;
  webkit-justify-content: center;
  justify-content: center;
}
.curved-arrow {
  display: inline-block;
  border-top: 10px solid #fff;
  border-bottom: 10px solid #fff;
  border-left: 30px solid #fff;
  border-top-right-radius: 100%;
  border-bottom-right-radius: 100%;
}
<div class="button">
  <div class="curved-arrow"></div>
</div>

这甚至可能吗?我知道我可以使用图片,但我的设计师制作了其中几种颜色的图片,以便在整个网站中使用,而我宁愿只使用 CSS 来制作形状。

如果很难看清,它是一条从上到下的平坦直线,左侧是一条直线,右侧是一条向中间弯曲的曲线。

【问题讨论】:

  • 我不知道该怎么做,但从这个页面来看pattle.github.io/simpsons-in-css我会说是的,这是可能的。
  • 我一直在四处寻找,目前还没有找到任何东西,但是感谢您用这些复杂但令人印象深刻的样式给了我希望!
  • 您可以考虑使用网络字体或 SVG。
  • @RobertMcKee 谢谢,我没有考虑过,我会调查的!

标签: html css svg css-shapes


【解决方案1】:

SVG

CSS 边框半径不会让您轻松获得所需的准确输出。我建议使用带有 quadratic bezier command 的路径元素的内联 SVG:

svg{width:100px;}
<svg viewbox="0 0 20 8">
  <path d="M0 0 Q 30 4 0 8" />
</svg>

然后您可以使用 CSS 填充属性为箭头着色,请参阅此 fiddle(在 cmets 中发布的示例归功于 @Nick R


CSS

您也可以使用 2 个值作为border-radius 属性(请参阅MDN 以了解其工作原理)它很简单,但它不会让您使箭头“点”变得如此尖锐在您的图像中:

.curved-arrow { 
  width:40px; height:25px;
  background:#000;
  border-top-right-radius:100% 50%;
  border-bottom-right-radius:100% 50%;
  
}
&lt;div class="curved-arrow"&gt;&lt;/div&gt;

【讨论】:

  • 这也很好用。我很高兴看到有多个答案。我确实喜欢可以在这里应用的 SVG 方法。谢谢!
  • 只是补充一点,对于 SVG,您可以使用 fill 属性 - jsfiddle.net/kcnv7fm0 设置颜色
  • 有了这个,因为它将在多个地方使用,我可以将此 svg 设置为 CSS 中的一个类,还是您的 CSS 版本是此解决方案的最佳实践?
  • @Ishio 这取决于你的目标。如果您希望它看起来像您发布的图像,那么 CSS 不是正确的工具。如果 CSS 版本的输出足够好,对你来说可能会更容易,特别是如果你不习惯 svg。要为 svg 版本着色,请查看 Nick 在 cmets 中发布的小提琴。
  • 是的,我不习惯 SVG,我正在和我的团队一起尝试调整它,但我一直在使用高版本的图像,跨越到控制高度并为其设置一个类。试图减少对图像的调用。但是,这会很好用!
【解决方案2】:

CSS

这需要一点花哨的border-radius 样式来实现您想要的形状。

.container {
  width: 100px;
  height: 100px;
  background: red;
  position: relative;
}
.arrow {
  width: 50px;
  height: 30px;
  position: absolute;
  top: 32px;
  left: 25%;
  border-radius: 0px 100% 100% 0px / 0 50% 50% 0;
  background: white;
}
<div class="container">
  <div class="arrow"></div>
</div>

如果你想在单个元素中使用它,你可以像这样使用伪元素。

.arrow {
  width: 100px;
  height: 100px;
  background: red;
  position: relative;
}
.arrow:before {
  content: '';
  width: 50px;
  height: 30px;
  position: absolute;
  top: 32px;
  left: 25%;
  border-radius: 0px 100% 100% 0px / 0 50% 50% 0;
  background: white;
}
&lt;div class="arrow"&gt;&lt;/div&gt;

SVG

一个很好的选择也是 SVG 来创建这个形状,这也使它完全响应。

<svg width="100px" height="100px" viewbox="0 0 20 20" style="background: red;">
  <path d="M5,7
           Q15,7 15,10
           Q15,13 5,13z" fill="white"></path>
</svg>

【讨论】:

  • 这太完美了,非常感谢您对此的帮助!这将完全按照我的需要工作!
【解决方案3】:

这是我的看法。没有我想要的那么尖锐,但它更接近一点。编辑:第二次拍摄使用两个伪元素来帮助锐化角度。

.button { margin: 4em 0; padding: 2em; width: 15%; margin: 0 auto; text-align: center; background-color: #000000; color: #ffffff; isplay: -moz-box; display: -ms-flexbox; display: -webkit-flex; display: flex; -moz-flex-flow:row wrap; -webkit-flex-flow:row wrap; -ms-flex-flow:row wrap; flex-flow:row wrap; webkit-justify-content: center; justify-content: center; }

.curved-arrow { 
    position: relative;
    display: inline-block;
    height: 36px;
    width: 56px;
    overflow: hidden;
}
.curved-arrow:after {
    content: '';
    position: absolute;
    right: 0;
    top: 0;
    border-radius: 0 100% 0 0;
    height: 50%;
    width: 200%;
    background: #FFF;
}
.curved-arrow:before {
    content: '';
    position: absolute;
    right: 0;
    bottom: 0;
    border-radius: 0 0 100% 0;
    height: 50%;
    width: 200%;
    background: #FFF;
}
<div class="button">
  <div class="curved-arrow"></div>
</div>

【讨论】:

    【解决方案4】:

    我想我做了一些非常相似的东西,也许你可以改变一些边距/宽度或高度:

    #arrow{
        display: block;
        box-sizing: content-box;
        float: none;
        width: 130px;
        height: 50px;
        top: auto;
        right: auto;
        bottom: auto;
        left: auto;
        margin: 0;
        padding: 0;
        overflow: visible;
        outline: none;
        border: 0 solid rgba(0,0,0,1);
        -webkit-border-radius: 50%;
        border-radius: 50%;
        background: #1abc9c;
        box-shadow: none;
        text-shadow: none;
        margin-left:-65px;
    }
    #arrow:after {
     position:absolute;
        left:-65px;
        width:65px;
        background:transparent;
        content:'';
    
    }
    

    它应该是一个椭圆形,我用:after把它隐藏了一半(相当肮脏的把戏)

    js fiddle

    但是,我建议您使用 CSS 精灵,我想它们应该最适合您的情况,因为 CSS 形状对于浏览器来说并不那么容易阅读。

    希望对您有所帮助! :)

    【讨论】:

    • 类似width:80pxheight:20px
    【解决方案5】:

    这样的?

    #arrow-base {
      width: 120px;
      height: 80px;
      background: red;
    }
    #arrow {
      width: 0;
      height: 0;
      border-top: 16px solid transparent;
      border-bottom: 16px solid transparent;
      border-left: 32px solid white;
      display: inline-block;
      margin: 24px 44px;
      border-radius: 2px;
    }
    <div id="arrow-base">
      <div id="arrow">
        <!-- should be empty -->
      </div>
    </div>

    【讨论】:

    • 否定。我需要把左边的提示四舍五入。如果该图片没有显示在页面顶部,请告诉我。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-14
    • 1970-01-01
    • 2021-09-22
    • 1970-01-01
    • 2018-07-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多