【问题标题】:How can you make an HTML element 'float' without affecting any other elements? [duplicate]如何在不影响任何其他元素的情况下使 HTML 元素“浮动”? [复制]
【发布时间】:2020-12-17 22:40:25
【问题描述】:

我正在设计一张产品卡片,我希望删除按钮是一个圆形按钮,产品照片的右上角有 X。

我通常会使用绝对位置来实现这种浮动效果,但考虑到这是一个将为不同产品动态创建的元素,我需要一些相对于父元素的样式,因为我不需要知道每个产品卡的位置。我认为我不能使用绝对位置,因为每张产品卡的位置都不同。

有什么想法吗?

HTML

<div class="cart-card">
  <span class="delete-button">
  <i class="fas fa-times"></i>
  </span>
  <div class="cart-img-cont">
    <img src="" alt="" class="cart-img" />
  </div>
  <p class="cart-card-product-name h-font">Product Name</p>
  <div class="cart-card-price">
    <p class="cart-price">$15.00</p>
    <p class="cart-merchant">
      <span class="cart-from">From</span> Merchant Name
    </p>
  </div>
  <div class="cart-card-qty-cont"></div>
  <div class="cart-opt-cont">
    <p class="opt-name">Option:</p>
    <select name="" id="" class="opt-select">
      <option value="#">Option</option>
    </select>
  </div>
</div>

CSS

.cart-card {
  width: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  margin-top: 10px;
  padding-top: 10px;
  border-top: 1px solid $gray;
  border-bottom: 1px solid $gray;
  padding-bottom: 10px;
}

.cart-img-cont {
  display: flex;
  flex-direction: row;
  justify-content: center;
  align-items: center;
  border: 1px solid $gray;
  padding: 10px;
  border-radius: 10px;
  margin: 0px auto;
  width: 100%;
  height: 272px;
}

.delete-button {
  background-color: $orange;
  border-radius: 50%;
  height: 36px;
  width: 36px;
  color: white;
  text-align: center;
  padding-top: 1.5px;
  font-size: 28px;
  position: absolute;
}

.cart-img {
  height: 100%;
  width: auto;
}

.cart-card-product-name {
  font-size: 18px;
  font-weight: 700;
  margin: 5px 0;
}

.cart-card-price {
  display: flex;
  flex-direction: row;
  justify-content: center;
  align-items: center;
}

.cart-price {
  border-right: 1px solid $gray;
  color: $orange;
  padding: 5px;
  font-weight: 700;
  font-size: 18px;
}

.cart-merchant {
  color: $blue;
  padding: 5px;
  font-weight: 700;
  font-size: 18px;
}

.cart-from {
  color: black;
  font-weight: 600;
}

.cart-card-qty-cont {}

.cart-opt-cont {
  display: flex;
  flex-direction: row;
  justify-content: center;
  align-items: center;
  margin-top: 5px;
}

.opt-name {
  padding-right: 10px;
  font-size: 18px;
}

.opt-select {
  width: 170px;
  height: 45px;
  border-radius: 5px;
  border: 1px solid $gray;
  padding: 5px;
  font-size: 18px;
  background: white;
}

像这样:

【问题讨论】:

    标签: html css flexbox position css-position


    【解决方案1】:

    您正在寻找这样的东西吗?您可以根据需要修改以下内容:

    .cart-card {
      width: 100%;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      margin-top: 10px;
      padding-top: 10px;
      border-top: 1px solid $gray;
      border-bottom: 1px solid $gray;
      padding-bottom: 10px;
      background: lightgrey;
    }
    
    .cart-img-cont {
      display: flex;
      flex-direction: row;
      justify-content: center;
      align-items: center;
      border: 1px solid $gray;
      padding: 10px;
      border-radius: 10px;
      margin: 0px auto;
      width: 100%;
      height: 272px;
    }
    
    .delete-button {
      background-color: $orange;
      border-radius: 50%;
      height: 36px;
      width: 36px;
      color: white;
      text-align: center;
      padding-top: 1.5px;
      font-size: 28px;
      position: absolute;
    }
    
    .cart-img {
      height: 100%;
      width: auto;
    }
    
    .cart-card-product-name {
      font-size: 18px;
      font-weight: 700;
      margin: 5px 0;
    }
    
    .cart-card-price {
      display: flex;
      flex-direction: row;
      justify-content: center;
      align-items: center;
    }
    
    .cart-price {
      border-right: 1px solid $gray;
      color: $orange;
      padding: 5px;
      font-weight: 700;
      font-size: 18px;
    }
    
    .cart-merchant {
      color: $blue;
      padding: 5px;
      font-weight: 700;
      font-size: 18px;
    }
    
    .cart-from {
      color: black;
      font-weight: 600;
    }
    
    .cart-card-qty-cont {}
    
    .cart-opt-cont {
      display: flex;
      flex-direction: row;
      justify-content: center;
      align-items: center;
      margin-top: 5px;
    }
    
    .opt-name {
      padding-right: 10px;
      font-size: 18px;
    }
    
    .opt-select {
      width: 170px;
      height: 45px;
      border-radius: 5px;
      border: 1px solid $gray;
      padding: 5px;
      font-size: 18px;
      background: white;
    }
    
    .close {
      position: absolute;
      top: 0;
      right: 0;
      background: red;
      color: white;
      font-size: 24px;
      border-radius: 50px;
      width: 45px;
      height: 45px;
      display: flex;
      justify-content: center;
      align-items: center;
      font-family: cursive;
    }
    <div class="cart-card">
      <span class="delete-button">
      <i class="fas fa-times"></i>
      </span>
      <div class="cart-img-cont">
        <img src="" alt="" class="cart-img" />
      </div>
      <p class="cart-card-product-name h-font">Product Name</p>
      <div class="cart-card-price">
        <p class="cart-price">$15.00</p>
        <p class="cart-merchant">
          <span class="cart-from">From</span> Merchant Name
        </p>
      </div>
      <div class="cart-card-qty-cont"></div>
      <div class="cart-opt-cont">
        <p class="opt-name">Option:</p>
        <select name="" id="" class="opt-select">
          <option value="#">Option</option>
        </select>
      </div>
      <div class="close">X</div>
    </div>

    【讨论】:

    • 是的,这会起作用。我的印象是绝对定位与整个页面相关,而不仅仅是父元素。有人将其链接到以前的答案,这也有效。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2019-03-16
    • 1970-01-01
    • 1970-01-01
    • 2018-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多