【问题标题】:How can I create div with a pointed top with CSS如何使用 CSS 创建带有尖顶的 div
【发布时间】:2014-11-19 13:01:38
【问题描述】:

我看到很多与远程相关的线程基本上建议在 ::after 或 ::before 伪类中使用 CSS 三角形,但没有一个真正成功。我把这个扔出去看看是否有人有任何想法。

我希望创建一个带有尖顶或倾斜顶部的 div,该 div 仍与 div 的其余部分保持统一的边框和框阴影。

查看我正在尝试创建的图像的链接:

【问题讨论】:

标签: html css css-shapes


【解决方案1】:

如果你不想使用图像,你可以这样做。但在这种情况下,使用图像要容易得多。

body {
    background-color: #CCC;
}

.wrapper {
    
}

.outer {
    width: 0;
    height: 0;
    border-style: solid;
    border-width: 0 205px 32px 205px;
    border-color: transparent transparent #ffffff transparent;
    position: absolute;
}

.inner {
    width: 0;
    height: 0;
    border-style: solid;
    border-width: 0 200px 32px 200px;
    border-color: transparent transparent #ea2225 transparent;
    margin-left: -200px;
    margin-top: 5px;
    position: absolute;
}

.fix {
    background-color: #FFF;
    height: 10px;
    width: 410px;
    position: absolute;
    margin-top: 32px;
}

.red {
    width: 396px;
    height: 300px;
    background-color: #ea2225;
    margin-top: 37px;
    position: absolute;
    border-left: 7px solid #FFF;
    border-right: 7px solid #FFF;
    border-bottom: 6px solid #FFF;
-webkit-box-shadow: 3px 5px 5px 0px rgba(48,48,48,1);
-moz-box-shadow: 3px 5px 5px 0px rgba(48,48,48,1);
box-shadow: 3px 5px 5px 0px rgba(48,48,48,1);
}
<div class="wrapper">
     <div class="fix"></div>
<div class="outer">
   
  <div class="inner"> 
    
  </div>
    
</div>
  
</div>
   <div class="red"></div>

http://jsfiddle.net/0csqog8s/

【讨论】:

    【解决方案2】:

    this 应该让你开始:

    更新

    This is an updated fiddle 更好地呈现。

    .first {
      display: inline-block;
      width: 3em;
      height: 3em
    }
    .second {
      position: relative;
      display: inline-block;
      width: 3em;
      height: 3em
    }
    .third {
      position: absolute;
      display: inline-block;
      width: 0;
      height: 0;
      line-height: 0;
      border: 1.5em solid transparent;
      margin-top: -1em;
      border-bottom: 1em solid #007BFF;
      left: 0em;
      top: 0em
    }
    .forth {
      position: absolute;
      display: inline-block;
      width: 0;
      height: 0;
      line-height: 0;
      border: 1.5em solid #007BFF;
      border-bottom: 1.5em solid #007BFF;
      left: 0em;
      top: 1.5em
    }
    &lt;span class="first"&gt;&lt;span class="second"&gt;&lt;i class="third"&gt;&lt;/i&gt;&lt;i class="forth"&gt;&lt;/i&gt;&lt;/span&gt;&lt;/span&gt;

    【讨论】:

    • 创建三角形的边框技术的问题是你不能给形状一个边框或盒子阴影
    • 如果旧浏览器不重要,他可以用两个旋转的元素创建三角形的顶部。这将解决边框或框阴影的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-04
    • 2013-07-25
    • 2019-09-15
    • 1970-01-01
    • 2014-06-12
    • 1970-01-01
    相关资源
    最近更新 更多