【问题标题】:How to make a box with arrow in CSS?如何在 CSS 中制作带箭头的框?
【发布时间】:2011-10-21 18:43:02
【问题描述】:

如何在 CSS 中制作带箭头的框?

制作圆角很容易。但是任何想法都可以在不使用图像的情况下在左侧制作箭头。

是否有可能实现

只有一个元素<p>....</p>

body {
  background: #ff004e;
  padding: 40px
}
p {
  background: white;
  -webkit-border-radius: 10px;
  -moz-border-radius: 10px;
  border-radius: 10px;
  width: 250px;
  height: 150px
}
<p></p>

【问题讨论】:

标签: css css-shapes


【解决方案1】:

像这样:

.arrow {
    border: solid 10px transparent;
    border-right-color: #FFF;
}

演示: http://jsfiddle.net/sparkup/edjdxjf2/

更新:

也可以不用空元素用css属性:before实现

element:before {
    content: "";
    position: absolute;
    top: 50%;                         // half way down (vertical center).
    margin-top: -15px;                // adjust position, arrow has a height of 30px. 
    left:-30px;
    border: solid 15px transparent;
    border-right-color: #FFF;
    z-index: 1;
}

演示http://jsfiddle.net/sparkup/y89f1te0/

希望对你有帮助

【讨论】:

  • 不错,但箭头顶部不锋利。这非常好,但如果我们可以不用空元素就更好了。
  • 不添加<div id="pointer"></div> 是否可以做到before:
  • 我不知道这种方法,对不起。
  • @holodoc:Floern 链接到的问题是独家新闻。
  • 不错!我添加了指针事件:无;这样箭头就不会干扰对下方按钮的点击。
【解决方案2】:

如果你不想使用 div,你可以使用 span。

span#pointer{border:solid 10px transparent;border-right-color:#fff;position:absolute;margin:-85px 0 0 -20px;}

http://jsfiddle.net/SSKwn/

【讨论】:

  • 我的观点是空元素在语义上不好不要将 div 更改为 span
【解决方案3】:

Chris Coyier 对使用单个元素(以及之前/之后)在 CSS 中构建的可能形状进行了出色的总结: http://css-tricks.com/examples/ShapesOfCSS/

【讨论】:

  • +1 在我看到链接中的示例之前,其他答案如何在没有轮换的情况下完成这一点对我来说并不是很明显。
【解决方案4】:
a.right{ border-style: dashed;
  border-color: transparent;
  border-width: 0.53em;
  display: -moz-inline-box;
  display: inline-block;
  font-size: 100px;
  height: 0;
  line-height: 0;
  position: relative;
  vertical-align: middle;
  width: 0; border-left-width: 1em;
  border-left-style: solid;
  border-left-color: #666;
  left: 0.25em; }

以上代码可用于右箭头。

【讨论】:

    【解决方案5】:

    标准工具提示

    如果你想要一个简单的箭头,那么你可以添加一个带有border-right的伪元素。

    body {
        background:#ff004e;
        padding:40px;
    }
    p {
        background:white;
        border-radius: 10px;
        width:250px;
        height:150px;
        position: relative;
        display: inline-block;
    }
    p:before {
        content:"";
        position: absolute;
        height: 0px;
        width: 0px;
        top: 60px;
        left: -29px; /* 1px buffer for zooming problems while rendering*/
        border-width: 15px;
        border-color: transparent white transparent transparent;
        border-style: solid;
    }
    <p></p>

    FIDDLE 1


    平边工具提示

    如果你想要箭头的平边,试试这个:

    body {
        background: #ff004e;
        padding:40px;
    }
    p {
        background:white;
        border-radius: 10px;
        width:250px;
        height:150px;
        position: relative;
        display: inline-block;
    }
    p:before {
        content:"";
        position: absolute;
        height: 45px;
        width: 16px; /* 1px buffer for zooming problems while rendering*/
        top: 50px;
        left: -15px; 
        background: white;
    }
    p:after {
        content:"";
        position: absolute;
        height: 40px;
        width: 15px;
        border-radius: 0 40px 40px 0;
        top: 75px;
        left: -15px;
        background: #ff004e;
        box-shadow: 0 -45px 0 0 #ff004e;
    }
    <p></p>

    FIDDLE 2

    【讨论】:

      【解决方案6】:

      我的回答(没有平边), 添加了一些计算公式:

      .mainBox {
          border: solid 1px #e0e0e0;        
      }
      
      .mainBox:before {
          content:"";
          position: absolute;
          /*The right value must be calculated with: (top value of after) - (top value of before) = (right value of before) */
          /*example: (-4px) - (-7px) = 3px*/
          right: 72px; 
          /*The `top` value must be identical to border-width*/
          top: -7px; 
          width: 0;
          height: 0;
          border-style: solid;
          /*The `border-width` value must be identical to top*/
          border-width: 0 7px 7px 7px;
          /*The border color 3rd (#e0e0e0) value must be identical to its parent border color*/
          border-color: transparent transparent #e0e0e0 transparent;
          /*The (z-index of before) must at least one below the (z-index of after)*/
          /*Example: (z-index of before) < (z-index of after) or 9998 < 9999*/
          z-index:9998;
      }
      
      .mainBox:after {
          content:"";
          position: absolute;
          right: 75px;
          top: -4px; /*suppose to be identical to border-width*/
          width: 0;
          height: 0;
          border-style: solid;
          border-width: 0 4px 4px 4px;
          border-color: transparent transparent #fff transparent; 
          z-index:9999;
      }
      

      基本规则是:

      1. 前右值必须使用以下公式计算: (after's top) - (before's top) = (before's right)

      示例:(-4px) - (-7px) = 3px

      1. beforeaftertop 值必须与border-width 相同。

      2. 第三个边框颜色(示例中为#e0e0e0)值必须与其父边框颜色相同。

      3. beforez-index 必须至少低于afterz-index

      示例:(before's z-index) after's z-index) 或 9998

      结果:

      【讨论】:

        【解决方案7】:

        使用此在线工具,您无需输入大量代码即可完成

        http://www.cssarrowplease.com

        【讨论】:

          猜你喜欢
          • 2013-04-17
          • 1970-01-01
          • 1970-01-01
          • 2018-08-02
          • 2015-02-22
          • 1970-01-01
          • 1970-01-01
          • 2015-12-28
          相关资源
          最近更新 更多