天天看點

更改input的樣式【單選框、輸入框、上傳檔案的都有】

感謝内容提供者:​​金牛區吳迪軟體開發工作室​​

​如果我們不想用現有各種UI元件庫的input樣式那麼我們可以自己設計隻屬于我們自己的樣式~​

文章目錄

  • ​​一、單選框與檔案上傳​​
  • ​​二、輸入框:​​

一、單選框與檔案上傳

更改input的樣式【單選框、輸入框、上傳檔案的都有】

​注:不喜歡這個樣式可以自行修改color和background。​

HTML代碼如下:

<div>
    <label class="label-one"><input name="nex" type="radio"/><span class="label">男</span><br></label>
    <label class="label-two"><input name="nex" type="radio"/><span class="label">女</span></label>
</div>
<div class="file-wraps">
    上傳檔案
    <input type="file" />
</div>      

CSS代碼如下:

/* 此為單選框樣式 */
.label{
    width: 70px;
    height: 40px;
    line-height: 40px;
    margin-top: 10px;
    display: block;
    position: relative;
    padding-left:20px;
    box-sizing: border-box; 
    color: #999;
}
.label::after{
    content: "";
    border:1px solid #999;
    width: 10px;
    height: 10px;
    display: block;
    position: absolute;
    top: 15px;
    left: 0px;
    border-radius: 50%;
}
.label::before{
    content:"";
    background: #00ff00;
    border-radius: 50%;
    display: block;
    width: 6px;
    height: 6px;
    position: absolute;
    top: 18px;
    left: 3px;
    opacity: 0;
}
label input{
    display: none;
}
label input:checked + .label:after{
    border-color: #09f;
}
label input:checked + .label:before{
    opacity: 1;
    transition: opacity 0.5s ease;
}
label input:checked + .label{
    color: #0f00ff;
    transition: color 0.6s ease;
}
/* 此為上傳檔案樣式 */
.file-wraps{
    background: orange;
    width: 100px;
    height: 45px;
    position: relative;
    color: white;
    text-align: center;
    line-height: 45px;
}
.file-wraps input{
    position: absolute;
    top: 0;
    bottom: 0;
    bottom: 0;  
    right: 0;
    opacity: 0;
}      

二、輸入框:

修改input輸入框的樣式很簡單,我們隻需要把input的樣式都去除掉,然後弄個假的input覆寫即可,與我們之前做的複選框類似。

下面開始先做三個input輸入框【html代碼】:

<div class="input_control">
  <input type="text" class="form_input" placeholder="輸入你的手機号"/>
</div>
<div class="input_control">
    <input type="password" class="form_input" placeholder="輸入你的密碼"/>
</div>
<div class="input_control">
    <input type="email" class="form_input" placeholder="輸入你的郵箱"/>
</div>      

而後去處掉他原本的樣式并加入我們想要的樣式【css代碼】

.input_control{
    width:360px;
    margin:20px auto;
}
input[type="text"],input[type="password"],input[type="email"]{
    /* 清除原有input樣式 */
    -web-kit-appearance:none;
    -moz-appearance: none;
    outline:0;
    /* 設定我們要的樣式 */
    box-sizing: border-box;
    text-align:center;
    font-size:1.2em;
    height:2.5em;
    border-radius:6px;
    border:1px solid #c8cccf;
    color:#6a6f77;
    display:block;
    padding:0 1em;
    text-decoration:none;
    width:100%;
}
input[type="password"]:focus,input[type="email"]:focus,input[type="text"]:focus{
    border:1px solid #ff7496;
}
/* 更改placeholder的顔色,筆者是為了讓效果更明顯選用了紅色 */
::-moz-placeholder { /* Mozilla Firefox 19+ */
    color: red;
}
input::-webkit-input-placeholder{
    color: red;
}      

最後的效果: