为六边形类添加发光效果

时间:2017-10-15 13:18:47

标签: html css

我目前正在尝试在使用CSS类创建的六边形周围添加均匀的发光效果,但由于绘制六边形的方式,发光效果最终会出现奇怪的中断,我似乎无法修复。



.hexagon {
	position: relative;
	border-radius: 5px;
	height: 125px;
	width: 75px;
	margin: 50px;
	box-sizing: border-box;
	border: 5px solid transparent;
	border-top-color: black;
	border-bottom-color: black;
	display: inline-block;
}
.hexagon:before, .hexagon:after {
	content: "";
	border: inherit;
	position: absolute;
	top: -5px;
	left: -5px;
	background: inherit;
	border-radius: inherit;
	height: 100%;
	width: 100%;
}
.hexagon:before {
	transform: rotate(60deg);
}
.hexagon:after {
	transform: rotate(-60deg);
}
.hexagon:hover, .hexagon:hover:before, .hexagon:hover:after {
	box-shadow: 0 10px 2px -2px rgba(255,0,0,0.5), 0 -10px 2px -2px rgba(255,0,0,0.5);
}

<div class=hexagon></div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

您可以在元素内添加更多填充并调整边框半径。所有线都将交叉,半径将固定形状:

&#13;
&#13;
.hexagon {
  position: relative;
  border-radius: 20px;
  padding: 40px;
  height: 125px;
  width: 75px;
  margin: 50px;
  box-sizing: border-box;
  border: 5px solid transparent;
  border-top-color: black;
  border-bottom-color: black;
  display: inline-block;
}

.hexagon:before,
.hexagon:after {
  content: "";
  border: inherit;
  position: absolute;
  top: -5px;
  left: -5px;
  background: inherit;
  border-radius: inherit;
  height: 100%;
  width: 100%;
}

.hexagon:before {
  transform: rotate(60deg);
}

.hexagon:after {
  transform: rotate(-60deg);
}

.hexagon:hover,
.hexagon:hover:before,
.hexagon:hover:after {
  box-shadow: 0 10px 2px -2px rgba(255, 0, 0, 0.5), 0 -10px 2px -2px rgba(255, 0, 0, 0.5);
}
&#13;
<div class=hexagon></div>
&#13;
&#13;
&#13;

您当然可以通过同时更改填充和边界半径

来控制形状的半径