单个 div 水平 CSS 六边形按钮

Mik*_*tom 3 css button hexagonal-tiles css-shapes

我想使用单个 div 创建一个六边形形状的 CSS 按钮,以保持标记干净。我一直在试验前后伪元素,可以用顶部和底部的六边形“点”来做,但我想用它们向左和向右指向以适应我主题的其余部分。我已经接近了,但我无法在我想要的地方获得 after 伪元素。任何人都可以解决这个问题吗?

这是我要做的:

#hex {
  background-color:green;
  width:100px;
  height:100px;
  float:left;
  display:block;
}

#hex::before {
  content:"";
  border-top:50px solid red;
  border-bottom:50px solid red;
  border-right:30px solid blue;
  float:left;
}

#hex::after {
  content:"";
  border-top:50px solid red;
  border-bottom:50px solid red;
  border-left:30px solid blue;
  float:left;
}
Run Code Online (Sandbox Code Playgroud)

并且在http://jsfiddle.net/higginbottom/YKx2M/有一个 JS Fiddle

fca*_*ran 8

试试这个例子:http : //jsbin.com/ipaked/6 (在 Fx 和 Chrome 上测试)

相关 CSS

.hexagon {
  position: relative;
  width: 124px;
  height: 100px;
  background: #d8d8d8;
}

.hexagon:after,
.hexagon:before {
  position: absolute;
  content: "";
  z-index: 1;
  top: 0;  
  width: 0px;
  background: #fff;
  border-top: 50px transparent solid; 
  border-bottom: 50px transparent solid;
}

.hexagon:before {
    left: 0;
    border-right: 30px #d8d8d8 solid; 
}
.hexagon:after {
    right: 0;
    border-left: 30px #d8d8d8 solid; 
}
Run Code Online (Sandbox Code Playgroud)

(调整六边形的边框宽度和大小,使其看起来像您喜欢的那样。)


作为替代方案,您还可以使用单个伪元素,您可以在其中显示黑色六边形 unicode 字符U+2B21,如下例所示:http : //jsbin.com/ipaked/7

CSS

.hexagon {
  position: relative;
  width: 120px;
  height: 100px;
  line-height: 100px;
  text-align: center;
}

.hexagon:before {
  position: absolute;
  content: "\2B21";
  font-size: 160px;
  z-index: 1;
  width: 100%; 
  height: 100%;
  left: 0;
  top: 0;  
}
Run Code Online (Sandbox Code Playgroud)

这可能是一个更好的选择(如果使用相对字体大小),因此当用户在浏览器上增加或减少基本字体大小时,六边形可以自行调整。


VVS*_*VVS 6

我正在使用剪辑路径

.btn {
  display: inline-block;
  text-align: center;
  text-decoration: none;
  vertical-align: middle;
  user-select: none;
  padding: 0.375rem 2rem;
  
  --btn-raise: 1rem;
  clip-path: polygon(var(--btn-raise) 0%, calc(100% - var(--btn-raise)) 0%, 100% 50%, calc(100% - var(--btn-raise)) 100%, var(--btn-raise) 100%, 0 50%);
  background-color: #fefd64;
  text-transform: uppercase;
}
Run Code Online (Sandbox Code Playgroud)
<a class="btn" href="/call">Call call</a>
Run Code Online (Sandbox Code Playgroud)