.hexagon-gallery {
    margin: auto;
    margin-top: 50px;
    max-width: 1000px;
    display: grid;
    grid-template-columns: repeat(8, 1fr);
    grid-auto-rows: 200px;
    padding-bottom: 50px;
  }
  
  .hex {
    display: flex;
    position: relative;
    width: 240px;
    height: 265px;
    background-color: #424242;
    -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%);
  }
  
  .hex:first-child {
    grid-row-start: 1;
    grid-column: 3 / span 2;
  }
  
  .hex:nth-child(2) {
    grid-row-start: 1;
    grid-column: 4 / span 2;
  }
  
  .hex:nth-child(3) {
    grid-row-start: 1;
    grid-column: 6 / span 2;
  }
  
       <section class="hexagon-gallery">
            <div class="hex"><img src="https://images.pexels.com/photos/1421264/pexels-photo-1421264.jpeg?cs=srgb&dl=aerial-aerial-photo-aerial-photography-1421264.jpg&fm=jpg" alt="some"></div>
            <div class="hex"></div>
            <div class="hex"></div>
          </section>
      

enter image description here

我想找个形状相似的东西.六边形拉伸排印形状,idk如果有任何其他更简单的方法来做到这一点.也许两边都成形的矩形可以用,我不确定.

推荐答案

这样行吗?我稍微整理了一下代码,以删除无关的CSS属性.

主要部分有:

  • clip-path: polygon(85% 0%, 100% 50%, 85% 100%, 15% 100%, 0% 50%, 15% 0%);以获得正确的形状.
  • grid-auto-flow: column;grid-auto-columns: 168px;(这是您的.hex宽度-30%,因为形状每侧切割15%),以正确对齐项目`
  • z-index应按递减顺序排列,以便每个项目始终覆盖下一个项目.如果删除z-index,则会产生相反的效果(每个元素都被下一个元素覆盖),在HTML中,默认情况下,同级组底部的元素会显示在其上方的元素的"上方".
  • 仅适用于tidyness grid-auto-rows: 0px;,它取代了每个元素上的grid-row-start: 1;.

.hexagon-gallery {
  display: grid;
  grid-auto-rows: 0px;
  grid-auto-columns: 168px;
  grid-auto-flow: column;
}

.hex {
  display: flex;
  width: 240px;
  height: 265px;
  clip-path: polygon(85% 0%, 100% 50%, 85% 100%, 15% 100%, 0% 50%, 15% 0%);
}

.hex:first-child {
  background: red;
  z-index: 1000;
}

.hex:nth-child(2) {
  background: green;
  z-index: 900;
}

.hex:nth-child(3) {
  background: blue;
  z-index: 800;
}
<section class="hexagon-gallery">
  <div class="hex"><img src="https://images.pexels.com/photos/1421264/pexels-photo-1421264.jpeg?cs=srgb&dl=aerial-aerial-photo-aerial-photography-1421264.jpg&fm=jpg" alt="some"></div>
  <div class="hex"></div>
  <div class="hex"></div>
</section>

To tweak the shape, modify the 85% and 15% (they should always add up to 100 if you want to keep a symmetric shape).
For example putting 75% and 25% instead will make the angle bigger.
Don't forget to change the grid-auto-columns value to width * (25*2)%, so 120px in this case to keep the correct spacing between each element.
Example with these parameters: enter image description here

Javascript相关问答推荐

如何在不指定宽度和高度的情况下显示来自网址的下一张图像

JavaScript Date对象在UTC中设置为午夜时显示不正确的日期

在HTML中使用脚本变量作为背景图像源

Flisk和JS错误:未捕获的Syntax错误:意外的令牌'<'

如何按预期聚合SON数据?

为什么JavaScript双边字符串文字插值不是二次的?

React Hooks中useState的同步问题

获取表格的左滚动位置

根据总价格对航班优惠数组进行排序并检索前五个结果- Angular HTTP请求

如何分配类型脚本中具有不同/额外参数的函数类型

使用axios.获取实时服务器时的404响应

Angular 中的类型错误上不存在获取属性

如何在ASP.NET JavaScript中使用Google Charts API仅对绘制为负方向的条形图移动堆叠条形图标签位置

使用ThreeJ渲染的形状具有抖动/模糊的边缘

提交链接到AJAX数据结果的表单

TypeError:无法分解';React2.useContext(...)';的属性';basename';,因为它为空

扩展类型的联合被解析为基类型

AddEventListner,按键事件不工作

FireBase云函数-函数外部的ENV变量

使用API调用的VUE 3键盘输入同步问题