我在下面的代码中包含一个带有悬浮效果的简单html按钮。我要完成的工作是,当我将鼠标悬停在按钮上时,它将变为透明,带有白色边框。我不知道为什么无论我如何更改我的代码都不起作用。任何帮助将不胜感激。
但是当我反过来做
body{
background-color: red;
}
.upload-btn-wrapper {
margin-top: 20px;
position: relative;
overflow: hidden;
display: inline-block;
}
.btn {
color: white;
border: 1px solid white;
background-color: black;
padding: 8px 20px;
font-size: 20px;
font-weight: bold;
}
.upload-btn-wrapper:hover {
background-color: transparent;
border: 1px solid white;
color: white;
}
.upload-btn-wrapper input[type=file] {
font-size: 100px;
position: absolute;
left: 0;
top: 0;
opacity: 0;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<div class="upload-btn-wrapper">
<button id="uploadbtn" class="btn">Upload CV!</button>
<input type="file" name="myfile" />
</div>
</body>
</html>
答案 0 :(得分:3)
这是因为您在.btn
上放置了背景,但是在悬停时更改了包装器的背景,而不是按钮本身。将选择器更改为.upload-btn-wrapper:hover .btn
body{
background-color: red;
}
.upload-btn-wrapper {
margin-top: 20px;
position: relative;
overflow: hidden;
display: inline-block;
}
.btn {
color: white;
border: 1px solid white;
background-color: black;
padding: 8px 20px;
font-size: 20px;
font-weight: bold;
}
.upload-btn-wrapper:hover .btn {
background-color: transparent;
border: 1px solid white;
color: white;
}
.upload-btn-wrapper input[type=file] {
font-size: 100px;
position: absolute;
left: 0;
top: 0;
opacity: 0;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<div class="upload-btn-wrapper">
<button id="uploadbtn" class="btn">Upload CV!</button>
<input type="file" name="myfile" />
</div>
</body>
</html>
答案 1 :(得分:2)
将此添加到您的CSS:
#uploadbtn:hover {
background: transparent;
}
答案 2 :(得分:1)
尝试此代码。您可以通过一些动画来完成想要的悬停操作
由于输入type = file而发生。由于我们无法按ID选择该元素,请尝试按标记名称input[type=file]
<!DOCTYPE html>
<html>
<head>
<style>
.button {
width:180px;
color: white;
padding: 16px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 4px 2px;
-webkit-transition-duration: 0.8s;
transition-duration: 0.8s;
cursor: pointer;
}
.button1 {
background-color: red;
color: black;
border: 2px solid red;
}
.button2 {
background-color: green;
color: black;
border: 2px solid #008CBA;
}
.button3 {
background-color: rgba(0,0,0,0.1);
color: black;
border: 4px double blue;
}
.button1:hover {
background-color: transparent;
color: red;
}
input[type=file]:hover,.button2:hover {
background-color: transparent;
color: transparent;'
}
.button3:hover {
background-color:rgba(0,0,0,0.6);
color: white;
border:none;
}
</style>
</head>
<body>
<h1>transperant animated button</h1>
<div>
<button class="button button1">background hide</button>
<input type="file" name="myfile" />
</div>
<br>
<button class="button button2">background and text hide</button><br>
<button class="button button3">border hide with color change </button>
</body>
</html>
答案 3 :(得分:0)
使悬停时的背景色继承不透明。例如:
.upload-btn-wrapper:hover .btn {
background-color: inherit;
border: 1px solid white;
color: white;
}
答案 4 :(得分:-1)
这是因为您将:hover规则应用于错误的元素
将其更改为
#uploadbtn:hover {
background-color: transparent;
border: 1px solid white;
color: white;
}
加上您的输入[type =“ file”]被重叠在按钮上。添加以下CSS来克服它
.upload-btn-wrapper input[type=file]
{
z-index : -1;
}
#uploadbtn{
z-index : 99;
}