将鼠标悬停在图像上时显示按钮
Button reveal when hovering over image
我是 css 的新手,正在尝试在我的图像上实现一项功能,即当有人将鼠标悬停在图像上时,将显示一个 'Read More' 按钮。这是我到目前为止的代码 -
HTML-
<div class="container">
<img src="./images/pairingo.png">
<div class="button"><a href="#">READ MORE</a></div>
</div>
CSS-
.container{
position: relative;
margin-top: 20px;
}
.container .button {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
background-color: #555;
opacity: 0;
font-size: 16px;
padding: 12px 24px;
border-radius: 5px;
transition: opacity .5s;
}
img{
width:40%;
height:40%
}
img:hover .button{
opacity:1;
}
这似乎不起作用。也许我在悬停部分做错了什么?任何帮助将不胜感激。
您需要将 hover css 添加到 .container class。因为 img 是 .container class 的 child 而 .button 不是 img 的 child。
试试这个。
.container{
position: relative;
margin-top: 20px;
width: 300px;
height: 300px;
}
.container .button {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
background-color: #555;
opacity: 0;
font-size: 16px;
padding: 12px 24px;
border-radius: 5px;
transition: opacity .5s;
}
img{
width:100%;
}
.container:hover .button{
opacity:1;
}
<div class="container">
<img src="https://placehold.it/300x300/ccc/666?text=" class="img-responsive" alt="">
<div class="button"><a href="#">READ MORE</a></div>
</div>
给你:
Set the container
s top
and left
property to make your button visible wherever you need.
.container{
position: relative;
margin-top: 20px;
}
.container .button {
position: absolute;
top: 10%;
left: 20%;
transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
background-color: #555;
opacity: 0;
font-size: 16px;
padding: 12px 24px;
border-radius: 5px;
transition: opacity .5s;
}
img{
width:80px;
height:80px;
}
.container:hover .button{
opacity:1;
}
<div class="container">
<img src="http://placekitten.com/301/301">
<div class="button"><a href="#">READ MORE</a></div>
</div>
我是 css 的新手,正在尝试在我的图像上实现一项功能,即当有人将鼠标悬停在图像上时,将显示一个 'Read More' 按钮。这是我到目前为止的代码 -
HTML-
<div class="container">
<img src="./images/pairingo.png">
<div class="button"><a href="#">READ MORE</a></div>
</div>
CSS-
.container{
position: relative;
margin-top: 20px;
}
.container .button {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
background-color: #555;
opacity: 0;
font-size: 16px;
padding: 12px 24px;
border-radius: 5px;
transition: opacity .5s;
}
img{
width:40%;
height:40%
}
img:hover .button{
opacity:1;
}
这似乎不起作用。也许我在悬停部分做错了什么?任何帮助将不胜感激。
您需要将 hover css 添加到 .container class。因为 img 是 .container class 的 child 而 .button 不是 img 的 child。
试试这个。
.container{
position: relative;
margin-top: 20px;
width: 300px;
height: 300px;
}
.container .button {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
background-color: #555;
opacity: 0;
font-size: 16px;
padding: 12px 24px;
border-radius: 5px;
transition: opacity .5s;
}
img{
width:100%;
}
.container:hover .button{
opacity:1;
}
<div class="container">
<img src="https://placehold.it/300x300/ccc/666?text=" class="img-responsive" alt="">
<div class="button"><a href="#">READ MORE</a></div>
</div>
给你:
Set the
container
stop
andleft
property to make your button visible wherever you need.
.container{
position: relative;
margin-top: 20px;
}
.container .button {
position: absolute;
top: 10%;
left: 20%;
transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
background-color: #555;
opacity: 0;
font-size: 16px;
padding: 12px 24px;
border-radius: 5px;
transition: opacity .5s;
}
img{
width:80px;
height:80px;
}
.container:hover .button{
opacity:1;
}
<div class="container">
<img src="http://placekitten.com/301/301">
<div class="button"><a href="#">READ MORE</a></div>
</div>