CSS:如何使自定义文件上传按钮占据全角(按钮有效)
CSS: How to make custom file upload button take full width (button works)
我是 CSS 的新手,希望这里有人可以帮助我。
我正在尝试将简单的自定义样式应用到文件上传按钮(作为HTML form) 使它看起来与我页面上的其他按钮相似,并在跨浏览器中获得相似的外观。
到目前为止,我有以下按预期工作的。
我现在唯一的问题是我希望按钮 占据其父 div 的整个宽度(在我的例子中,这将跨越 9/12('col-9') 的页面)。
我尝试将 width: 100%;
添加到 CSS,但此按钮不再起作用。
我的HTML:
<div class="col-3 frmCaption">Attachments:</div>
<div class="col-9">
<div class="customUpload btnUpload btnM">
<span>Upload files</span>
<input type="file" class="upload" />
</div>
</div>
我的CSS:
.btnDefault, .btnUpload {
background-color: #FFFFFF;
border: 1px solid #CCCCCC;
color: #333333;
cursor: pointer;
font-weight: 400;
display: inline-block;
padding: 6px 12px;
text-align: center;
text-decoration: none;
vertical-align: middle;
}
.btnDefault:focus, .btnDefault:hover, .btnUpload:focus, .btnUpload:hover {
background-color: #E6E6E6;
}
.btnM {
border-radius: 4px;
font-size: 14px;
padding: 6px 12px;
}
.customUpload {
overflow: hidden;
position: relative;
}
.customUpload input.upload {
cursor: pointer;
margin: 0;
opacity: 0;
filter: alpha(opacity=0);
padding: 0;
position: absolute;
right: 0;
top: 0;
}
您还需要将宽度 属性 应用于包含的 <div>
。一旦 div 有完整的尺寸,那么只有里面的按钮可以有完整的宽度。
为简单起见,我已对 html 进行了更改,您可以将其移动到适当的 类。
<div class="col-3 frmCaption">Attachments:</div>
<div class="col-9">
<div class="customUpload btnUpload btnM" style="width:100%;">
<span>Upload files</span>
<input type="file" class="upload" style="width:100%;"/>
</div>
</div>
或者您可以使用此 CSS 并将其添加到您的 div 和文件上传中,
.fullwidth
{
width : 100%;
}
<div class="col-3 frmCaption">Attachments:</div>
<div class="col-9">
<div class="customUpload btnUpload btnM fullwidth">
<span>Upload files</span>
<input type="file" class="upload fullwidth"/>
</div>
</div>
要设置输入元素的样式,您需要实际设置其 label
元素的样式。
来自 MDN、
The HTML Label Element () represents a caption for an item in a user interface. It can be associated with a control either by placing the control element inside the element, or by using the for attribute. Such a control is called the labeled control of the label element.
因此,无论何时单击 label
,都会触发附加的输入。
因此,只需将输入元素包裹在标签中而不是 div 中,然后随意拉伸。这将解决您的问题。
.btnDefault,
.btnUpload {
background-color: #FFFFFF;
border: 1px solid #CCCCCC;
color: #333333;
cursor: pointer;
font-weight: 400;
display: inline-block;
padding: 6px 12px;
text-align: center;
text-decoration: none;
vertical-align: middle;
}
.btnDefault:focus,
.btnDefault:hover,
.btnUpload:focus,
.btnUpload:hover {
background-color: #E6E6E6;
}
.btnM {
border-radius: 4px;
font-size: 14px;
padding: 6px 12px;
}
.customUpload {
overflow: hidden;
position: relative;
display: block;
}
.customUpload input.upload {
cursor: pointer;
margin: 0;
opacity: 0;
filter: alpha(opacity=0);
padding: 0;
position: absolute;
right: 0;
top: 0;
}
<div class="col-3 frmCaption">Attachments:</div>
<div class="col-9">
<label class="customUpload btnUpload btnM"> <span>Upload files</span>
<input type="file" class="upload" />
</label>
</div>
使按钮采用 12 列(因为您使用的是 12 列系统),因为这是可用的 cils 的最大数量,这样元素将占据 div 的大小包含在
我是 CSS 的新手,希望这里有人可以帮助我。
我正在尝试将简单的自定义样式应用到文件上传按钮(作为HTML form) 使它看起来与我页面上的其他按钮相似,并在跨浏览器中获得相似的外观。
到目前为止,我有以下按预期工作的。 我现在唯一的问题是我希望按钮 占据其父 div 的整个宽度(在我的例子中,这将跨越 9/12('col-9') 的页面)。
我尝试将 width: 100%;
添加到 CSS,但此按钮不再起作用。
我的HTML:
<div class="col-3 frmCaption">Attachments:</div>
<div class="col-9">
<div class="customUpload btnUpload btnM">
<span>Upload files</span>
<input type="file" class="upload" />
</div>
</div>
我的CSS:
.btnDefault, .btnUpload {
background-color: #FFFFFF;
border: 1px solid #CCCCCC;
color: #333333;
cursor: pointer;
font-weight: 400;
display: inline-block;
padding: 6px 12px;
text-align: center;
text-decoration: none;
vertical-align: middle;
}
.btnDefault:focus, .btnDefault:hover, .btnUpload:focus, .btnUpload:hover {
background-color: #E6E6E6;
}
.btnM {
border-radius: 4px;
font-size: 14px;
padding: 6px 12px;
}
.customUpload {
overflow: hidden;
position: relative;
}
.customUpload input.upload {
cursor: pointer;
margin: 0;
opacity: 0;
filter: alpha(opacity=0);
padding: 0;
position: absolute;
right: 0;
top: 0;
}
您还需要将宽度 属性 应用于包含的 <div>
。一旦 div 有完整的尺寸,那么只有里面的按钮可以有完整的宽度。
为简单起见,我已对 html 进行了更改,您可以将其移动到适当的 类。
<div class="col-3 frmCaption">Attachments:</div>
<div class="col-9">
<div class="customUpload btnUpload btnM" style="width:100%;">
<span>Upload files</span>
<input type="file" class="upload" style="width:100%;"/>
</div>
</div>
或者您可以使用此 CSS 并将其添加到您的 div 和文件上传中,
.fullwidth
{
width : 100%;
}
<div class="col-3 frmCaption">Attachments:</div>
<div class="col-9">
<div class="customUpload btnUpload btnM fullwidth">
<span>Upload files</span>
<input type="file" class="upload fullwidth"/>
</div>
</div>
要设置输入元素的样式,您需要实际设置其 label
元素的样式。
来自 MDN、
The HTML Label Element () represents a caption for an item in a user interface. It can be associated with a control either by placing the control element inside the element, or by using the for attribute. Such a control is called the labeled control of the label element.
因此,无论何时单击 label
,都会触发附加的输入。
因此,只需将输入元素包裹在标签中而不是 div 中,然后随意拉伸。这将解决您的问题。
.btnDefault,
.btnUpload {
background-color: #FFFFFF;
border: 1px solid #CCCCCC;
color: #333333;
cursor: pointer;
font-weight: 400;
display: inline-block;
padding: 6px 12px;
text-align: center;
text-decoration: none;
vertical-align: middle;
}
.btnDefault:focus,
.btnDefault:hover,
.btnUpload:focus,
.btnUpload:hover {
background-color: #E6E6E6;
}
.btnM {
border-radius: 4px;
font-size: 14px;
padding: 6px 12px;
}
.customUpload {
overflow: hidden;
position: relative;
display: block;
}
.customUpload input.upload {
cursor: pointer;
margin: 0;
opacity: 0;
filter: alpha(opacity=0);
padding: 0;
position: absolute;
right: 0;
top: 0;
}
<div class="col-3 frmCaption">Attachments:</div>
<div class="col-9">
<label class="customUpload btnUpload btnM"> <span>Upload files</span>
<input type="file" class="upload" />
</label>
</div>
使按钮采用 12 列(因为您使用的是 12 列系统),因为这是可用的 cils 的最大数量,这样元素将占据 div 的大小包含在