【问题标题】:html/css hexagon with image insidehtml/css 六边形,里面有图片
【发布时间】:2011-11-18 00:10:23
【问题描述】:

是否有机会将图像放置在六边形中? 我习惯了hexagonal shaped cells in html,但我无法用(背景?)图像填充它。

这是我尝试过的:

.top {
  height: 0;
  width: 0;
  display: block;
  border: 20px solid red;
  border-top-color: transparent;
  border-right-color: transparent;
  border-bottom-color: red;
  border-left-color: transparent;
}
.middle {
  height: 20px;
  background: red;
  width: 40px;
  display: block;
}
.bottom {
  height: 0;
  width: 0;
  display: block;
  border: 20px solid red;
  border-top-color: red;
  border-right-color: transparent;
  border-bottom-color: transparent;
  border-left-color: transparent;
}
<div class="hexagon pic">
  <span class="top" style="background: url(http://placekitten.com/400/400/)"></span>
  <span class="middle" style="background: url(http://placekitten.com/400/400/)"></span>
  <span class="bottom" style="background: url(http://placekitten.com/400/400/)"></span>
</div>


<div class="hexagon">
  <span class="top" style="overflow: hidden;"><img src="http://placekitten.com/400/400/" /></span>
  <span class="middle" style="overflow: hidden;"><img src="http://placekitten.com/400/400/" /></span>
  <span class="bottom" style="overflow: hidden;"><img src="http://placekitten.com/400/400/" /></span>
</div>

<div class="hexagon">
  <span class="top"><img src="http://placekitten.com/400/400/" /></span>
  <span class="middle"><img src="http://placekitten.com/400/400/" /></span>
  <span class="bottom"><img src="http://placekitten.com/400/400/" /></span>
</div>

这是一个小提琴:http://jsfiddle.net/jnz31/kGSCA/

【问题讨论】:

标签: html css svg css-shapes


【解决方案1】:

使用 CSS3 几乎一切皆有可能:http://jsfiddle.net/kizu/bhGn4/

我对@9​​87654323@ 使用了不同的旋转,因此您可以获得一个跨浏览器(嗯,现代跨浏览器)遮罩,甚至可以在遮罩区域上覆盖和点击。

【讨论】:

  • 我认为我是 html/css 方面的专家(一点也不难),但我没有得到你在那里所做的任何事情。谢谢!
  • 太棒了...但是为六边形添加边框怎么样?
  • 嘿,我注意到 Chrome 中的边缘不平滑,我添加了一个修复:jsfiddle.net/bhGn4/1733
  • 这真是太棒了!如果这个 -ms-transform: rotate(120deg); 在 -60deg 中是错误的或者必须是这样的,请告诉我?
【解决方案2】:

用图像实现六边形最灵活的方法是使用内联 SVG

svg{
  width:30%;
}
<svg viewBox="0 0 100 100" version="1.1" xmlns="http://www.w3.org/2000/svg">
  <defs>
    <pattern id="img" patternUnits="userSpaceOnUse" width="100" height="100">
      <image xlink:href="https://farm9.staticflickr.com/8461/8048823381_0fbc2d8efb.jpg" x="-25" width="150" height="100" />
    </pattern>
  </defs>
  <polygon points="50 1 95 25 95 75 50 99 5 75 5 25" fill="url(#img)"/>
</svg>

至少有两种方法可以用 SVG 实现六边形图像:

  • 制作一个六边形多边形并用图像和pattern 元素填充多边形(我在之前的 sn-p 中使用的方法)
  • 用六边形多边形剪裁图像(参见下一个 sn-p)

svg{width:30%}
<svg viewbox="0 0 100 100" version="1.1" xmlns="http://www.w3.org/2000/svg">
  <defs>
    <clipPath id="hexClip">
      <polygon points="50 1 99 25 99 75 50 99 1 75 1 25"/>
    </clipPath>
  </defs>  
  <image xlink:href="https://farm4.staticflickr.com/3165/5733278274_2626612c70.jpg" x="-25" width="150" height="100" clip-path="url(#hexClip)"/>
</svg>

SVG 方法允许控制六边形形状和图像的许多方面。它们可以用 CSS 设置样式。这是一个例子:

svg{
  width:30%;
  margin:0 auto;
}
#hex{
  stroke-width:2;
  stroke: teal;
  fill-opacity:0.6;
  transition:fill-opacity .8s;
}
svg:hover #hex {
  fill-opacity:1;
}
#text{
  stroke-width:0.5;
  stroke:teal;
  fill-opacity:0.4;
  fill:teal;
  transition:fill-opacity .8s;
}
svg:hover #text{
  fill-opacity:1;
}
<svg viewbox="0 0 100 100" version="1.1" xmlns="http://www.w3.org/2000/svg">
  <defs>
    <pattern id="img" patternUnits="userSpaceOnUse" width="100" height="100">
      <image xlink:href="https://farm9.staticflickr.com/8461/8048823381_0fbc2d8efb.jpg" x="-25" width="150" height="100" />
    </pattern>
  </defs>
  <polygon id="hex" points="50 1 95 25 95 75 50 99 5 75 5 25" fill="url(#img)"/>
  <text id="text" font-size="20" x="50" y="50" text-anchor="middle">Some text</text>
</svg>

对于另一种制作带有图像的六边形的方法,请检查以下问题:Responsive grid of hexagons

【讨论】:

  • 不错的答案。但有一件事让我很困惑。使用199 坐标不是0100 有什么特殊原因吗?
  • @MuhammadUsman 是为了防止 svg 元素剪切笔画
  • 好的,非常感谢。
  • 如何放大这个东西?现在,如果我只是将图像放大,它似乎并没有完全填满盒子。
  • @JCharette 只是让 svg 的宽度变大/变小。
