css - 如何自动调整自定义上传按钮的宽度?
问题描述
我正在为网站上的网络表单开发自定义上传按钮,但无法正确处理宽度。
我覆盖了默认输入标签并实现了自定义标签。我希望标签也显示上传的文件名,因此我将其分为两部分(1. 文本和 2. 上传按钮)。
当焦点在上传按钮上时,我需要一个边框出现。但是标签对象(带边框)总是比其中的 2 个跨度对象短。此外,如果我调整跨度内的文本,标签会变宽,但仍会在第二个跨度结束之前裁剪(它总是在文本的同一部分裁剪......大约 3 个字母总是在标签边框之外) .
所以我的问题是:如何调整标签的宽度以始终适合其中的两个跨度。
为了您的理解,这里有一个例子:
.wrapper {
width: 60vw;
height: 50vh;
background: rgba(0,0,0,.05);
margin: auto auto;
}
.inputFile {
width: 0.1px;
height: 0.1px;
opacity: 0;
position: absolute;
z-index: -1;
}
.inputFile + label {
font-size: 1rem;
display: inline-flex;
cursor: pointer;
white-space: nowrap;
}
.inputFile + label > span.labelFileButton {
background-color: blue;
border: 1px solid blue;
color: white;
padding: 2%;
}
.inputFile + label > span.labelFileAnzeige {
color: black;
border: 1px solid blue;
padding: 2%;
}
.inputFile:focus + label,
.inputFile + label:hover {
border: 3px solid red;
}
<!DOCTYPE html>
<head>
<link rel="stylesheet" type="text/css" href="css.css">
<script type="text/javascript" src="js.js"></script>
</head>
<body>
<div class="wrapper">
<input type="text" id="texteingabe">
<label for="texteingabe">Texteingabe</label>
<input type="file" id="uploadInput" class="inputFile">
<label for="uploadInput" class="labelFile">
<span class="labelFileButton">
Eine Datei hochladen
</span>
<span class="labelFileAnzeige">
Keine Datei ausgewählt
</span>
</label>
</div>
</body>
非常感谢您的任何意见或帮助!
最好的,亚瑟
解决方案
推荐阅读
- java - Java 创建一个泛型类,该类实现另一个具有约束的泛型接口
- reactjs - 如何将数据从表单发送到另一个页面?
- python - 如何在 GUI-init 上初始化 Kivy TreeView?
- javascript - 错误时重新定位所需的属性
- excel - 如何在将 Excel 连接字符串连接到 SQL Server 分析服务时对其进行加密?
- html - 输入在里面弄乱了标签
- python - 如何使用 python 请求 url 和下载文件夹?
- r - 即使在 R 中满足条件,也使用 WHILE 进行无限循环
- nlp - spaCy:为什么 token.conjuncts 只返回一个连词,而不是所有的连词?
- javascript - 使用 react-router-dom 添加多个参数并出现连接问题