输入类型文件字段的样式不正常工作

时间:2017-10-03 12:47:53

标签: html css html5 css3

我将input字段中的自定义样式添加到label字段中。

但是它没有按预期工作。我希望它的width为100px,height为25px。另外我希望文本居中。

HTML:

.fileContainer {
  overflow: hidden;
  position: relative;
}

.fileContainer [type=file] {
  cursor: inherit;
  display: block;
  font-size: 999px;
  filter: alpha(opacity=0);
  min-height: 100%;
  min-width: 100%;
  opacity: 0;
  position: absolute;
  right: 0;
  text-align: right;
  top: 0;
}

.fileContainer {
  height: 25px;
  width: 100px;
  border: 1px solid #6C7A89;
  border-radius: 20px;
  background-color: transparent;
  font-size: 14px;
  color: #6C7A89;
}

.fileContainer [type=file] {
  cursor: pointer;
}
<label class="fileContainer">
Select file
<input name="filetoupload" type="file">
</label>

Fiddle

5 个答案:

答案 0 :(得分:2)

试试这个:

&#13;
&#13;
.fileContainer {
    position: relative;
}
.fileContainer input {
    position: absolute;
    z-index: 2;
    opacity: 0;
    width:100%;
    height:100%;
}

.fileContainer {
    border: 1px solid #6C7A89;
    border-radius: 20px;
    padding: 2px 15px;
    margin: 2px;
    background: transparent;
    display: inline-block;
}
&#13;
<div class="fileContainer">
    <input name="filetoupload" type="file"/>
    <span>Select file</span>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

widthheight并未发生任何问题,因为默认情况下label是内联元素级别。

您可以将此display: flex;添加到.filecontainer以制作块级元素。

这将文本置于其中心:

justify-content: center;
align-items: center;

<强>段

.fileContainer {
  overflow: hidden;
  position: relative;
}

.fileContainer [type=file] {
  opacity: 0;
  position: absolute;
  cursor: pointer;
}

.fileContainer {
  height: 25px;
  width: 100px;
  border: 1px solid #6C7A89;
  border-radius: 20px;
  background-color: transparent;
  font-size: 14px;
  color: #6C7A89;
  display: flex;
  justify-content: center;
  align-items: center;
}
<label class="fileContainer">
Select file
<input name="filetoupload" type="file">
</label>

答案 2 :(得分:1)

试试这个:

adminPassword

.fileContainer {
  display: inline-block;<---------Added[Need for set width and height]
  line-height: 25px;<------------Added[Center in the vertical axis]
  text-align: center;<------------Added[Center in the horizontal axis]
  //more code...
}
.fileContainer {
  overflow: hidden;
  position: relative;
}

.fileContainer [type=file] {
  cursor: inherit;
  display: block;
  font-size: 999px;
  filter: alpha(opacity=0);
  min-height: 100%;
  min-width: 100%;
  opacity: 0;
  position: absolute;
  right: 0;
  text-align: right;
  top: 0;
}

.fileContainer {
  height: 25px;
  line-height: 25px;
  width: 100px;
  border: 1px solid #6C7A89;
  border-radius: 20px;
  background-color: transparent;
  font-size: 14px;
  color: #6C7A89;
  display: inline-block;
  text-align: center;
}

.fileContainer [type=file] {
  cursor: pointer;
}

答案 3 :(得分:0)

添加此

.fileContainer{display:block; text-align:center;}

答案 4 :(得分:0)

plz更新你的css“fileContainer”

.fileContainer {
    overflow: hidden;
    position: relative;
}

.fileContainer [type=file] {
    cursor: inherit;
    min-height: 100%;
    min-width: 100%;
    opacity: 0;
    position: absolute;
    right: 0;
    text-align: right;
    top: 0;
}

.fileContainer {
  height:25px;
	width:100px;
	border:1px solid #6C7A89;
	border-radius:20px;
	background-color:transparent;
	font-size:14px;
	color:#6C7A89;
  padding-left: 21px;
  padding-right: 21px;
  padding-top: 5px;
  padding-bottom: 5px;
}

.fileContainer [type=file] {
    cursor: pointer;
}
.file{
  width: 100px;
  height: 100px;
  text-align: center;
}
<label class="fileContainer">
Select file
<input name="filetoupload" type="file" class="file">
</label>

<br><br><br>

<p>
/* add this css into label css "fileContainer" */ <br>

</p>
<code>
.fileContainer{
  padding-left: 21px;
  padding-right: 21px;
  padding-top: 5px;
  padding-bottom: 5px;
}
</code>