搜尋

首頁  >  問答  >  主體

如何設定絕對定位元素的最大允許底部邊界位置

我正在為 WordPress 開發一個產品標籤插件,我已經完成了以下操作:

這是您在圖像上看到的輸出代碼:

<a href="https://localhost/woo-metal/product/test-3/" class="woocommerce-LoopProduct-link woocommerce-loop-product__link">

  <div class="wb_badge-container">
    <span class="wb_badge wb_badge-rounded wb_badge-topright" style="background-color:#dd3333;color:#ffffff;">TEST</span>
  </div>

  <img src="https://localhost/woo-metal/wp-content/uploads/woocommerce-placeholder.png" class="woocommerce-placeholder wp-post-image" alt="Placeholder" decoding="async" loading="lazy" width="450" height="450">
  
  <h2 class="woocommerce-loop-product__title">Test 3</h2>
  
  <span class="price">

    <span class="woocommerce-Price-amount amount">
      <bdi>
        <span class="woocommerce-Price-currencySymbol">$</span>0.00
      </bdi>
    </span>

  </span>

</a>

徽章有以下 CSS:

// ....
.wb_badge {
    z-index: 100;
    position: absolute;
    display: inline-flex;
    justify-content: center;
    align-items: center;
    padding: 12px;
    min-width: 15px;
    min-height: 15px;
    font-size: 12.5px;
    font-weight: bold;
    text-align: center;
    word-break: break-word;
    -webkit-box-shadow: 0px 0px 3px -1px rgba(0,0,0,0.75);
    -moz-box-shadow: 0px 0px 3px -1px rgba(0,0,0,0.75);
    box-shadow: 0px 0px 3px -1px rgba(0,0,0,0.75);
}

.wb_badge-topleft {
    top: 0;
    left: 0;
}
.wb_badge-topright {
    top: 0;
    right: 0;
}
.wb_badge-bottomleft {
    bottom: 0;
    left: 0;
}
.wb_badge-bottomright {
    bottom: 0;
    right: 0;
}
// ....

我希望實現徽章的左下或右下位置放置在產品圖像的末端而不是下方。目前,使用我的 CSS,我可以將其放置在「加入購物車」按鈕旁邊的最底部。我嘗試添加相對於某些元素的位置來設定位置的斷點,但它不起作用。您知道我該如何實現這一目標嗎?

P粉578680675P粉578680675279 天前546

全部回覆(1)我來回復

  • P粉980815259

    P粉9808152592024-03-23 09:25:43

    使用 CSS(不使用 JavaScript)執行此操作的唯一方法是更改​​ HTML 標記。您需要將 img 包裝在一個元素中,並將您的徽章元素放入其中:

    .image-wrapper {
      position: relative;
      display: inline-block;
      /* remove this line in your real code. testing only */
    }
    
    .wb_badge {
      z-index: 100;
      position: absolute;
      display: inline-flex;
      justify-content: center;
      align-items: center;
      padding: 12px;
      min-width: 15px;
      min-height: 15px;
      font-size: 12.5px;
      font-weight: bold;
      text-align: center;
      word-break: break-word;
      box-shadow: 0px 0px 3px -1px rgba(0, 0, 0, 0.75);
    }
    
    .wb_badge-topleft {
      top: 0;
      left: 0;
    }
    
    .wb_badge-topright {
      top: 0;
      right: 0;
    }
    
    .wb_badge-bottomleft {
      bottom: 0;
      left: 0;
    }
    
    .wb_badge-bottomright {
      bottom: 0;
      right: 0;
    }
    
    
      
    TEST
    Placeholder

    Test 3

    $0.00

    回覆
    0
  • 取消回覆