我使用James Tauber's hexagon tutorial创建蜂窝式导航。我有我想要的方向,但我无法弄清楚如何将图像添加到六边形内部。 jsfiddle中的代码是否未针对将图像放入六边形内进行优化,或者我没有看到某些内容?
我意识到将图像放在六边形内还有其他问题,但是没有一个真正帮助过我。
建议将不胜感激。
HTML:
.hex {
float: left;
margin-right: -26px;
margin-bottom: -50px;
}
.hex .left {
float: left;
width: 0;
border-right: 30px solid #444;
border-top: 52px solid transparent;
border-bottom: 52px solid transparent;
}
.hex .middle {
float: left;
width: 60px;
height: 104px;
background: #444;
}
.hex .right {
float: left;
width: 0;
border-left: 30px solid #444;
border-top: 52px solid transparent;
border-bottom: 52px solid transparent;
}
.hex-row {
clear: left;
}
.hex.even {
margin-top: 53px;
}
#wrap {
min-width: 600px;
}

<div id="wrap">
<div class="hex-row">
<a href="http://www.google.ca">
<div class="hex">
<div class="left"></div>
<div class="middle"></div>
<div class="right"></div>
</div>
</a>
<div class="hex even">
<div class="left"></div>
<div class="middle"></div>
<div class="right"></div>
</div>
<div class="hex">
<div class="left"></div>
<div class="middle"></div>
<div class="right"></div>
</div>
<div class="hex even">
<div class="left"></div>
<div class="middle"></div>
<div class="right"></div>
</div>
<div class="hex">
<div class="left"></div>
<div class="middle"></div>
<div class="right"></div>
</div>
</div>
</div>
&#13;
答案 0 :(得分:4)
好吧,我不能用你制作六边形的方式来做,因为它只是一个洞 - 边框(background-image
不适用于边框)。但我找到了另一种方法,使用@geoffrey_crofte's solution on codepen。
首先,这是带有工作示例的JSFiddle。
这是更新的代码。
HTML:
<div id="wrap">
<div class="hex-row">
<a href="http://www.google.ca" class='hexaHolder'>
<div class="hexa">
<div class="hex1">
<div class="hex2">
<img src="http://farm3.staticflickr.com/2788/4392569951_8bcec3b3ed_z.jpg?zz=1" alt="" />
</div>
</div>
</div>
</a>
<a href="http://www.google.ca" class='hexaHolder even'>
<div class="hexa">
<div class="hex1">
<div class="hex2">
<img src="http://farm3.staticflickr.com/2441/3657346647_a11111ed39_z.jpg?zz=1" alt="" />
</div>
</div>
</div>
</a>
<a href="http://www.google.ca" class='hexaHolder'>
<div class="hexa">
<div class="hex1">
<div class="hex2">
<img src="http://farm3.staticflickr.com/2178/3531465579_8bff044e9b_z.jpg?zz=1" alt="" />
</div>
</div>
</div>
</a>
<a href="http://www.google.ca" class='hexaHolder even'>
<div class="hexa">
<div class="hex1">
<div class="hex2">
<img src="http://farm3.staticflickr.com/2441/3657346647_a11111ed39_z.jpg?zz=1" alt="" />
</div>
</div>
</div>
</a>
<a href="http://www.google.ca" class='hexaHolder'>
<div class="hexa">
<div class="hex1">
<div class="hex2">
<img src="http://farm4.staticflickr.com/3637/3658139504_c33433f163_z.jpg?zz=1" alt="" />
</div>
</div>
</div>
</a>
</div>
</div>
CSS:
.hexaHolder{
height: 115px;
width: 99px;
float: left;
}
.hexa img{
width:100%;
margin-top: -5px;
}
.hexa, .hexa div {
margin: 0 auto;
transform-origin: 50% 50%;
overflow: hidden;
}
.hexa {
text-align: center;
margin: 0;
width: 135px;
height: 115px;
}
.hexa div {
width: 100%;
height: 100%;
}
.hexa {
transform: rotate(120deg);
}
.hex1 {
transform: rotate(-60deg);
}
.hex2 {
transform: rotate(-60deg);
}
.hex-row {
clear: left;
}
.hexaHolder.even {
margin-top: 57.5px;
}
#wrap {
min-width:600px;
}
好的,基本上我所做的就是使用CodePen中的代码作为起始块,因为它已经将图像放在六边形内。但是,我修改了CSS一个HTML,以便你可以浮动六边形并定位它们,使它们看起来像你的例子。所有的信用都需要转到@geoffrey_crofte才能成为六角形背后的天才。我刚刚定位并调整了大小。
干杯!
答案 1 :(得分:1)
通过设置此CSS的方式,您可以在中心执行背景图像,但左侧和右侧仍然有灰色三角形。代码对于background-image:
来说有点复杂,但请查看我的小提琴以了解它是如何工作的
http://jsfiddle.net/z9Lo0gmu/
这是一个更复杂的六边形网格示例:Responsive grid of hexagons
答案 2 :(得分:0)
创建了一个快速模型:
.hex {
width: 150px;
height: 86px;
background-color: #ccc;
background-repeat: no-repeat;
background-position: 50% 50%;
background-size: auto 173px;
position: relative;
float: left;
margin: 25px 5px;
text-align: center;
zoom: 1;
}
.hex.hex-gap {
margin-left: 86px;
}
.hex a {
display: block;
width: 100%;
height: 100%;
text-indent: -9999em;
position: absolute;
top: 0;
left: 0;
}
.hex .corner-1,
.hex .corner-2 {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: inherit;
z-index: -2;
overflow: hidden;
backface-visibility: hidden;
}
.hex .corner-1 {
z-index: -1;
transform: rotate(60deg);
}
.hex .corner-2 {
transform: rotate(-60deg);
}
.hex .corner-1:before,
.hex .corner-2:before {
width: 173px;
height: 173px;
content: '';
position: absolute;
background: inherit;
top: 0;
left: 0;
z-index: 1;
background: inherit;
background-repeat: no-repeat;
backface-visibility: hidden;
}
.hex .corner-1:before {
transform: rotate(-60deg) translate(-87px, 0px);
transform-origin: 0 0;
}
.hex .corner-2:before {
transform: rotate(60deg) translate(-48px, -11px);
bottom: 0;
}
/* Custom styles*/
.hex .inner {
color: #eee;
}
.hex h4 {
font-family: 'Josefin Sans', sans-serif;
margin: 0;
}
.hex hr {
border: 0;
border-top: 1px solid #eee;
width: 60%;
margin: 15px auto;
}
.hex p {
font-size: 16px;
font-family: 'Kotta One', serif;
width: 80%;
margin: 0 auto;
}
.hex.hex-1 {
background: #74cddb;
}
.hex.hex-2 {
background: #f5c53c;
}
.hex.hex-3 {
background: #80b971;
}
<div class="hex hex-3">
<div href="#" class="inner">
<h4>CONTACT US</h4>
<hr />
<p>We Open Everyday</p>
</div>
<div class="corner-1"></div>
<div class="corner-2"></div>
</div>
<div class="hex" style="background-image: url(http://placekitten.com/g/300/300);">
<a href="#"></a>
<div class="corner-1"></div>
<div class="corner-2"></div>
</div>
是的,是的,你可以这样做。创建它们需要一些小数学,但一旦放在一起它看起来很酷!
body {
background-color: cyan;
}
.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: 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://i1166.photobucket.com/albums/q605/Artem_Lebedev/dog-training-collars.jpg);
background-position: -50px 0;
/* -left position -1 x width x sin(30) */
background-repeat: no-repeat;
background-size: auto 120%;
color: #000000;
text-align: center;
line-height: 173.2px;
/*equals height*/
display: inline-block;
}
.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)) */
}
.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: 0 0;
background-repeat: no-repeat;
background-size: auto 120%;
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: 0 0;
background-repeat: no-repeat;
background-size: auto 120%;
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;
}
.hexrow > div > span {
display: inline-block;
margin: 0 -30px;
line-height: 1.1;
vertical-align: middle;
white-space: normal;
}
.hexrow:nth-child(2) > div:nth-child(1) {
background-image: url(http://i724.photobucket.com/albums/ww244/NBAchikk1995/flowers.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://i197.photobucket.com/albums/aa231/sterling_red/landscape.jpg);
color: #ffffff;
}
.hexrow:nth-child(2) > div:nth-child(3) {
background-image: url(http://i257.photobucket.com/albums/hh204/h22prld98/2157781306_7a7a8e4cf7.jpg);
opacity: .3;
color: #ffffff;
}
.hexrow:nth-child(2) > div:nth-child(3) > div:before {
/* nothing special needed here */
}
.hexrow:nth-child(2) > div:nth-child(4) {
background-image: url(http://i916.photobucket.com/albums/ad8/paulak100/Obraz395.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 -20px;
}
.hexrow:nth-child(2) > div:nth-child(4) > div:before {
background-position: -100px -20px;
/* the left shift is always less in the pseudo elements by the amount of the base shift */
}
<div class="hexrow">
<div>
<span>Any text you would like (that fits).</span>
<div></div>
<div></div>
</div>
<div>
<span>Really, change the text to say what you want!</span>
<div></div>
<div></div>
</div>
<div>
<span>I'm not kidding here.</span>
<div></div>
<div></div>
</div>
<div>
<span>Okay?</span>
<div></div>
<div></div>
</div>
</div>
<div class="hexrow">
<div>
<span>Did I mention you can change images? I mean, really, these are just rectangular images "cropped" to a hex shape (well, more complicated than just a crop, but the result is the same).</span>
<div></div>
<div></div>
</div>
<div>
<span>See, another image!</span>
<div></div>
<div></div>
</div>
<div>
<span>Testing opacity.</span>
<div></div>
<div></div>
</div>
<div>
<span>Satisfied?</span>
<div></div>
<div></div>
</div>
</div>
Here也是这个设计的一个很好的例子!