文件输入字段不是100%有效

时间:2016-12-23 11:59:43

标签: html css file input field

我有自定义文件附加字段。它工作正常,但有一点需要修复:该字段的右侧完全不活动。此外,如果您将光标移动到左上角,您可以注意到此部分显然处于活动状态,但CSS“cursor:pointer”选项没有效果。很确定我错过了一些简单的事情,请告诉我,我的错误是什么?先感谢您。 JSFiddle

@import url('https://fonts.googleapis.com/css?family=Roboto:100,300,500&subset=cyrillic');

* {
  margin: 0;
  padding: 0;
}

body, html {
  height: 100%;
}

body {
  color: #fff;
  font-size: 18px;
  font-family: "Roboto";
  font-weight: 100;
  background: #333;
}

body a {
  color:#fff;
  text-decoration: none;
  outline: none;
}

#container {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
}

.file-area {
  width: 450px;
  box-sizing: border-box;
  position: relative;
}
.file-area input[type=file] {
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  opacity: 0;
  cursor: pointer;
}
.file-area .file-dummy {
  width: 450px;
  padding: 30px;
  background: rgba(75, 58, 38, 0.3);
  border: 1px dashed rgba(255, 255, 255, 0.5);
  text-align: center;
  transition: background 0.3s ease-in-out;
}
.file-area .file-dummy .success {
  display: none;
}
.file-area:hover .file-dummy {
  background: rgba(255, 255, 255, 0.1);
}
.file-area input[type=file]:focus + .file-dummy {
  outline: 2px solid rgba(255, 255, 255, 0.5);
  outline: -webkit-focus-ring-color auto 5px;
  outline: none;
}
.file-area input[type=file]:valid + .file-dummy {
  border-color: rgba(0, 255, 0, 0.4);
  background-color: rgba(0, 255, 0, 0.2);
}
.file-area input[type=file]:valid + .file-dummy .success {
  display: inline-block;
}
.file-area input[type=file]:valid + .file-dummy .default {
  display: none;
}
<div id="container">
  <div class="file-area">  
    <input type="file" name="files" id="files" required="required"/>
    <div class="file-dummy">
      <span class="default">Choose files</span>
      <span class="success">Yahoo!</span>
    </div>
  </div>
</div>

5 个答案:

答案 0 :(得分:2)

刚刚从.file-area移除宽度,似乎工作正常。

============最新变化================

只是为了让cursor: pointer技巧改变font-size:0;

&#13;
&#13;
@import url('https://fonts.googleapis.com/css?family=Roboto:100,300,500&subset=cyrillic');

* {
    margin: 0;
    padding: 0;
}

body, html {
    height: 100%;
}

body {
	color: #fff;
	font-size: 18px;
	font-family: "Roboto";
	font-weight: 100;
	background: #333;
}

	body a {
		color:#fff;
		text-decoration: none;
		outline: none;
	}
	
#container {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100%;
}

.file-area {
  box-sizing: border-box;
  position: relative;
}
.file-area input[type=file] {
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  opacity: 0;
  cursor: pointer;
  font-size:0;
}
.file-area .file-dummy {
  width: 450px;
  padding: 30px;
  background: rgba(75, 58, 38, 0.3);
  border: 1px dashed rgba(255, 255, 255, 0.5);
  text-align: center;
  transition: background 0.3s ease-in-out;
}
.file-area .file-dummy .success {
  display: none;
}
.file-area:hover .file-dummy {
  background: rgba(255, 255, 255, 0.1);
}
.file-area input[type=file]:focus + .file-dummy {
  outline: 2px solid rgba(255, 255, 255, 0.5);
  outline: -webkit-focus-ring-color auto 5px;
  outline: none;
}
.file-area input[type=file]:valid + .file-dummy {
  border-color: rgba(0, 255, 0, 0.4);
  background-color: rgba(0, 255, 0, 0.2);
}
.file-area input[type=file]:valid + .file-dummy .success {
  display: inline-block;
}
.file-area input[type=file]:valid + .file-dummy .default {
  display: none;
}
&#13;
<div id="container">
	<div class="file-area">  
		<input type="file" name="files" id="files" required="required"/>
		<div class="file-dummy">
			<span class="default">Choose files</span>
			<span class="success">Yahoo!</span>
		</div>
	</div>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

问题仅在于课程width中已修复file-area,请尝试删除宽度,希望它能解决您的问题

Updated Fiddle Demo

答案 2 :(得分:1)

UITextField移除宽度,应该解决

.file-dummy

答案 3 :(得分:1)

将.file-area类宽度编辑为100%

答案 4 :(得分:1)

您添加padding&amp;应用width 100%,填充包含宽度。

像这样改变宽度width: calc(100% - 60px);

.file-area .file-dummy {
    background: rgba(75, 58, 38, 0.3) none repeat scroll 0 0;
    border: 1px dashed rgba(255, 255, 255, 0.5);
    padding: 30px;
    text-align: center;
    transition: background 0.3s ease-in-out 0s;
    width: calc(100% - 60px);
}