(虽然安娜的回答比我晚了几个月,可能以我的作为“思考”的基础,但她能够使用单个div 提出一种方法这一事实值得推广,所以@987654321 @--但请注意,十六进制中的内容更有限。)
这真是一个了不起的问题。谢谢你问。最棒的是:
Original Fiddle Used(在稍后的编辑中修改为上面的小提琴链接)——它使用了 imgur.com 图像,这些图像在加载时似乎不是很可靠,所以新的小提琴正在使用 photobucket.com(let我知道是否存在持续的图像加载问题)。我保留了原始链接,因为下面的解释代码与此相关(background-size 或 position 与新小提琴有一些差异)。
阅读您的问题后,我几乎立即想到了这个想法,但需要一些时间来实施。我最初尝试使用单个 div 和仅伪元素获取单个“十六进制”,但据我所知,没有办法只旋转 background-image(我需要),所以我不得不添加一些额外的div 元素来获取十六进制的右侧/左侧,这样我就可以使用伪元素作为background-image 旋转的手段。
我在 IE9、FF 和 Chrome 中进行了测试。理论上任何支持 CSS3 transform 的浏览器都应该在其中工作。
第一次主要更新(添加说明)
我现在有时间发布一些代码解释,所以这里是:
首先,六边形是由 30/60 度关系和三角函数定义的,因此这些将是涉及的关键角度。其次,我们从十六进制网格所在的“行”开始。HTML 定义为(额外的div 元素有助于构建十六进制):
<div class="hexrow">
<div>
<span>First Hex Text</span>
<div></div>
<div></div>
</div>
<div>
<span>Second Hex Text</span>
<div></div>
<div></div>
</div>
<div>
<span>Third Hex Text</span>
<div></div>
<div></div>
</div>
</div>
我们将使用inline-block 表示六边形display,但我们不希望它们意外换行并破坏网格,因此white-space: nowrap 解决了这个问题。此行上的margin 将取决于您想要的十六进制之间的空间,并且可能需要进行一些实验才能得到您想要的。
.hexrow {
white-space: nowrap;
/*right/left margin set at (( width of child div x sin(30) ) / 2)
makes a fairly tight fit;
a 3px bottom seems to match*/
margin: 0 25px 3px;
}
使用.hexrow 的直接子元素,它们只是div 元素,我们形成了十六进制形状的基础。 width 将驱动六边形顶部的水平方向,height 来自该数字,因为在正六边形上所有边的长度都相等。同样,边距将取决于间距,但这是各个六边形的“重叠”将发生的地方,以使网格看起来出现。 background-image 定义一次,就在这里。左移是为了至少容纳十六进制左侧的附加宽度。假设您想要居中文本,text-align 处理水平(当然),但匹配 height 的 line-height 将允许垂直居中。
.hexrow > div {
width: 100px;
height: 173.2px; /* ( width x cos(30) ) x 2 */
/* For margin:
right/left = ( width x sin(30) ) makes no overlap
right/left = (( width x sin(30) ) / 2) leaves a narrow separation
*/
margin: 0 25px;
position: relative;
background-image: url(http://i.imgur.com/w5tV4.jpg);
background-position: -50px 0; /* -left position -1 x width x sin(30) */
background-repeat: no-repeat;
color: #ffffff;
text-align: center;
line-height: 173.2px; /*equals height*/
display: inline-block;
}
每个奇数 十六进制数我们将相对于“行”向下移动,每个偶数 向上移动。移位计算(width x cos(30) / 2)也和(height / 4)一样。
.hexrow > div:nth-child(odd) {
top: 43.3px; /* ( width x cos(30) / 2 ) */
}
.hexrow > div:nth-child(even) {
top: -44.8px; /* -1 x( ( width x cos(30) / 2) + (hexrow bottom margin / 2)) */
}
我们正在使用 2 个子 div 元素来创建十六进制的“翅膀”。它们的大小与主六角矩形相同,然后旋转并推到主六角“下方”。 Background-image 被继承,因此图像是相同的(当然),因为“翅膀”中的图像将与主矩形中的图像“对齐”。伪元素用于生成图像,因为它们需要“重新旋转”回水平(因为我们旋转了它们的父级 div 以创建“翅膀”)。
第一个的:before 会将其背景转换为负数的宽度,等于十六进制的主要部分加上主十六进制的原始背景偏移。第二个的:before 会改变平移的原点,并且会在x 轴上移动主宽度,在y 轴上移动一半高度。
.hexrow > div > div:first-of-type {
position: absolute;
width: 100%;
height: 100%;
top: 0;
left: 0;
z-index: -1;
overflow: hidden;
background-image: inherit;
-ms-transform:rotate(60deg); /* IE 9 */
-moz-transform:rotate(60deg); /* Firefox */
-webkit-transform:rotate(60deg); /* Safari and Chrome */
-o-transform:rotate(60deg); /* Opera */
transform:rotate(60deg);
}
.hexrow > div > div:first-of-type:before {
content: '';
position: absolute;
width: 200px; /* width of main + margin sizing */
height: 100%;
background-image: inherit;
background-position: top left;
background-repeat: no-repeat;
bottom: 0;
left: 0;
z-index: 1;
-ms-transform:rotate(-60deg) translate(-150px, 0); /* IE 9 */
-moz-transform:rotate(-60deg) translate(-150px, 0); /* Firefox */
-webkit-transform:rotate(-60deg) translate(-150px, 0); /* Safari and Chrome */
-o-transform:rotate(-60deg) translate(-150px, 0); /* Opera */
transform:rotate(-60deg) translate(-150px, 0);
-ms-transform-origin: 0 0; /* IE 9 */
-webkit-transform-origin: 0 0; /* Safari and Chrome */
-moz-transform-origin: 0 0; /* Firefox */
-o-transform-origin: 0 0; /* Opera */
transform-origin: 0 0;
}
.hexrow > div > div:last-of-type {
content: '';
position: absolute;
width: 100%;
height: 100%;
top: 0;
left: 0;
z-index: -2;
overflow: hidden;
background-image: inherit;
-ms-transform:rotate(-60deg); /* IE 9 */
-moz-transform:rotate(-60deg); /* Firefox */
-webkit-transform:rotate(-60deg); /* Safari and Chrome */
-o-transform:rotate(-60deg); /* Opera */
transform:rotate(-60deg);
}
.hexrow > div > div:last-of-type:before {
content: '';
position: absolute;
width: 200px; /* starting width + margin sizing */
height: 100%;
background-image: inherit;
background-position: top left;
background-repeat: no-repeat;
bottom: 0;
left: 0;
z-index: 1;
/*translate properties are initial width (100px) and half height (173.2 / 2 = 86.6) */
-ms-transform:rotate(60deg) translate(100px, 86.6px); /* IE 9 */
-moz-transform:rotate(60deg) translate(100px, 86.6px); /* Firefox */
-webkit-transform:rotate(60deg) translate(100px, 86.6px); /* Safari and Chrome */
-o-transform:rotate(60deg) translate(100px, 86.6px); /* Opera */
transform:rotate(60deg) translate(100px, 86.6px);
-ms-transform-origin: 100% 0; /* IE 9 */
-webkit-transform-origin: 100% 0; /* Safari and Chrome */
-moz-transform-origin: 100% 0; /* Firefox */
-o-transform-origin: 100% 0; /* Opera */
transform-origin: 100% 0;
}
这个span 包含您的文本。 line-height 被重置以使文本行正常,但 vertical-align: middle 有效,因为 line-height 在父级上更大。 white-space 被重置,因此它允许再次包装。可以将左右边距设置为负数,以允许文本进入十六进制的“翅膀”。
.hexrow > div > span {
display: inline-block;
margin: 0 -30px;
line-height: 1.1;
vertical-align: middle;
white-space: normal;
}
您可以单独定位行和这些行中的单元格以更改图像,或span 文本设置,或不透明度,或容纳更大的图像(将其移动到您想要的位置)等。这就是以下内容为第二行做。
.hexrow:nth-child(2) > div:nth-child(1) {
background-image: url(http://i.imgur.com/7Un8Y.jpg);
}
.hexrow:nth-child(2) > div:nth-child(1) > span {
/*change some other settings*/
margin: 0 -20px;
color: black;
font-size: .8em;
font-weight: bold;
}
.hexrow:nth-child(2) > div:nth-child(2) {
background-image: url(http://i.imgur.com/jeSPg.jpg);
}
.hexrow:nth-child(2) > div:nth-child(3) {
background-image: url(http://i.imgur.com/Jwmxm.jpg);
/*you can shift a large background image, but it can get complicated
best to keep the image as the total width (200px) and height (174px)
that the hex would be.
*/
background-position: -150px -120px;
opacity: .3;
color: black;
}
.hexrow:nth-child(2) > div:nth-child(3) > div:before {
/*you can shift a large background image, but it can get complicated
best to keep the image as the total width (200px) and height (174px)
that the hex would be.
*/
background-position: -100px -120px; /* the left shift is always less in the pseudo elements by the amount of the base shift */
}
.hexrow:nth-child(2) > div:nth-child(4) {
background-image: url(http://i.imgur.com/90EkV.jpg);
background-position: -350px -120px;
}
.hexrow:nth-child(2) > div:nth-child(4) > div:before {
background-position: -300px -120px;
}