【解决方案3】:

一种新的简单方法是使用 css3 clip-path 属性。

来自Documentation

clip-path CSS 属性可防止元素的一部分 通过定义要显示的剪辑区域来显示,即 仅显示元素的特定区域。

下面的css会显示一个六边形的矩形元素:

div.hexagon {
  clip-path: polygon(50% 0, 100% 25%, 100% 75%, 50% 100%, 0 75%, 0 25%);
}

输出图像:

body {
  background: linear-gradient(orange, yellow);
  min-height: 100vh;
  margin: 0;
}
.hexagon {
  clip-path: polygon(50% 0, 100% 25%, 100% 75%, 50% 100%, 0 75%, 0 25%);
  background: url("https://i.imgur.com/waDgcnc.jpg") no-repeat;
  background-size: cover;
  margin: 10px auto;
  height: 200px;
  width: 200px;      
}
<div class="hexagon">
  
</div>

我们可以使用这个属性来绘制我们想要的任何形状。下面是几个例子:

div.pentagon {
  clip-path: polygon(50% 0, 100% 45%, 80% 100%, 20% 100%, 0 45%);
}
div.octagon {
  clip-path: polygon(33.33% 0%, 66.66% 0%, 100% 33.33%, 100% 66.66%, 66.66% 100%, 33.33% 100%, 0 66.66%, 0 33.33%);
}

输出图像:

body {
  background: linear-gradient(orange, yellow);
  min-height: 100vh;
  margin: 0;
}
div {
  background: url("https://i.imgur.com/waDgcnc.jpg") no-repeat;
  background-size: cover;
  margin: 10px 20px;
  height: 170px;
  width: 170px;
  float: left;
}

.pentagon {
  clip-path: polygon(50% 0, 100% 45%, 80% 100%, 20% 100%, 0 45%);
}

.octagon {
  clip-path: polygon(33.33% 0%, 66.66% 0%, 100% 33.33%, 100% 66.66%, 66.66% 100%, 33.33% 100%, 0 66.66%, 0 33.33%);
}
<div class="pentagon">
  
</div>
<div class="octagon">
  
</div>

注意: IE 和 Edge 不支持 clip-path css 属性。 但是,Edge 的未来版本预计将支持此功能 属性。

【讨论】:

【解决方案4】:

你可以通过像这样覆盖角落来做到这一点:

http://jsfiddle.net/Eric/kGSCA/3/

HTML:

<div class="hexagon pic">
    <span class="top"></span>
    <span class="bottom"></span>
</div>

CSS:

.hexagon {
    background: url(http://placekitten.com/400/400/);
    width: 400px;
    height: 346px;
    position: relative;
}

.hexagon span {
    position: absolute;
    display: block;
    border-left: 100px solid red;
    border-right: 100px solid red;
    width: 200px;
}

.top {
    top: 0;
    border-bottom: 173px solid transparent;
}

.bottom {
    bottom: 0;
    border-top: 173px solid transparent;
}

【讨论】:

  • 也喜欢这个。在较旧的浏览器上工作。 :)
【解决方案5】:

如果您只需要放置一张图片,这是一种简单的方法。

<style>
.hex{
    width:80px;
    height:80px;
    background-image: url(http://placekitten.com/400/400/);
    background-size: cover;
    position:relative;
    margin:10px;
}
.hex:before, .hex:after{
    content:"";
    position:absolute;
    top:0px;height:40px;width:0px; /* 40 = height/2 */
    z-index:1;
    border:20px solid #FFF; /*change #FFF to your bg color*/
}
.hex:before{
    left:-20px; /* -1*borderWidth */
    border-right:40px solid transparent;/* width/2 */
}
.hex:after{
    left:40px; /* width/2 */
    border-left:40px solid transparent;/* width/2 */
}
</style>
<div class="hex"></div>

需要边框吗?背景 img 会更简单、更快捷。

<div class="hex">
    <div style="position:absolute;top:-20px;left:-20px;bottom:-20px;right:-20px;
        z-index:2;background-image:url(/images/hexagon.png);">
    </div>
</div>

【讨论】:

  • 我喜欢这个,因为它在一定程度上适用于旧版浏览器,但无论如何也不完全适用。 :)
【解决方案6】:

我不知道这是否是您正在寻找的答案,但您可以在您想要成为六边形的图像上放置一个六边形透明 .png 并让它像面具一样。

只需用 z-index 将透明 png 放在图像上

【讨论】:

    【解决方案7】:

    我认为没有任何外部图形的纯 HTML/CSS 没有办法做到这一点。使用您在问题中链接到的技术可能会有一些聪明的黑客攻击,但它们只是 - 聪明的黑客攻击 - 所以可能不适合在实时网站中使用(并且与大多数“聪明的黑客攻击”一样,也可能至少存在一些跨浏览器兼容性问题)。

    您可以使用 Canvas 或 SVG 来实现。

    Canvas 是一种位图图形功能,是 HTML5 规范的一部分。 SVG 是一种矢量图形语言,可以在 HTML 页面中使用。

    这两个都是现代浏览器的功能,因此遗憾的是大多数版本的 Internet Explorer(IE8 和更早版本)都缺少这些功能。

    不过,幸运的是,IE 确实支持一种类似于 SVG 的称为 VML 的语言,并且有许多 javacript 库允许 IE 通过将 Canvas 和 SVG 转换为 VML 来同时使用它们。

    另见:

    使用上面链接的任何工具,您可以使用 Canvas 或 SVG 绘制六边形(或任何其他)形状,并用您的图形填充它。

    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 2023-03-20
      • 2017-07-19
      • 1970-01-01
      • 1970-01-01
      • 2020-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多