【发布时间】:2016-07-25 04:01:20
【问题描述】:
我正在尝试创建两个细长的六边形:
主要特点应该是:
- 可以添加渐变背景
- 可以添加渐变边框
- 文本可以是两行或单行
- 在 Bootstrap 网格中响应(很高兴) - 角的角度应该始终相同。
根据Elongated hexagon shaped button using only one element 到目前为止最好的解决方案是 https://jsfiddle.net/veuc78af/:
/*hexagons*/
.hexagon {
box-sizing: border-box;
position: relative;
display: inline-block;
min-width: 200px;
height: 80px;
margin: 40px auto;
color: #fd0;
text-align: center;
text-decoration: none;
line-height: 80px;
}
.hexagon:before, .hexagon:after {
position: absolute;
content:'';
width: 100%;
left: 0px;
height: 34px;
z-index: -1;
}
.hexagon:before {
transform: perspective(15px) rotateX(3deg);
}
.hexagon:after {
top: 40px;
transform: perspective(15px) rotateX(-3deg);
}
/* hexagon Border Style */
.hexagon.border:before, .hexagon.border:after {
border: 4px solid #fd0;
}
.hexagon.border:before {
border-bottom: none;
/* to prevent the border-line showing up in the middle of the shape */
}
.hexagon.border:after {
border-top: none;
/* to prevent the border-line showing up in the middle of the shape */
}
/* hexagon hover styles */
.hexagon.border:hover:before, .hexagon.border:hover:after {
background: #fd0;
}
.hexagon.border:hover {
color: #fff;
}
此解决方案的主要问题是无法创建渐变背景。所以这对我来说不起作用。
有没有可能这样做?
本项目的主要平台是 iPad2 上的 Safari。
【问题讨论】:
-
适合您的情况的最佳解决方案是使用 SVG。我们可以看看 CSS
clip-path但浏览器支持很差(IE 根本不支持,FF 需要提供 SVG 作为路径)。 -
您也可以使用 JS/JQuery 执行此操作,使用字符串中的字符数,计算/设置六边形的宽度。其他功能可以在 CSS3 中实现。但是这个解决方案意味着你可以为每个六边形设置一个唯一的 id,来设置宽度......
-
我认为 SVG 也是最好的解决方案。我忘了说我项目的主要设备是 Apple iPad。我会更新它并添加标签“svg”
标签: html css svg css-shapes