【问题标题】:How to make a comment shape using CSS如何使用 CSS 制作评论形状
【发布时间】:2018-07-29 02:23:26
【问题描述】:

是否可以使用 CSS 代码制作如下所示的形状?

我尝试了一些方法,但我无法做到,所以我需要一些帮助。

#demo {
  background-color: #333;
  height: 100px;
  position: relative;
  width: 100px;
}
#demo:after {
  content: ' ';
  height: 0;
  position: absolute;
  width: 0;
  border: 10px solid transparent;
  border-top-color: #333;
  top: 100%;
  left: 50%;
  margin-left: -10px;
}
<div id="demo"></div>

【问题讨论】:

标签: html css shape css-shapes


【解决方案1】:

您可以在:before:after 上使用两个三角形:伪元素。

div {
  position: relative;
  width: 100px;
  height: 40px;
  border: 4px solid black;
}
div:after, div:before {
  content: '';
  position: absolute;
  width: 0;
  height: 0;
  bottom: -9px;
  right: 20px;
  border-top: 10px solid white;
  border-right: 2px solid white;
  border-left: 10px solid transparent;
}
div:before {
  bottom: -15px;
  right: 16px;
  border-top: 15px solid black;
  border-right: 4px solid black;
  border-left: 15px solid transparent;
}
<div></div>

您总是可以使用svg 使其更加轻松。

<svg width="108" height="68" viewBox="-2 -2 108 68">
  <path d="M0,0 h100 v40 h-20 v10 l-10,-10 h-70z" fill="none" stroke="black" stroke-width="3">
</svg>

您可以在stroke 上使用pattern 而不是单一颜色。

<svg width="108" height="68" viewBox="-2 -2 108 68">
  <defs>
    <pattern id="pat" patternUnits="userSpaceOnUse" width="6" height="6" viewBox="0 0 6 6">
      <path d="M0,0 h3 l3,3 v3z M0,6 h3 l-3,-3z" fill="#E9D641" />
      <path d="M0,0 v3 l3,3 h3z M3,0 h3 v3z" fill="#85A03C" />
    </pattern>
  </defs>
  <path d="M0,0 h100 v40 h-20 v10 l-10,-10 h-70z" fill="none" stroke="url(#pat)" stroke-width="4" />
</svg>

【讨论】:

    【解决方案2】:

    试试这个:

    .speech-bubble {    
        border-radius: 2px;
        color: #fff;
        margin: 1em 0 3em;
        padding: 15px;
        position: relative;
        border: solid 2px #000;
        color: #000;
    }
    
    .speech-bubble:after, .speech-bubble:before {
        top: 100%; left: 90%;
        border: solid transparent;
        content: " ";
        height: 0;
        width: 0;
        position: absolute;
        pointer-events: none;
    } 
    
    .speech-bubble:after {
        border-color: rgba(255, 255, 255, 0);
        border-top-color: #fff;
        border-width: 20px 0 0 20px; 
        margin-left: -20px;
    } 
    
    .speech-bubble:before {
        border-color: rgba(0, 0, 0, 0);
        border-top-color: #000;
        border-width: 27px 0px 0px 27px;
        margin-left: -24px;
    }
    &lt;div class="speech-bubble"&gt;This is a speech bubble&lt;/div&gt;

    【讨论】:

      【解决方案3】:

      div.icon {
          height: 32px;
          width: 32px;
          position: relative;
          margin: 15px;
          overflow: hidden;
          display: inline-block;
      }
      
      div.icon div.chat {
          width: 32px;
          height: 22px;
          background: #333;
      }
      
      div.icon div.chat:after {
          content: '';
          width: 0px;
          height: 0px;
          border-style: solid;
          border-color: #333 transparent transparent transparent;
          border-width: 5px;
          position: absolute;
          top: 16px;
          left: 2px;
          -webkit-transform: rotate(135deg);
          -moz-transform: rotate(135deg);
          -ms-transform: rotate(135deg);
          -o-transform: rotate(135deg);
          transform: rotate(135deg);
      }
      <div class="icon">
          <div class="chat"></div>
      </div>

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-09-21
        • 2019-08-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多