使用CSS3生成重复的六角形图案

因此,我需要使用CSS制作重复的六角形图案。如果需要图像,我可以去那里,但是如果可能的话,我宁愿只使用CSS。

基本上,我只需要一种创建六边形形状的方法,然后将文本/图像覆盖在它们之上。我还没有太多代码,因为我不太确定从哪里开始。问题是,我只能使用<div>(http://css-tricks.com/examples/ShapesOfCSS/)中所示的六边形形状的s ,但是它们不会连接。我可以使用重复的六边形图案,但是无法指定特定形状的文本或图像的确切位置。感谢您的任何帮助。


哆啦的时光机
浏览 719回答 3
3回答

慕容森

实际上,可以使用每个六边形只有一个元素以及用于背景图像和文本的伪元素来完成此操作。演示基本的HTML结构:<div class='row'>&nbsp; &nbsp; <div class='hexagon'></div></div><div class='row'>&nbsp; &nbsp; <div class='hexagon content ribbon' data-content='This is a test!!!&nbsp;&nbsp; &nbsp; 9/10'></div><!--&nbsp; &nbsp; --><div class='hexagon content longtext' data-content='Some longer text here.&nbsp; &nbsp; &nbsp; &nbsp;Bla bla bla bla bla bla bla bla bla bla blaaaah...'></div></div>您可以有更多行,只需要n在奇数行上有六边形,在偶数行上有六边形n+/-1。相关CSS:* { box-sizing: border-box; margin: 0; padding: 0; }.row { margin: -18.5% 0; text-align: center; }.row:first-child { margin-top: 7%; }.hexagon {&nbsp; &nbsp; position: relative;&nbsp; &nbsp; display: inline-block;&nbsp; &nbsp; overflow: hidden;&nbsp; &nbsp; margin: 0 8.5%;&nbsp; &nbsp; padding: 16%;&nbsp; &nbsp; transform: rotate(30deg) skewY(30deg) scaleX(.866); /* .866 = sqrt(3)/2 */}.hexagon:before, .content:after {&nbsp; &nbsp; display: block;&nbsp; &nbsp; position: absolute;&nbsp; &nbsp; /* 86.6% = (sqrt(3)/2)*100% = .866*100% */&nbsp; &nbsp; top: 6.7%; right: 0; bottom: 6.7%; left: 0; /* 6.7% = (100% -86.6%)/2 */&nbsp; &nbsp; transform: scaleX(1.155) /* 1.155 = 2/sqrt(3) */&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;skewY(-30deg) rotate(-30deg);&nbsp; &nbsp; background-color: rgba(30,144,255,.56);&nbsp; &nbsp; background-size: cover;&nbsp; &nbsp; content: '';}.content:after { content: attr(data-content); }/* add background images to :before pseudo-elements */.row:nth-child(n) .hexagon:nth-child(m):before {&nbsp; &nbsp; background-image:&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; url(background-image-mxn.jpg);&nbsp;}

MM们

我将提供有关如何创建六边形形状的简单演示。.hex {&nbsp; width: 40px;&nbsp; height: 70px;&nbsp; margin: 20px;&nbsp; overflow: hidden;}.hex:before {&nbsp; content: "";&nbsp; transform: rotate(45deg);&nbsp; background: #f00;&nbsp; width: 50px;&nbsp; height: 50px;&nbsp; display: inline-block;&nbsp; margin: 10px -5px 10px -5px;}<div class="hex"></div>
打开App,查看更多内容
随时随地看视频慕课网APP