1

I have following code

.container {
  height: auto;
  width: 200px;
  background-color: gray;
  display: inline-block;
  text-align: center;
}

.tocart {
  width: 150px;
  display: inline-block;
}

.wishlist {
  display: inline-block;
  right: 36px;
  opacity: 0;
  visibility: visible;
  position: relative;
}

.container:hover .wishlist {
  right: 0;
  visibility: visible;
  opacity: 1;
  transition: all 0.3s;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">

<div class="container">
  <button class="tocart">
        Add to Cart
      </button>
  <a href="" class="wishlist"><i class="fa fa-heart"></i></a>
</div>

Currently the Add to cart button is shifted towards left due to wish-list icon. How to make my Add to cart button center of container div and shift it towards left when hovered on container class to accommodate wish-list icon?

2 Answers 2

3

You can achieve this using the position-property. Here is the edited CSS:

.container {
  height: auto;
  width: 200px;
  background-color: gray;
  display: inline-block;
  text-align: center;
  position: relative;
}

.tocart {
  width: 150px;
  display: inline-block;
}

.wishlist {
  display: inline-block;
  right: 36px;
  opacity: 0;
  visibility: visible;
  position: absolute;
}

.container:hover .wishlist {
  right: 15px;
  visibility: visible;
  opacity: 1;
  transition: all 0.3s;
  top: 2px;
}

.container:hover .tocart {
  float: left;
}
<head>
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
</head>
<div class="container">
  <button class="tocart">
    Add to Cart
  </button>
  <a href="" class="wishlist"><i class="fa fa-heart"></i></a>
</div>

0
-2
.wishlist{
  position:absolute; /* change from relative */
  transition: all 0.3s; /* add */
}


.container:hover .wishlist{
  position:relative; /* add */
  /* transition: all 0.3s; */ /* delete */
}
3
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - From Review Commented Oct 24, 2018 at 7:12
  • @MuhammadSaqlain it provides fixes and supplements for his pre-existing code so therefore does provide an answer. Commented Oct 24, 2018 at 7:15
  • Although this code might (or might not) solve the question, a good answer should always contain an explanation on what the code does and how it solves the problem.
    – BDL
    Commented Oct 24, 2018 at 9:36

Not the answer you're looking for? Browse other questions tagged or ask your own question.