社内デフォルトで使っているフォームパーツのCSSを大公開!!
フォームパーツ用CSS
このCSSをreset.css
などに組み込んでデフォルトで使ってます。
案件に応じて、style.css
などで上書きします。
*ラジオボタン/チェックボックスはlabel内包型
*ファイルアップロードは基本的にはデフォルトで、変更の必要がある場合は別で作ります。
*普段あまり使わない、color や progressなど爪のあまい部分があります。
@charset "utf-8";
fieldset,
legend,
button {
margin: 0;
padding: 0;
border: none;
border-radius: 0;
outline: none;
background: none;
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
}
input[type="date"],
input[type="datetime-local"],
input[type="month"],
input[type="time"] {
margin: 0;
padding: 4px;
border: 1px solid #ccc;
outline: none;
font-size: 16px;
}
input[type="text"],
input[type="password"],
input[type="email"],
input[type="tel"],
input[type="url"],
input[type="search"],
input[type="number"],
input[type="datetime"],
input[type="week"],
textarea,
select {
margin-right: 0;
margin-left: 0;
padding: 4px;
border: 1px solid #ccc;
border-radius: 0;
outline: none;
background: none;
font-size: 16px;
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
}
textarea {
width: 100%;
height: 100px;
overflow: auto;
}
select {
padding-right: 30px;
background-image: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAALJJREFUeNrslssNgCAQRCmB0ijBEuyEEijBEijBEizBEnAPy0UBWVj8JDvJJB5w3gsXVUoikXwsBrpAPdRCNcOmxi2P2yZ3cIKGU9dOCY0b590pdXhPHOyRyMEDsi4JhVIlSvBYTRGgSNTAQ+pFyyBRC7e5AdchUQt3d1fYIsEGb5Fgh1MktlFwisQwOIeE4/pGuDfhLRLscIrEMHiNxHB4SeIxeMyMPxkenyUSyT9zCDAAj5btZ4c3zaUAAAAASUVORK5CYII=");
background-repeat: no-repeat;
background-size: 12px 12px;
background-position: right 8px center;
}
.select {
display: inline-block;
position: relative;
overflow: hidden;
background: #fff;
}
.select select {
width: 100%;
padding-right: 24px;
border: 1px solid #ccc;
background: none;
text-overflow: "";
cursor: pointer;
}
.select::before {
position: absolute;
top: 13px;
right: 8px;
width: 0;
height: 0;
padding: 0;
content: "";
border-left: 6px solid transparent;
border-right: 6px solid transparent;
border-top: 6px solid #666;
pointer-events: none;
}
/* radio & checkbox */
input[type="radio"],
input[type="checkbox"] {
display: none;
}
input[type="radio"] + span,
input[type="checkbox"] + span {
display: inline-block;
position: relative;
margin-left: 8px;
padding: 10px 20px;
color: #555;
font-size: 1.4rem;
text-align: center;
line-height: 1;
cursor: pointer;
}
input[type="radio"] + span::before,
input[type="checkbox"] + span::before {
position: absolute;
top: 50%;
left: -8px;
width: 16px;
height: 16px;
content: "";
margin-top: -8px;
border: 1px solid #ccc;
background: #fff;
}
/* fieldset */
fieldset {
padding: 8px 16px;
border: 1px solid #ccc;
}
legend {
padding: 0 8px;
}
/* button */
input[type="submit"],
input[type="reset"],
input[type="button"],
button {
display: inline-block;
margin: 0;
padding: 10px 30px;
border: 1px solid #999;
border-radius: 0;
-webkit-box-sizing: content-box;
-webkit-appearance: button;
appearance: button;
background: #efefef;
color: #000;
font-size: 20px;
cursor: pointer;
}
/* ブラウザ対策
---------------------------------------------------------------------------- */
/* Firefox */
button::-moz-focus-inner,
[type="button"]::-moz-focus-inner,
[type="reset"]::-moz-focus-inner,
[type="submit"]::-moz-focus-inner {}
button:-moz-focusring,
[type="button"]:-moz-focusring,
[type="reset"]:-moz-focusring,
[type="submit"]:-moz-focusring {}
/* IE */
input::-ms-clear {}
input::-ms-reveal {}
select::-ms-expand {
display: none;
}
/* webkit */
[type="number"]::-webkit-inner-spin-button,
[type="number"]::-webkit-outer-spin-button {}
::-webkit-file-upload-button {}
/* iOS */
input[type="submit"]::-webkit-search-decoration,
input[type=reset]::-webkit-search-decoration,
input[type="button"]::-webkit-search-decoration,
input[type="search"]::-webkit-search-decoration {
display: none;
}
::-webkit-search-cancel-button {
-webkit-appearance: none;
}
input[type="search"]::-webkit-search-decoration {
margin: 0;
padding: 0;
border: 0;
border-radius: 0;
-moz-appearance: none;
-webkit-appearance: textfield;
-webkit-box-sizing: border-box;
appearance: none;
background: none;
outline: none;
font-size: 16px;
}
/* 挙動
---------------------------------------------------------------------------- */
/* hover */
input:hover,
textarea:hover,
select:hover {
border-color: #666;
}
input[type="radio"] + span:hover,
input[type="checkbox"] + span:hover {
color: #000;
}
input[type="radio"] + span:hover::before,
input[type="checkbox"] + span:hover::before {
border-color: #000;
}
input[type="radio"] + span:hover::after,
input[type="checkbox"] + span:hover::after {
position: absolute;
content: "";
top: 50%;
left: -4px;
width: 8px;
height: 8px;
margin-top: -4px;
background: #ccc;
}
/* checked */
input[type="radio"]:checked + span,
input[type="checkbox"]:checked + span {
color: #3498db;
}
input[type="radio"]:checked + span::before,
input[type="checkbox"]:checked + span::before {
border-color: #3498db;
}
input[type="radio"]:checked + span::after,
input[type="checkbox"]:checked + span::after {
position: absolute;
content: "";
top: 50%;
left: -4px;
width: 8px;
height: 8px;
margin-top: -4px;
background: #3498db;
}
/* radio */
input[type="radio"] + span::before,
input[type="radio"] + span:hover::after,
input[type="radio"]:checked + span::after {
border-radius: 50%;
}
/* button */
input[type="submit"]:hover,
input[type="reset"]:hover,
input[type="button"]:hover,
button:hover {
border-color: #3498db;
background: #3498db;
color: #fff;
}
/* focus */
input:focus,
textarea:focus {
border-color: #3498db;
}
input[type="submit"]:focus,
input[type=reset]:focus,
input[type="button"]:focus,
input[type="search"]:focus {
outline-offset: -2px;
}
/* disabled */
input:disabled,
input:disabled:hover,
textarea:disabled,
textarea:disabled:hover {
border-color: #ccc;
background: #eee;
cursor: not-allowed;
}
input[type="radio"]:disabled + span,
input[type="checkbox"]:disabled + span {
color: #ccc;
cursor: not-allowed;
}
input[type="radio"]:disabled + span::before,
input[type="checkbox"]:disabled + span::before {
border-color: #ccc;
cursor: not-allowed;
}
/* バリデーション */
input:valid {}
input:invalid {}
input:required {}
/* placeholder */
input::-webkit-input-placeholder {}
input::-moz-placeholder {}
input:-ms-input-placeholder {}
selectについて
select
をdiv
で囲う事で、装飾の幅が広がります。
WordPressで使う場合、「ウィジェット – アーカイブ」等でドロップダウン表示設定にした場合、div
を付けれないので、念のため両方指定しています。
<select name="select1">
<option value="あいうえお">あいうえお</option>
<option value="かきくけこ">かきくけこ</option>
<option value="さしすせそ">さしすせそ</option>
</select>
<div class="select">
<select name="select2">
<option value="あいうえお">あいうえお</option>
<option value="かきくけこ">かきくけこ</option>
<option value="さしすせそ">さしすせそ</option>
</select>
</div>
改造パターン
ラジオボタン/チェックボックスはlabel
内包型にするには、こちらの記事を参考にしてください。
その他の注意点
リセットやノーマライズの方法によっては、微妙なずれが生じる場合があります。
その場合は、普段使っているモノに合わせた微調整が必要になります。