【问题标题】:How can i create a hexagon shape with an image inside? [duplicate]如何创建内部带有图像的六边形? [复制]
【发布时间】:2015-01-12 23:01:21
【问题描述】:

我正在尝试创建一个内部带有图像的六边形

我不想把背景放在css里面,我想用html来做

我该怎么做?

http://jsfiddle.net/093hv8d1/

HTML

<div class="hexagon"><img src="http://www.edinphoto.org.uk/0_STREET/0_street_views_-_arden_street_2006_barry_nelson.jpg" width="200" height="200" /></div>

CSS

.hexagon {
  position: relative;
  width: 150px; 
  height: 86.60px;
  background-color: #64C7CC;
  margin: 43.30px 0;
  float:left;
  margin-right:10px;
}

.hexagon:before,
.hexagon:after {
  content: "";
  position: absolute;
  width: 0;
  border-left: 75px solid transparent;
  border-right: 75px solid transparent;
}

.hexagon:before {
  bottom: 100%;
  border-bottom: 43.30px solid #64C7CC;
}

.hexagon:after {
  top: 100%;
  width: 0;
  border-top: 43.30px solid #64C7CC;
}

【问题讨论】:

  • 你会使用纯色背景吗?此外,您提供的小提琴也不是很令人满意:如果您查看开发工具,您会注意到.hexagon 的大小(在 CSS 中指定)比图像小得多。
  • 对于使用 img 标签的响应式六边形网格,您可以查看stackoverflow.com/questions/26114920/…

标签: html css


【解决方案1】:

需要增加边框粗细---->border-left: 103px solid transparent;border-right: 100px solid transparent;并做一些定位调整。要显示图像的特定区域,您可以使用clip 财产。为此,您必须使用position: absolute;

clip 是如何工作的?

它创建一个矩形区域,显示元素的一部分。

值:

clip: rect(top offset, visible width, visible height, left offset)

  1. 第一个数字表示顶部偏移 - 剪辑窗口的顶部边缘。
  2. 最后一个数字表示左偏移 - 剪辑窗口的左边缘。
  3. 第二个数字是剪辑窗口的宽度加上左偏移量(最后一个数字)。
  4. 第三个数字是剪辑窗口的高度加上顶部偏移量(第一个数字)。

dabblet 上的演示

.hexagon {
    position: relative;
    top: 50px;
    width: 150px;
    height: 86.60px;
    margin: 43.30px 0;
    float:left;
    margin-right:10px;
}
img {
    position: absolute;
    clip: rect(43px,200px,157px,0px);
}
.hexagon:before, .hexagon:after {
    content:"";
    position: absolute;
    border-left: 101px solid transparent;
    border-right: 100px solid transparent;
}
.hexagon:before {
    top: 0px;
    border-bottom: 43.30px solid #64C7CC;
}
.hexagon:after {
    top: 100%;
    top: 157px;
    border-top: 43.30px solid #64C7CC;
}
#hexagons-1 {
    display: table;
    margin: 0 auto;
    margin-top: 100px;
}
#hexagons-2 {
    display: table;
    margin: 0 auto;
    margin-top:-28px;
}

【讨论】:

    【解决方案2】:

    另一种方法是使用clip-path。它可以让你覆盖整个六边形,而不仅仅是中间的正方形。

    JsFiddle

    html:

    <div class="hexagon"><img src="http://www.edinphoto.org.uk/0_STREET/0_street_views_-_arden_street_2006_barry_nelson.jpg" width="200" height="200" /></div>
    

    css:

    .hexagon{
        width: 190px;
        -webkit-clip-path: polygon( 50% 0, 100% 25%, 100% 75%, 50% 100%, 0 75%, 0 25% );
        clip-path: polygon( 50% 0, 100% 25%, 100% 75%, 50% 100%, 0 75%, 0 25% );
    }
    

    【讨论】:

      猜你喜欢
      • 2023-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-17
      • 2015-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多