为什么光标没有在按钮上改变

时间:2019-06-29 15:13:40

标签: html css

我不确定为什么光标不会更改为按钮上的指针。我看到一些解决方案说这是由于z-index造成的。但是当我尝试使用z-index时,仍然无法使用。

.upload-btn-wrapper {
  position: relative;
  overflow: hidden;
  display: inline-block;
}

.btn {
  border: 2px solid gray;
  color: gray;
  background-color: white;
  padding: 8px 20px;
  border-radius: 8px;
  font-size: 20px;
  font-weight: bold;
}

.upload-btn-wrapper input[type=file] {
  font-size: 100px;
  position: absolute;
  left: 0;
  top: 0;
  opacity: 0;
  cursor: pointer;
}
<div class="upload-btn-wrapper">
  <button class="btn">Upload a file</button>
  <input type="file" name="myfile" />
</div>

感谢您的帮助。

我正在从这个Codepen作弊:

https://codepen.io/adambene/pen/xRWrXN

7 个答案:

答案 0 :(得分:2)

尝试将cursor: pointer;属性添加到文件的输入类型。 这是文件输入类型的最后摘要。

.upload-btn-wrapper input[type=file] {
  font-size: 100px;
  position: absolute;
  left: 0;
  top: 0;
  opacity: 0;
  cursor: pointer; /*this is what you need*/
}

让我知道是否可行。

答案 1 :(得分:1)

您需要对输入内容进行以下更改:

  1. 添加cursor: pointer
  2. 添加width: 100%height: 100%
  3. font-size: 100px更改为font-size: 0

.upload-btn-wrapper {
  position: relative;
  display: inline-block;
  overflow: hidden;
}

.btn {
  border: 2px solid gray;
  color: gray;
  background-color: white;
  padding: 8px 20px;
  border-radius: 8px;
  font-size: 20px;
  font-weight: bold;
}

.upload-btn-wrapper input[type=file] {
  font-size: 0;
  position: absolute;
  left: 0;
  top: 0;
  opacity: 0;
  cursor: pointer;
  width: 100%;
  height: 100%;
}
<div class="upload-btn-wrapper">
  <button class="btn">Upload a file</button>
  <input type="file" name="myfile" />
</div>

答案 2 :(得分:1)

您只需要将font-size 100%更改为font-size 0并将width:100%;height:100%添加到输入文件即可使用entier按钮

.upload-btn-wrapper {
  position: relative;
  overflow: hidden;
  display: inline-block;
}

.btn {
  border: 2px solid gray;
  color: gray;
  background-color: white;
  padding: 8px 20px;
  border-radius: 8px;
  font-size: 20px;
  font-weight: bold;
}

.upload-btn-wrapper input[type=file] {
      font-size: 0;
      position: absolute;
      left: 0;
      top: 0;
      opacity: 0;
      cursor: pointer;
      width: 100%;
      height: 100%;
}

答案 3 :(得分:1)

贷记到@Alon(还要感谢大家):

"A"

在FireFox和Chrome上均可使用以下CSS:

<div class="upload-btn-wrapper">
  <button class="btn">Upload a file</button>
  <input type="file" name="myfile" />
</div>

答案 4 :(得分:1)

工作正常。请检查一下

.upload-btn-wrapper {
  position: relative;
  overflow: hidden;
  display: inline-block;
}

.btn {
  border: 2px solid gray;
  color: gray;
  background-color: white;
  padding: 8px 20px;
  border-radius: 8px;
  font-size: 20px;
  font-weight: bold;
}

.upload-btn-wrapper input[type=file] {
  font-size: 100px;
  position: absolute;
  left: 0;
  top: 0;
  opacity: 0;
  text-indent: -999px;
  cursor: pointer;
}
<div class="upload-btn-wrapper">
  <button class="btn">Upload a file</button>
  <input type="file" name="myfile" />
</div>

答案 5 :(得分:1)

尝试

**.upload-btn-wrapper input[type=file]   {
  font-size: 100px;
  position: absolute;
  left: 0;
  top: 0;
  opacity: 0;
  cursor: pointer;
  width: 24px;
  height: 24px;
}**

答案 6 :(得分:0)

请尝试

.upload-btn-wrapper {
  position: relative;
  overflow: hidden;
}

.btn {
  border: 2px solid gray;
  color: gray;
  background-color: white;
  padding: 8px 20px;
  border-radius: 8px;
  font-size: 20px;
  font-weight: bold;
  cursor: pointer;
}

.upload-btn-wrapper input[type=file] {
  font-size: 100px;
  left: 0;
  top: 0;
  opacity: 0;
}
<div class="upload-btn-wrapper">
  <button class="btn">Upload a file</button>
  <input type="file" name="myfile" />
</div>