【问题标题】:Images inside of css-based hexagons基于 css 的六边形内的图像
【发布时间】:2015-01-17 00:04:32
【问题描述】:

我正在使用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>

【问题讨论】:

标签: html css shapes css-shapes


【解决方案1】:

好吧,我不能用你制作六边形的方式来做到这一点,因为它只是一个孔束 - 一个边框(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,因为他是六边形背后的天才。我只是定位和调整大小。

干杯!

【讨论】:

  • 很好的例子。谢谢!修改multiple rows 的示例相当容易。随后的行基本上得到了它们自己的类(hexaholder2 和 even2),这样我就可以包含 top 属性(对于 hexaholder2)并将 margin-top 归零(在 even2 上)以向上移动原本会从行向下间隔的六边形在他们之上。
【解决方案2】:

通过这种 CSS 的设置方式,您可以在中心制作背景图片,但您的左右两侧仍然会有灰色三角形。 background-image: 的代码稍微复杂一些,但请查看我的小提琴以了解它是如何工作的 http://jsfiddle.net/z9Lo0gmu/

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

【讨论】:

    【解决方案3】:

    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 也是这种设计的一个很好的例子!

    【讨论】:

    • 我当然见过that exampleScottS。我会使用它,但我很难将六边形变成链接。它不像将 div 包装在 中那么简单
    • @mateikav:我匆忙忘记添加源代码!
    • @mateikav:你看到this 了吗?可能是一本非常有趣的读物!实施了一个基本样本
    猜你喜欢
    • 2020-08-13
    • 2017-07-19
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 2014-04-07
    • 2013-05-17
    相关资源
    最近更新 更多