代码之家  ›  专栏  ›  技术社区  ›  Anyname Donotcare

如何将一组div形成为像树一样的金字塔形状?

  •  0
  • Anyname Donotcare  · 技术社区  · 2 年前

    如果我有以下HTML结构:

     <div class="slots-container">
                <div class="slot"></div>
                <div class="slot"></div>
                <div class="slot"></div>
                <div class="slot"></div>
                <div class="slot"></div>
                <div class="slot"></div>
    </div>
    

    以下CSS:

     <style>
      .slots-container {
                display: grid;
                grid-template-columns: repeat(3, 1fr);
                grid-gap: 5px;
            } 
    
    
            .slot {
                border: 2px dashed #ccc;
                padding: 5px;
                min-height: 300px;
                position: relative;
                cursor: pointer;
                transition: background-color 0.3s ease;
                display: flex;
                align-items: center;
                justify-content: center;
            }
    
                .slot:nth-child(3) {
                    background: linear-gradient(to bottom right, #e66465, #9198e5);
                }
    
                .slot:nth-child(2),
                .slot:nth-child(1) {
                    background: linear-gradient(to top left, #EAEAEA, #C4C4C4);
                }
    
                .slot:nth-child(4),
                .slot:nth-child(5),
                .slot:nth-child(6) {
                    background: linear-gradient(to top left, #D9D9D9, #ADADAD);
                }
    
    
                .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: #fff;
                    font-size: 14px;
                    font-weight: bold;
                    display: flex;
                    align-items: center;
                    justify-content: center;
                    font-family: inherit;
                    color: black;
                    border-bottom: 1px;
                    border-color: 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;
            }
        </style>
    

    根据@Rene的回答:

     <style>
     .slots-container {
        display: grid;
        gap: 0.3125rem;
        grid-template-areas: ". . a a . ."
            ". b b c c ."
            "d d e e f f";
        margin: 1px;
        overflow-x: hidden;
    }
    
    .slot:nth-child(1) {
        grid-area: b
    }
    
    .slot:nth-child(2) {
        grid-area: c
    }
    
    .slot:nth-child(3) {
        grid-area: a
    }
    
    .slot:nth-child(4) {
        grid-area: d
    }
    
    .slot:nth-child(5) {
        grid-area: e
    }
    
    .slot:nth-child(6) {
        grid-area: f
    }
    
    /* eye-candy only */
    .slot {
        min-height: 15.2rem;
        background-color: Silver;
        display: grid;
        place-items: center;
        font-size: 3vw;
        padding: 5px 1px 5px 1px;
        position: relative;
        cursor: pointer;
        transition: background-color 0.3s ease;
        align-items: center;
        justify-content: center;
        width: 370px;//The problem
        border-radius: 20px;
        height: 15.2rem;
    }
    
    
        .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);
        }
        </style>
    

    我想要像apyramid这样的插槽形状,如何做到保持相同的HTML:

                                    slot:nth-child(3)
                           slot:nth-child(1)   slot:nth-child(2)
                  slot:nth-child(4)   slot:nth-child(5) slot:nth-child(6)
    
    0 回复  |  直到 2 年前
        1
  •  2
  •   pier farrugia    2 年前

    你开始使用网格,为什么不使用每个元素的定位

    .slots-container {
      display: grid;
      grid-template-columns: repeat(12, 1fr);
      grid-template-rows: repeat(3, 1fr);
      grid-gap: 5px;
      width: 100vw;
      height: 100vh;
    }
    
    .slot {
      display: flex;
      align-items: center;
      justify-content: center;
    }
    
    .slot:nth-child(1) {
      grid-area: 2 / 3 / 3 / 7;
      background-color: rgba(163, 34, 173, 0.5);
    }
    
    .slot:nth-child(2) {
      grid-area: 2 / 7 / 3 / 11;
      background-color: rgba(30, 203, 81, 0.5);
    }
    
    .slot:nth-child(3) {
      grid-area: 1 / 5 / 2 / 9;
      background-color: rgba(58, 215, 181, 0.5);
    }
    
    .slot:nth-child(4) {
      grid-area: 3 / 1 / 4 / 5;
      background-color: rgba(178, 166, 72, 0.5);
    }
    
    .slot:nth-child(5) {
      grid-area: 3 / 5 / 4 / 9;
      background-color: rgba(103, 177, 182, 0.5);
    }
    
    .slot:nth-child(6) {
      grid-area: 3 / 9 / 4 / 13;
      background-color: rgba(122, 201, 176, 0.5);
    }
    <div class="slots-container">
      <div class="slot">1</div>
      <div class="slot">2</div>
      <div class="slot">3</div>
      <div class="slot">4</div>
      <div class="slot">5</div>
      <div class="slot">6</div>
    </div>
        2
  •  1
  •   Rene van der Lende    2 年前

    可以定义三角形 grid-template-areas 在您的 slots-container 并分配相关 grid-area 到特定 .slot :

    更新 代码到 justify-content: center 网格项。。。

    .slots-container {
        display: grid; 
    
        justify-content: center; /* centers grid items */
        
        gap: 5px;
    
        grid-template-areas: ". . a a . ."
                             ". b b c c ."
                             "d d e e f f";
    }
    
    .slot:nth-child(1) { grid-area: b }
    .slot:nth-child(2) { grid-area: c }
    .slot:nth-child(3) { grid-area: a }
    .slot:nth-child(4) { grid-area: d }
    .slot:nth-child(5) { grid-area: e }
    .slot:nth-child(6) { grid-area: f }
    
    /* eye-candy only */
    .slot {
        min-height: 18rem;
    
        width: 10rem; /* to test justification */
    
        border: 2px solid black;
        background-color: Silver;
        display: grid; place-items: center;
        font-size: 3vw;
    }
    <div class="slots-container">
        <div class="slot">1</div>
        <div class="slot">2</div>
        <div class="slot">3</div>
        <div class="slot">4</div>
        <div class="slot">5</div>
        <div class="slot">6</div>
    </div>