基于 css 的六边形内部的图像

Images inside of css-based hexagons

我使用的 James Tauber's hexagon tutorial to create a honeycomb-styled navigation. I have the orientation that I want, but I am having trouble figuring out how to add images to the inside of the hexagons. Is the code in the jsfiddle 未针对将图像放入六边形进行优化,还是我没有看到什么?

我意识到关于将图像放入六边形内还有其他问题,但 none 确实帮助了我。

不胜感激。

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>

按照此 CSS 的设置方式,您可以在中心制作背景图片,但左右两侧仍然会有灰色三角形。 background-image: 的代码有点复杂,但请查看我的 fiddle 以了解其工作原理 http://jsfiddle.net/z9Lo0gmu/

这是一个更复杂的六边形网格示例:Responsive grid of hexagons

好吧,好吧,我不能用你制作六边形的方法来做,因为它只是一堆孔 - 一个边界(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 a HTML 以便您可以浮动六边形并定位它们,使它们看起来像您的示例。所有的功劳都需要归功于@geoffrey_crofte,因为他是六边形背后的天才。我刚刚定位并调整了大小。

干杯!

Tutorial

创建的快速模型:

.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>

~SOURCE


Here也是这种设计的一个很好的例子!