我有以下代码
.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>
由于“愿望清单”图标,当前“添加到购物车”按钮向左移动。当将鼠标悬停在容器类上以容纳愿望清单图标时,如何使我的“添加到购物车”按钮位于容器div的中心并向左移动?
答案 0 :(得分:3)
您可以使用position
属性来实现此目的。这是经过编辑的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>
答案 1 :(得分:-2)
.wishlist{
position:absolute; /* change from relative */
transition: all 0.3s; /* add */
}
.container:hover .wishlist{
position:relative; /* add */
/* transition: all 0.3s; */ /* delete */
}