带边框/轮廓的六角形

编程入门 行业动态 更新时间:2024-10-24 12:24:01
本文介绍了带边框/轮廓的六角形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我知道可以使用以下代码创建一个六边形形状:

.hex:before { content:; width:0; height:0; border-bottom:30px solid#6C6; border-left:52px solid transparent; border-right:52px solid transparent; position:absolute; top:-30px; } .hex { margin-top:30px; width:104px; height:60px; background-color:#6C6; position:relative; } .hex:after { content:; width:0; position:absolute; bottom:-30px; border-top:30px solid#6C6; border-left:52px solid transparent; border-right:52px solid transparent; }

如何创建一个用一种颜色填充的六角形, ?

欢迎任何其他选择,我想避免 >使用图片。

解决方案

不能直接 这是因为六边形是通过伪元素通过边界创建的。另一种方法是在六边形内重叠六边形,从而获得相同的期望结果。

这里是

HTML 。

< div class =hex> < div class =hex inner> < div class =hex inner2>< / div> < / div> < / div>

CSS (三层 - 两个内部元素)

从六边形类开始,定义形状/大小/颜色:

.hex { margin-top:70px; width:208px; height:120px; background:#6C6; position:relative; } .hex:before,.hex:after { content:; border-left:104px solid transparent; border-right:104px solid transparent; position:absolute; } .hex:before { top:-59px; border-bottom:60px solid#6C6; } .hex:after { bottom:-59px; border-top:60px solid#6C6; }

设计内部样式并使用 transform:scale() 按比例减小内部元素的尺寸。在本示例中,使用 scale(.8,.8)的比例。如果你想要更粗的边框,减少数字;

指定并覆盖颜色,同时增加 z-index value,使元素向前。

.hex.inner { background-color:blue; -webkit-transform:scale(.8,.8); -moz-transform:scale(.8,.8); transform:scale(.8,.8); z-index:1; } .hex.inner:before { border-bottom:60px solid blue; } .hex.inner:after { border-top:60px solid blue; }

基本上按照与上次相同的步骤设计第二个嵌套元素。值得一提的是,使用相同的 scale 值,因为它在已经缩放的元素中。当然,你可以使用你想要的;这只是一个基本的例子。

.hex.inner2 { background-color:red; -webkit-transform:scale(.8,.8); -moz-transform:scale(.8,.8); transform:scale(.8,.8); z-index:2; } .hex.inner2:before { border-bottom:60px solid red; } .hex.inner2:after { border-top:60px solid red; }

同样, 此处的实例

I know it's possible to create a hexagon shape using the following code:

.hex:before { content: " "; width: 0; height: 0; border-bottom: 30px solid #6C6; border-left: 52px solid transparent; border-right: 52px solid transparent; position: absolute; top: -30px; } .hex { margin-top: 30px; width: 104px; height: 60px; background-color: #6C6; position: relative; } .hex:after { content: ""; width: 0; position: absolute; bottom: -30px; border-top: 30px solid #6C6; border-left: 52px solid transparent; border-right: 52px solid transparent; }

How can I create a hexagon that's filled with one color, and outlined with another? I'm trying to fiddle with it, but it does not seem to be possible.

Any other alternatives are welcome, I would like to avoid using images.

解决方案

It is not directly possible to achieve this, as hexagons are created by borders through pseudo elements. An alternative would be to overlay hexagons within hexagons, thus achieving the same desired results.

Here is an example of what can be achieved:

LIVE EXAMPLE HERE

HTML - pretty basic, continue the same pattern for more borders.

<div class="hex"> <div class="hex inner"> <div class="hex inner2"></div> </div> </div>

CSS (three layers - two inner elements)

Start with the hexagon class, defining the shape/size/colors:

.hex { margin-top: 70px; width: 208px; height: 120px; background: #6C6; position: relative; } .hex:before, .hex:after { content:""; border-left: 104px solid transparent; border-right: 104px solid transparent; position: absolute; } .hex:before { top: -59px; border-bottom: 60px solid #6C6; } .hex:after { bottom: -59px; border-top: 60px solid #6C6; }

Style the inner class and use transform: scale() to proportionally decrease the dimensions of the inner elements. In this example, a scale of scale(.8, .8) is used. If you want thicker borders, decrease the numbers; conversely, if you want thinner borders, increase the numbers.

Specify and overwrite the colors, also increase the z-index value to bring the element forward.

.hex.inner { background-color:blue; -webkit-transform: scale(.8, .8); -moz-transform: scale(.8, .8); transform: scale(.8, .8); z-index:1; } .hex.inner:before { border-bottom: 60px solid blue; } .hex.inner:after { border-top: 60px solid blue; }

Style the second nested element, essentially following the same steps as last time. It's worth nothing that the same scale value is used, because it is within an already scaled element. Of course, you can use whatever you want; this is just a basic example.

.hex.inner2 { background-color:red; -webkit-transform: scale(.8, .8); -moz-transform: scale(.8, .8); transform: scale(.8, .8); z-index:2; } .hex.inner2:before { border-bottom: 60px solid red; } .hex.inner2:after { border-top: 60px solid red; }

Again, live example here

更多推荐

带边框/轮廓的六角形

本文发布于:2023-11-30 02:40:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1648439.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:边框   轮廓

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!