【问题标题】:Styling a file-upload button样式化文件上传按钮
【发布时间】:2016-09-07 07:21:26
【问题描述】:

我正在创建一个简单的文件上传网站。这是我拥有的代码:

    <form  action="" method="post" enctype="multipart/form-data">
    <input type="file"  id="upload-photo" name="files[]" multiple="multiple" accept="image/*"/>
<button type="submit" value="Upload!">Upload</button>
</form>

我看到了很多与我的问题非常相似的问题,但我找不到解决方案。也许我无法将它们应用于我的代码。不确定如何使用标签,因为在我创建一个标签后,它显示为文本,我无法像按钮一样设置它的样式。在我将&lt;input type="file" id="upload-photo" name="files[]" 的不透明度更改为 0 之后,还有一块空间保持空白。任何建议都会有所帮助。 提前致谢。

【问题讨论】:

    标签: html css button file-upload


    【解决方案1】:

    检查这个sn-p

    您需要做的是为输入字段和样式提供一个标签,因为它是一个按钮

    #upload-photo {
        height: 0;
        width: 0;
    }
    
    #upload-photo-label {
        border: 1px solid #cccccc;
        padding: 5px 30px;
        font-family:arial;
        font-size: 13px;
    }
    #upload-photo-label:active{
        background:#ccc;
    }
    <form action="" method="post" enctype="multipart/form-data">
      <input type="file"  id="upload-photo" name="files[]" multiple="multiple" accept="image/*"/>
      <label id="upload-photo-label" for="upload-photo">Browse</label>
      <button type="submit" value="Upload!">Upload</button>
    </form>

    【讨论】:

    • 我将其设置为我的其他按钮,可以正常工作,但标签继续显示为文本。 @Hitesh Misro
    • @NikiKaraolis 你能发一下你的问题吗?
    • 在这里,即使 php 无法正常工作。 jsfiddle.net/dredby5u/1/#&togetherjs=nao7twFLtT ,但奇怪的是浏览就像小提琴中的一个按钮。这是什么意思,我的服务器有问题,或者? @Hitesh Misro
    • 这就是它的样式,我在你的小提琴中没有遇到任何与文件浏览相关的问题。
    • 不知何故,已修复。今天刚打开我的电脑,加载网络,一切都很好。感谢您的帮助@Hitesh Misro
    【解决方案2】:

    你可以这样做

    HTML

    <div class="box">
    <input type="file" name="file-1[]" id="file-1" class="inputfile inputfile-1" data-multiple-caption="{count} files selected" multiple style="display: none;" />
    <label for="file-1"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="17" viewBox="0 0 20 17"><path d="M10 0l-5.2 4.9h3.3v5.1h3.8v-5.1h3.3l-5.2-4.9zm9.3 11.5l-3.2-2.1h-2l3.4 2.6h-3.5c-.1 0-.2.1-.2.1l-.8 2.3h-6l-.8-2.2c-.1-.1-.1-.2-.2-.2h-3.6l3.4-2.6h-2l-3.2 2.1c-.4.3-.7 1-.6 1.5l.6 3.1c.1.5.7.9 1.2.9h16.3c.6 0 1.1-.4 1.3-.9l.6-3.1c.1-.5-.2-1.2-.7-1.5z"/></svg> <span>Choose a file&hellip;</span></label>
    </div>
    

    CSS

    .box {
        background-color: #dfc8ca;
        padding: 6.25rem 1.25rem;
    }
    
    .js .inputfile {
        width: 0.1px;
        height: 0.1px;
        opacity: 0;
        overflow: hidden;
        position: absolute;
        z-index: -1;
    }
    button, input {
        line-height: normal;
    }
    button, input, select, textarea {
        font-family: inherit;
        font-size: 100%;
        margin: 0;
    }
    
    inputfile-1 + label {
        color: #f1e5e6;
        background-color: #d3394c;
    }
    .inputfile + label {
        max-width: 80%;
        font-size: 1.25rem;
        font-weight: 700;
        text-overflow: ellipsis;
        white-space: nowrap;
        cursor: pointer;
        display: inline-block;
        overflow: hidden;
        padding: 0.625rem 1.25rem;
    }
    
    svg:not(:root) {
        overflow: hidden;
    }
    
    .inputfile-1 + label {
        color: #f1e5e6;
        background-color: #d3394c;
    }
    .inputfile + label {
        max-width: 80%;
        font-size: 1.25rem;
        font-weight: 700;
        text-overflow: ellipsis;
        white-space: nowrap;
        cursor: pointer;
        display: inline-block;
        overflow: hidden;
        padding: 0.625rem 1.25rem;
    }
    

    这里是演示链接https://jsfiddle.net/7we705q1/5/

    【讨论】:

    • 可能有效,但不确定如何将其连接到我的 php 代码。@Fahran Dharsi
    • 就是这么简单,你可以在php代码中使用html格式
    猜你喜欢
    • 2015-11-04
    • 1970-01-01
    • 1970-01-01
    • 2018-07-19
    • 1970-01-01
    • 2013-01-02
    • 1970-01-01
    • 1970-01-01
    • 2018-06-07
    相关资源
    最近更新 更多