好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

CSS3绘制六边形的简单实现

因为很 简单 ,所以先总结一下:使用CSS3 绘制 六边形主要使用 伪类 :before和 :after 在 源 元素之前和之后再绘制两个元素,并 利用 css3的 边框样式 ,将这两个元素变成三角形放置在源元素的两端即可。

(因为之前在生物 公司 工作过, 觉得 六边形更贴近生物分子、基因等概念,包括我们在网上搜索关于生物分子、基因等图片,好多也有六边形的样式,所以那时候在页面做一些功能性的导航或Tag,都会觉得六边形更贴近一些)。

完整的页面效果如下图:(其实是多个六边形定位成这样子的。当然,也可以设置不同六边形的颜色,这样就可以更好的区分不同的模块功能了)。

&nbs p;

我们可以单独提出一个六边形分析一下,如下图:

知道 了分析思路,我们可以先 了解 一下如何绘制三角形,网上的列子也很多, 不过 没有使用过的童鞋 不用 找了,下面也给出代码和示例,如下:

效果图:

CSS代码:

CSS Code 复制内容到剪贴板

.arrow{                   dis play :  inline - block ;                   width : 0px ;                   h ei ght :  0px ;                   border -s tyle :  solid ;                   border-width :  100px ; //与 padding 、 m arg in 属性类似,顺序为上、右、下、左                   border-color :  red   blue  orange  gray ;  //顺序为上、右、下、左}  

HT ML 代码:

XM L/HTML Code 复制内容到剪贴板

< div   class = "arrow" > </ div >   

如上图所说,利用border边框属性,填充我们 不想 要的颜色为透明色,即可得到某一部分三角形,代码和图片效果如下。

效果图:(左边的三角形是我们需要的,其它的设置为了透明色)

CSS代码:

CSS Code 复制内容到剪贴板

.arrow{                   display :  inline - block ;                   width : 0px ;                   height :  0px ;                   border-bottom :  100px   solid   transparent ;  //设置透明色                   border -t op :  100px   solid   transparent ;   //设置透明色                   border-right :  100px   solid   transparent ;  //设置透明色                   border-left :  100px   solid   gray ;                 }  

HTML代码:

XML/HTML Code 复制内容到剪贴板

< div   class = "arrow" > </ div >   

Okay。知道了如何画三角形,在利用CSS伪类:before和:after就可以完成我们想要绘制的六边形了。

:before是在元素的前面插入内容

:after是在元素的后面插入内容

如果我们想要插入一些文字性的内容可以在它的 content属性中录入需要展示的文字,例如 content:"HELLO WORLD",不过我们的例子是不需要展示额外信息的。我们只是需要将before和after这两个伪元素变成三角形放置到固定位置即可。

给出完整的代码如下:

XML/HTML Code 复制内容到剪贴板

<!DOCTY PE  html >    < html >    < head   lang = "en" >         < meta   charset = "UTF-8" >         < t IT le > </ title >         < style   type = "text/css" >            .sharp:before{             content:"";  //不需要展现文字等内容,所以设置为空字符             width:0px;             border-bottom:80px solid transparent;             border-top:80px solid transparent;             border-right:40px solid  # 6c6;             position:absolute;             left:-40px;             top:0px;         }         .sharp{              ;m in-width:100px;             height:160px;             background:#6c6;             display: inline-block;             position: absolute;             line-height: 160px;             color:#FFFFFF;             font- Size:  20px;             text-align:  center ;         }         .sharp:after{             content:"";  //不需要展现文字等内容,所以设置为空字符             width:0px;             border-bottom:80px solid transparent;             border-top:80px solid transparent;             border-left-width:40px;             border-left-style: solid;             border-left-color:#6c6;             position:absolute;             right:-40px;             top:0px;         }            #sharpCont ai ner{                width:100%;                height: 600px;            }            #sharpContainer .center{                   top:200px;                left:300px;            }             #sharpContainer .top{                 top:20px;                 left:300px;             }             #sharpContainer .top-left{                 top:110px;                 left:140px;             }             #sharpContainer .top-right{                 top:110px;                 left:460px;             }             #sharpContainer .bottom{                 top:380px;                 left:300px;             }             #sharpContainer .bottom-left{                 top:290px;                 left:140px;             }             #sharpContainer .bottom-right{                 top:290px;                 left:460px;             }            </ style >    </ head >    < body >       < div   id = "sharpContainer" >         < div   class = "sharp center" >            </ div >         < div   class = "sharp top" >            </ div >         < div   class = "sharp top-left" >            </ div >         < div   class = "sharp top-right" >            </ div >         < div   class = "sharp bottom" >            </ div >         < div   class = "sharp bottom-left" >            </ div >         < div   class = "sharp bottom-right" >            </ div >    </ div >          </ body >    </ html >   

六边形绘制其实是很简单的效果,只要我们了解如何绘制三角形和使用:before,:after伪类样式即可。以后我们在项目中就可以加入更多的不规则的图形了。

以上这篇CSS3绘制六边形的简单实现就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

总结

以上是 为你收集整理的 CSS3绘制六边形的简单实现 全部内容,希望文章能够帮你解决 CSS3绘制六边形的简单实现 所遇到的问题。

如果觉得 网站内容还不错, 推荐好友。

查看更多关于CSS3绘制六边形的简单实现的详细内容...

  阅读:33次