如何为CSS八边形形状提供完整的边框?

Pat*_*ick 3 html css css3 css-shapes

我试图把这个css八角形,并给它一个坚实的红色边框.但是,当我添加边框时,它只适用于形状的一部分.有人有解决方案吗?

在此输入图像描述

这是一个JS FIDDLE

HTML

<div class='octagonWrap'>
      <div class='octagon'></div>
  </div>
Run Code Online (Sandbox Code Playgroud)

CSS

.octagonWrap{
        width:200px;
        height:200px;
        float: left;
        position: relative;
        }
    .octagon{
        position: absolute;
        top: 0; right: 0; bottom: 0; left: 0;
        overflow: hidden;
        }
    .octagon:before {
        position: absolute;
        top: 0; right: 0; bottom: 0; left: 0;
        transform: rotate(45deg);
        background: #777;
        content: '';
        border: 3px solid red;
        }
Run Code Online (Sandbox Code Playgroud)

jpe*_*zov 7

您可以修改自己的代码以使用它.现在,你.octagon应该有适用的.octagon-wrapper规则,并且规则.octagon::before应当适用.octagon.只需转换CSS规则并将它们应用于父级,同时更改要继承的border属性..octagon::before.octagon

.octagonWrap {
    width:200px;
    height:200px;
    float: left;
    position: relative;
    overflow: hidden;
}
.octagon {
    position: absolute;
    top: 0; right: 0; bottom: 0; left: 0;
    overflow: hidden;
    transform: rotate(45deg);
    background: #777;
    border: 3px solid red;
}
.octagon:before {
    position: absolute;
    /* There needs to be a negative value here to cancel
     * out the width of the border. It's currently -3px,
     * but if the border were 5px, then it'd be -5px.
     */
    top: -3px; right: -3px; bottom: -3px; left: -3px;
    transform: rotate(45deg);
    content: '';
    border: inherit;
}
Run Code Online (Sandbox Code Playgroud)
<div class='octagonWrap'>
    <div class='octagon'></div>
</div>
Run Code Online (Sandbox Code Playgroud)

  • 当然可以.无需提出问题的许可 - 来回使得需要更长的时间. (2认同)

Per*_*ijn 5

SVG解决方案

我不知道使用 svg 是否是一种选择,
如果是这里,它是多么简单。

<svg viewBox="0 0 75 75" width="200px">
  <path d="m5,22 18,-18 28,0 18,18 0,28 -18,18, -28,0 -18,-18z" stroke="red" stroke-width="2" fill="black" />
</svg>
Run Code Online (Sandbox Code Playgroud)