You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何保持现有HTML结构将div集合设置为树形金字塔布局?

如何保持现有HTML结构将div集合设置为树形金字塔布局?

嘿,我来帮你搞定这个金字塔布局的需求!你想要的结构是第3个slot单独顶在最上方,下面一行是第1、2个slot,最底部一行是第4、5、6个slot,而且完全保留现有HTML结构对吧?之前的代码里固定width:370px导致布局不灵活,我们可以通过调整Grid布局来完美实现这个金字塔形状,还能保证响应式效果。

核心思路:用Grid列分配实现居中金字塔

我们给容器设置6列的等宽网格,然后让每个slot占据对应的列数,从而形成上窄下宽的金字塔结构:

  • 最顶部的第3个slot:占据中间2列,单独一行,实现居中
  • 中间行的第1、2个slot:各占中间的2列,并排显示
  • 最底部的第4、5、6个slot:各占2列,铺满整个容器宽度

修改后的完整CSS代码

.slots-container {
  display: grid;
  gap: 0.3125rem;
  /* 设置6列等宽网格,适配不同屏幕 */
  grid-template-columns: repeat(6, 1fr);
  margin: 1px;
  overflow-x: hidden;
}

/* 给每个slot分配对应的网格位置 */
.slot:nth-child(3) {
  /* 第3个slot占第3到第5列(左闭右开),单独一行 */
  grid-column: 3 / 5;
}
.slot:nth-child(1) {
  /* 第1个slot占第2到第4列 */
  grid-column: 2 / 4;
}
.slot:nth-child(2) {
  /* 第2个slot占第4到第6列 */
  grid-column: 4 / 6;
}
.slot:nth-child(4) {
  /* 第4个slot占第1到第3列 */
  grid-column: 1 / 3;
}
.slot:nth-child(5) {
  /* 第5个slot占第3到第5列 */
  grid-column: 3 / 5;
}
.slot:nth-child(6) {
  /* 第6个slot占第5到第7列 */
  grid-column: 5 / 7;
}

/* 保留你原来的视觉样式,去掉固定width让Grid自动分配宽度 */
.slot {
  min-height: 15.2rem;
  background-color: Silver;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 3vw;
  padding: 5px 1px;
  position: relative;
  cursor: pointer;
  transition: background-color 0.3s ease;
  border-radius: 20px;
  height: 15.2rem;
  /* 去掉固定width,让Grid根据列数自动分配 */
}

/* 保留你原来的渐变背景样式 */
.slot:nth-child(3) {
  background: linear-gradient(135deg, #b6d7a8, #ffd3b6);
}
.slot:nth-child(2),
.slot:nth-child(1) {
  background: linear-gradient(135deg, #c7b8e1, #ffe0e0);
}
.slot:nth-child(4),
.slot:nth-child(5),
.slot:nth-child(6) {
  background: linear-gradient(135deg, #a8cbd4, #ffe6b6);
}

/* 保留你原来的伪元素、hover等其他样式 */
.slot:hover {
  background-color: #f0f0f0;
}
.slot::before {
  content: "Drop Expert Card Here";
  position: absolute;
  top: 1px;
  left: 1px;
  width: 100%;
  height: 40px;
  background-color: #ffd700;
  color: black;
  font-size: 14px;
  font-weight: bold;
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: inherit;
  border-bottom: 1px solid dimgrey;
  margin-bottom: 5px;
}
.slot:nth-child(3)::before {
  content: "خبير صناعة فئة-أ";
  background-color: #ffd700;
  text-align: center;
}
.slot:nth-child(2)::before,
.slot:nth-child(1)::before {
  content: "خبير صناعة فئة-ب";
  background-color: #c0c0c0;
  text-align: center;
}
.slot:nth-child(4)::before,
.slot:nth-child(5)::before,
.slot:nth-child(6)::before {
  content: "خبير صناعة فئة-ج";
  background-color: #cd7f32;
  text-align: center;
}

/* 其他原有样式保留 */
#temp_emp {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 280px;
  background-color: #f9f9f9;
  border: 2px dashed #ccc;
}
.placeholder1 {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 5px;
  text-align: center;
  border: 2px dashed #999;
  background-color: #f9f9f9;
}
.placeholder-text {
  color: #999;
  font-weight: bold;
  font-size: 14px;
  margin-bottom: 10px;
}
.arrow-icon {
  width: 20px;
  height: 20px;
  background-color: #999;
  border-radius: 50%;
  margin-bottom: 10px;
}
.placeholder1:hover {
  background-color: #f0f0f0;
}
.placeholder1:before {
  font-size: 1.2rem;
  color: #888;
  opacity: 0.6;
}
.temp-emp-card {
  width: 250px;
  height: 250px;
  border: 2px solid #999;
  margin: 5px;
  background-color: #fff;
}
.card {
  background-color: #f8f8f8;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  padding: 1px;
  text-align: center;
  transition: background-color 0.3s ease;
}
.card:hover {
  background-color: #f0f0f0;
}
.emp-photo {
  margin-bottom: 15px;
}
.emp-photo img {
  width: 130px;
  height: 130px;
  border-radius: 50%;
  object-fit: cover;
  border: 4px solid #fff;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
.card-title {
  font-size: 15px;
  font-weight: bold;
  margin-bottom: 5px;
  margin-top: 5px;
  padding: 8px 16px;
  background-color: #e2e2e2;
  color: #333;
  border-radius: 20px;
  display: inline-block;
}
.card-text {
  font-size: 14px;
  color: #888;
}
.delete-button {
  position: absolute;
  top: 5px;
  right: 5px;
  font-size: 1.5rem;
  color: #dc3545;
  cursor: pointer;
}
.details-button {
  position: absolute;
  top: 5px;
  left: 5px;
  font-size: 1.5rem;
  color: #dc3545;
  cursor: pointer;
}
.card-body {
  padding: 1px;
}

说明

  • 我们去掉了原来固定的width:370px,改用Grid的列分配来自动适配宽度,不管容器多大,金字塔结构都会保持居中且比例合适。
  • 通过grid-column属性精确控制每个slot在网格中的位置,完美实现你要的:
    slot3
      slot1  slot2
    slot4 slot5 slot6
    

的金字塔布局。

  • 所有你原来的视觉样式(渐变背景、伪元素文字、hover效果等)都完整保留,只是调整了布局逻辑。

备注:内容来源于stack exchange,提问作者Anyname Donotcare

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.22 13:44:41