半透明背景和边框的六边形

3

我为一个六边形的 div 编写了一些代码,它运行得很好。问题是现在我需要一个 半透明背景,但是 :after 和 :before 标签重叠导致透明度不一致。

.hexagon {
  position: relative;
  width: 290px;
  height: 173.21px;
  margin: 86.60px 0;
  border-left: solid 5px #333333;
  border-right: solid 5px #333333;
  background-color: rgb(102, 204, 34, 0.7);
}

.hexagon:before,
.hexagon:after {
  box-sizing: border-box;
  content: "";
  position: absolute;
  z-index: 1;
  width: 212.13px;
  height: 212.13px;
  -webkit-transform: scaleY(0.5774) rotate(-45deg);
  -ms-transform: scaleY(0.5774) rotate(-45deg);
  transform: scaleY(0.5774) rotate(-45deg);
  background-color: rgb(102, 204, 34, 0.7);
  left: 38.9340px;
}

.hexagon:before {
  top: -106.0660px;
  border-top: solid 7.0711px #333333;
  border-right: solid 7.0711px #333333;
}

.hexagon:after {
  bottom: -106.0660px;
  border-bottom: solid 7.0711px #333333;
  border-left: solid 7.0711px #333333;
}
<div class="hexagon">
  <span style="font-size: 11px">1</span>
  <span>2     </span>
</div>

什么方法最好让它运行起来?

涉及两个以上的容器元素,并将伪元素放入其中,这样您就可以使用overflow:hidden来切断否则会重叠的部分...? - CBroe
2
等等,什么是十六进制问题,Web-Tiki不在这里? - Persijn
制作类似HTML中的形状最好的方法是使用SVG。我知道这不是通过HTML和CSS实现的,但在新的浏览器中它可以很好地工作。 - D. Pardal
2个回答

4
你可以使用渐变来修改伪元素的背景色,以使其只着色一半:

.hexagon {
  position: relative;
  width: 290px;
  height: 173.21px;
  margin: 86.60px 0;
  border-left: solid 5px #333333;
  border-right: solid 5px #333333;
  background-color: rgb(102, 204, 34, 0.7);
}

.hexagon:before,
.hexagon:after {
  box-sizing: border-box;
  content: "";
  position: absolute;
  z-index: 1;
  width: 212.13px;
  height: 212.13px;
  -webkit-transform: scaleY(0.5774) rotate(-45deg);
  -ms-transform: scaleY(0.5774) rotate(-45deg);
  transform: scaleY(0.5774) rotate(-45deg);
  left: 38.9340px;
}

.hexagon:before {
  top: -106.0660px;
  border-top: solid 7.0711px #333333;
  border-right: solid 7.0711px #333333;
  background:linear-gradient(to bottom left,rgb(102, 204, 34, 0.7) 48%,transparent 48%);
}

.hexagon:after {
  bottom: -106.0660px;
  border-bottom: solid 7.0711px #333333;
  border-left: solid 7.0711px #333333;
  background:linear-gradient(to top right,rgb(102, 204, 34, 0.7) 48%,transparent 49%);
}
<div class="hexagon">
  <span style="font-size: 11px">1</span>
  <span>2     </span>
</div>

您也可以考虑使用SVG解决方案:

body {
 margin:0;
 background:linear-gradient(to right, blue 10%,pink);
}

.box {
  background: url('data:image/svg+xml;utf8,<svg version="1.1" xmlns="http://www.w3.org/2000/svg" width="174" height="200" viewBox="-5 -5 180 220"><path fill="rgb(102, 204, 34, 0.5)" stroke="black" stroke-width="3" d="M86.60254037844386 0L173.20508075688772 50L173.20508075688772 150L86.60254037844386 200L0 150L0 50Z"></path></svg>');
  height: 200px;
  width: 180px;
  background-repeat: no-repeat;
  background-position: center;
  display:inline-block;
  line-height:150px;
  text-align:center;
}
svg {
 display:inline-block;
 vertical-align:top;
}
<div class="box">
  SVG as background
</div>
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" width="174" height="200" viewBox="-5 -5 180 220"><path fill="rgb(102, 204, 34, 0.5)" stroke="black" stroke-width="3" d="M86.60254037844386 0L173.20508075688772 50L173.20508075688772 150L86.60254037844386 200L0 150L0 50Z"></path>
<text x=20 y=80 >Normal SVG</text>
</svg>

这里有另一个结合clip-path和渐变的不完美想法:

body {
 background:linear-gradient(to right,pink, blue);
}
.hexagon {
  position: relative;
  width: 290px;
  height: 290px;
  margin: 50px;
  background:
  linear-gradient(to bottom left,transparent calc(50% - 3px),#000 calc(50% - 3px))0 100%/50% 25% no-repeat,
  linear-gradient(to bottom right,transparent calc(50% - 3px),#000 calc(50% - 3px))100% 100%/50% 25% no-repeat,
  linear-gradient(to top right,transparent calc(50% - 3px),#000 calc(50% - 3px))100% 0/50% 25% no-repeat,
  linear-gradient(to top left,transparent calc(50% - 3px),#000 calc(50% - 3px))0 0/50% 25% no-repeat,
  linear-gradient(#000,#000)0 0/3px 100% no-repeat,
  linear-gradient(#000,#000)100% 0/3px 100% no-repeat,
  rgba(102, 204, 34, 0.5);
  -webkit-clip-path: polygon(50% 0%, 100% 25%, 100% 75%, 50% 100%, 0% 75%, 0% 25%);
clip-path: polygon(50% 0%, 100% 25%, 100% 75%, 50% 100%, 0% 75%, 0% 25%);
}
<div class="hexagon">
</div>


哈哈,不错。 - trinaldi
这太棒了,它帮了我很多忙。我对它有一个问题,就是它不是像素完美的,但目前来说它运行得很好。谢谢! - Zbyszek Kisły
@TutuKaeen 我也加了一个SVG的解决方案 ;) - Temani Afif

4

考虑到您似乎只需要一个六边形,并希望为其添加边框和半透明背景,我建议使用内联SVG。

操作非常简单:

svg{display:block;width:30%;margin:0 auto;}
body{background:url('https://farm9.staticflickr.com/8760/17195790401_ceeeafcddb_o.jpg');background-position:center;background-size:cover;}
<svg viewbox="0 0 100 100" version="1.1" xmlns="http://www.w3.org/2000/svg">
  <polygon fill="#92D050"
           fill-opacity=".7"
           stroke="#000"
           stroke-width="2"
           points="50 1 95 25 95 75 50 99 5 75 5 25"/>
</svg>

在上面的代码中:
- `fill` 和 `fill-opacity` 属性控制六边形的颜色和不透明度, - `stroke` 和 `stroke-width` 控制六边形边框的颜色和宽度, - `points` 属性使六边形成为一个形状。
更多信息:
- 内联 SVG - 多边形元素

我需要很多,但我也会检查它 :) - Zbyszek Kisły
@TutuKaeen 如果你需要在网格中制作许多六边形,我建议你查看这个代码 https://github.com/web-tiki/responsive-grid-of-hexagons - web-tiki
这不是我想要的,但这是我需要在一周左右解决的问题!这真的很有帮助! - Zbyszek Kisły
@TutuKaeen,很高兴它能够帮助到你,那就是它存在的意义;) - web-tiki

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接