How can you make an HTML element 'float' without affecting any other elements?

0

Issue

I am designing a product card, and I want to have the delete button be a round button with the X in the upper right corner of the product’s photo.

I usually would use position absolute to achieve this floating effect, but considering this is an element that will be dynamically created for different products, I need some way of styling it that is relative to the parent element, as I don’t know the position of each product card. I don’t think I can use position absolute, as it would be a different position for every product card.

Any Ideas?

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;
}

Like this:

Example of what I am trying to achieve.

Solution

Are you looking for something like this? You can modify the below as per your requirements:

.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  data-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>

Answered By – akhtarvahid

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More