我正在尝试制作六边形网格,我需要在这些六边形上面添加一些文字。我尝试了不同的东西但没有任何效果。
我还尝试在它上面放置一个背景图像(带有数字),但我也无法在其上放置图像。
HTML / CSS
.container {
width: 1000px;
line-height: 1.3;
margin-left: 30%;
}
ol.even {
position: relative;
left: 3.63em;
}
ol.odd {
position: relative;
margin-top: -6.5%;
margin-bottom: -6.5%;
}
.hex {
position: relative;
margin: 1em auto;
width: 4em;
height: 6.8em;
border-radius: 1em/.5em;
background: #ccc;
transform: rotate(-90deg);
display: inline-block;
margin-right: 3em;
transition: all 150ms ease-in-out;
}
.hex:before, .hex:after {
position: absolute;
width: inherit; height: inherit;
border-radius: inherit;
background: inherit;
content: '';
}
.hex:before {
transform: rotate(60deg);
}
.hex:after {
transform: rotate(-60deg);
}
.hex:hover {
background: #F58787;
cursor: pointer;
}
#hexMidden:hover {
background: #ccc;
cursor: auto;
}
<body>
<div class="container">
<ol class="even">
<li class='hex'></li>
<li class='hex'></li>
</ol>
<ol class="odd">
<li class='hex'></li>
<li class='hex' id="hexMidden"></li>
<li class='hex'></li>
</ol>
<ol class="even">
<li class='hex'></li>
<li class='hex'></li>
</ol>
</div>
</body>
我在互联网上找到的上述代码。
答案 0 :(得分:1)
这有点令人费解,但是因为你为六边形指定了一个高度,所以可以用几个跨度来做到这一点:
.hex > span {
position: absolute;
top: 0;
left: 0;
width: 100%;
line-height: 6.8em;
transform: rotate(90deg);
text-align: center;
z-index: 20;
}
.hex > span > span {
display: inline-block;
vertical-align: middle;
line-height: 1em;
}
这实际上是在创建一个与六边形高度相同的文本区域并撤消旋转,从而使文本正确显示。 z-index
将它抛到Hexagon的其余部分之上,text-align: center
将文本整齐地放在它添加到的任何十六进制的中间。可悲的是,这两个跨度是必要的,以确保不止一行文本不会破坏效果。
你说你想要数字,所以可以通过在第一个子跨度中添加white-space: nowrap
来为每个文本一个范围执行此操作,我将为该版本创建一个单独的小提琴。 / p>
Fiddle for single-line numbers
希望这有帮助!
答案 1 :(得分:1)
.hex-text {
position: absolute;
transform: rotate(90deg);
z-index: 999;
left: 5px;
top: 30px;
}
<li class='hex'>
<div class="hex-text">Words words words</div>
</li>
<强> Demo 强>
.container {
width: 1000px;
line-height: 1.3;
margin-left: 30%;
}
ol.even {
position: relative;
left: 3.63em;
}
ol.odd {
position: relative;
margin-top: -6.5%;
margin-bottom: -6.5%;
}
.hex {
position: relative;
margin: 1em auto;
width: 4em;
height: 6.8em;
border-radius: 1em/.5em;
background: #ccc;
transform: rotate(-90deg);
display: inline-block;
margin-right: 3em;
transition: all 150ms ease-in-out;
}
.hex:before, .hex:after {
position: absolute;
width: inherit; height: inherit;
border-radius: inherit;
background: inherit;
content: '';
}
.hex:before {
transform: rotate(60deg);
}
.hex:after {
transform: rotate(-60deg);
}
.hex:hover {
background: #F58787;
cursor: pointer;
}
#hexMidden:hover {
background: #ccc;
cursor: auto;
}
.hex-text {
position: absolute;
transform: rotate(90deg);
z-index: 999;
left: 5px;
top: 30px;
}
<body>
<div class="container">
<ol class="even">
<li class='hex'><div class="hex-text">Words words words</div></li>
<li class='hex'><div class="hex-text">Words words words</div></li>
</ol>
<ol class="odd">
<li class='hex'><div class="hex-text">Words words words</div></li>
<li class='hex'><div class="hex-text">Words words words</div></li>
<li class='hex'><div class="hex-text">Words words words</div></li>
</ol>
<ol class="even">
<li class='hex'><div class="hex-text">Words words words</div></li>
<li class='hex'><div class="hex-text">Words words words</div></li>
</ol>
</div>
</body>
答案 2 :(得分:1)
或者只是添加到您的CSS:
.number {
float: left;
position: fixed;
z-index: 1;
margin-top: 60px;
margin-left: -85px;
}
将您的HTML更改为:
<div class="container">
<ol class="even">
<li class='hex'></li><span class="number">1</span>
<li class='hex'></li><span class="number">2</span>
</ol>
<ol class="odd">
<li class='hex'></li><span class="number">3</span>
<li class='hex' id="hexMidden"></li><span class="number">4</span>
<li class='hex'></li><span class="number">5</span>
</ol>
<ol class="even">
<li class='hex'></li><span class="number">6</span>
<li class='hex'></li><span class="number">7</span>
</ol>
</div>
工作演示:http://jsfiddle.net/3t4680ve/
.container {
width: 1000px;
line-height: 1.3;
margin-left: 30%;
}
ol.even {
position: relative;
left: 3.63em;
}
ol.odd {
position: relative;
margin-top: -6.5%;
margin-bottom: -6.5%;
}
.hex {
position: relative;
margin: 1em auto;
width: 4em;
height: 6.8em;
border-radius: 1em/.5em;
background: #ccc;
transform: rotate(-90deg);
display: inline-block;
margin-right: 3em;
transition: all 150ms ease-in-out;
}
.hex:before, .hex:after {
position: absolute;
width: inherit; height: inherit;
border-radius: inherit;
background: inherit;
content: '';
}
.hex:before {
transform: rotate(60deg);
}
.hex:after {
transform: rotate(-60deg);
}
.hex:hover {
background: #F58787;
cursor: pointer;
}
#hexMidden:hover {
background: #ccc;
cursor: auto;
}
.number {
float: left;
position: fixed;
z-index: 1;
margin-top: 60px;
margin-left: -85px;
}
&#13;
<div class="container">
<ol class="even">
<li class='hex'></li><span class="number">1</span>
<li class='hex'></li><span class="number">2</span>
</ol>
<ol class="odd">
<li class='hex'></li><span class="number">3</span>
<li class='hex' id="hexMidden"></li><span class="number">4</span>
<li class='hex'></li><span class="number">5</span>
</ol>
<ol class="even">
<li class='hex'></li><span class="number">6</span>
<li class='hex'></li><span class="number">7</span>
</ol>
</div>
&#13;