フォーム周りで覚えておくと便利なCSS Snippets http://www.nxworld.net/tips/15-useful-css-snippets-in-form-style.html
<!-- /*CSSのみでツールチップを表示する*/ -->
<p>
<input type="text" />
<span class="error">この項目は必須項目となります。</span>
</p>
<style>
p {
position: relative;
}
.error {
position: absolute;
top: -3.5em;
left: 0;
padding: .5em 1em .4em;
background-color: #f66;
border: 1px solid #f00;
border-radius: 5px;
color: #fff;
}
.error:after,
.error:before {
content: '';
position: absolute;
top: 100%;
left: 15px;
border: solid transparent;
}
.error:after {
margin-left: 1px;
border-top-color: #f66;
border-width: 7px;
}
.error:before {
border-top-color: #f00;
border-width: 8px;
}
</style>
/*textareaのリサイズ機能を制限する*/
/* 横のみリサイズ可 */
textarea {
resize: horizontal;
}
/* 縦のみリサイズ可 */
textarea {
resize: vertical;
}
/* 縦横ともにリサイズ不可 */
textarea {
resize: none;
}
/* リサイズの最小値と最大値を指定 */
textarea {
min-width: 250px;
max-width: 500px;
min-height: 100px;
max-height: 250px;
}
<input type="radio" id="radio01" name="demo" /><label for="radio01">foo</label>
<input type="radio" id="radio02" name="demo" /><label for="radio02">bar</label>
<input type="radio" id="radio03" name="demo" /><label for="radio03">baz</label>
/*ラジオボタンをCSSのみで装飾する*/
/* custom01 */
input[type="radio"] {
display: none;
}
label {
position: relative;
display: inline-block;
padding: 3px 3px 3px 20px;
cursor: pointer;
}
label::before {
position: absolute;
content: '';
top: 50%;
left: 0;
width: 14px;
height: 14px;
margin-top: -8px;
background: #fff;
border: 1px solid #ccc;
border-radius: 100%;
}
input[type="radio"]:checked + label::after {
position: absolute;
content: '';
top: 50%;
left: 4px;
width: 8px;
height: 8px;
margin-top: -4px;
background: #3498db;
border-radius: 100%;
}
/* custom02 */
input[type="radio"] {
display: none;
}
label {
position: relative;
display: inline-block;
padding: 3px 3px 3px 20px;
cursor: pointer;
}
label::before,
label::after {
position: absolute;
content: '';
top: 50%;
border-radius: 100%;
-webkit-transition: all .2s;
transition: all .2s;
}
label::before {
left: 0;
width: 14px;
height: 14px;
margin-top: -8px;
background: #f3f3f3;
border: 1px solid #ccc;
}
label:hover::before {
background: #fff;
}
label::after {
opacity: 0;
left: 4px;
width: 8px;
height: 8px;
margin-top: -4px;
background: #3498db;
-webkit-transform: scale(2);
transform: scale(2);
}
input[type="radio"]:checked + label::before {
background: #fff;
border: 1px solid #3498db;
}
input[type="radio"]:checked + label::after {
opacity: 1;
-webkit-transform: scale(1);
transform: scale(1);
}
/*placeholder属性のカラーを変更する*/
/* Chrome */
::-webkit-input-placeholder {
color: #3498db;
}
/* Firefox */
::-moz-placeholder {
color: #3498db;
}
/* IE */
:-ms-input-placeholder {
color: #3498db;
}
<p>
<input type="text" id="demo" />
<label for="demo">label</label>
</p>
/*フォーカス時にラベルをアニメーションさせる*/
/*#ラベルが上部に移動*/
p {
position: relative;
}
input,
label {
-webkit-transition: all .2s;
transition: all .2s;
font-size: 15px;
}
input {
width: 300px;
padding: .5em .5em .5em 3.5em;
border: 1px solid #aaa;
background: #fff;
border-radius: 0;
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
-webkit-box-sizing: border-box;
box-sizing: border-box;
color: #333;
}
label {
position: absolute;
top: .7em;
left: .5em;
color: #aaa;
line-height: 1;
}
input:focus {
padding-left: .5em;
}
input:focus + label {
top: -1.5em;
}
/*#ラベルが左端に移動+表示向きを変更*/
p {
position: relative;
}
input,
label {
-webkit-transition: all .2s;
transition: all .2s;
font-size: 15px;
}
input {
width: 300px;
padding: .5em .5em .5em 3.5em;
border: 1px solid #aaa;
background: #fff;
border-radius: 0;
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
-webkit-box-sizing: border-box;
box-sizing: border-box;
color: #333;
}
label {
position: absolute;
top: .7em;
left: .5em;
color: #aaa;
line-height: 1;
}
input:focus {
padding-left: 1.5em;
}
input:focus + label {
top: .75em;
left: -.5em;
-webkit-transform: rotate(-90deg) scale(.7);
transform: rotate(-90deg) scale(.7);
}
/*#ツールチップに変化させる*/
p {
position: relative;
}
input,
label {
-webkit-transition: all .2s;
transition: all .2s;
font-size: 15px;
}
input {
width: 300px;
padding: .5em .5em .5em 3.5em;
border: 1px solid #aaa;
background: #fff;
border-radius: 0;
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
-webkit-box-sizing: border-box;
box-sizing: border-box;
color: #333;
}
label {
position: absolute;
top: .7em;
left: .5em;
color: #aaa;
line-height: 1;
}
input:focus {
padding-left: .5em;
}
input:focus + label {
top: -2.5em;
left: 0;
padding: .4em .7em .45em;
border-radius: .2em;
background-color: #3498db;
color: #fff;
font-size: 14px;
}
input:focus + label::after {
content: '';
position: absolute;
top: 100%;
left: 50%;
margin-left: -6px;
border: 6px solid transparent;
border-top-color: #3498db;
}
/*各要素のデフォルトスタイルを削除*/
input, button, textarea, select {
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
}
/* 完全にリセット */
input, button, textarea, select {
margin: 0;
padding: 0;
background: none;
border: none;
border-radius: 0;
outline: none;
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
}
/*フォーカス時に入力エリアを伸ばす*/
input[type="text"] {
width: 200px;
-webkit-transition: width .3s;
transition: width .3s;
}
input[type="text"]:focus {
width: 300px;
}
/*フォーカス時のスタイルを変更する*/
input[type="text"],
textarea {
outline: none;
border: 1px solid #aaa;
-webkit-transition: all .3s;
transition: all .3s;
}
input[type="text"]:focus,
textarea:focus {
box-shadow: 0 0 7px #1abc9c;
border: 1px solid #1abc9c;
}
/*以下ボワっとな感じ*/
input[type=text], textarea {
-webkit-transition: all 0.30s ease-in-out;
-moz-transition: all 0.30s ease-in-out;
-ms-transition: all 0.30s ease-in-out;
-o-transition: all 0.30s ease-in-out;
outline: none;
padding: 3px 0px 3px 3px;
margin: 5px 1px 3px 0px;
border: 1px solid #DDDDDD;
}
input[type=text]:focus, textarea:focus {
box-shadow: 0 0 5px rgba(81, 203, 238, 1);
padding: 3px 0px 3px 3px;
margin: 5px 1px 3px 0px;
border: 1px solid rgba(81, 203, 238, 1);
}
/*disabled状態の要素にスタイルを適用する*/
:disabled {
background-color: #eee;
cursor: not-allowed;
}
<input type="checkbox" id="demo01" checked />
<label for="demo01" data-on-label="On" data-off-label="Off"></label>
<input type="checkbox" id="demo02" />
<label for="demo02" data-on-label="Yes" data-off-label="No"></label>
/*チェックボックスをCSSのみで装飾する #2*/
/* common style */
input {
display: none;
}
input+label,
input+label::before,
input+label::after {
-webkit-transition: all .2s;
transition: all .2s;
}
input+label {
display: inline-block;
position: relative;
width: 100px;
height: 48px;
border-radius: 24px;
cursor: pointer;
}
input+label::before {
display: block;
content: attr(data-off-label);
position: absolute;
top: 14px;
right: 7px;
color: #fff;
font-family: Arial, sans-serif;
font-size: 18px;
}
input+label::after {
content: '';
position: absolute;
top: 2px;
left: 2px;
width: 44px;
height: 44px;
background-color: #fff;
border-radius: 50%;
}
input:checked+label::before {
content: attr(data-on-label);
left: 7px;
right: auto;
color: #fff;
}
input:checked+label::after {
left: 54px;
background-color: #f7f7f7;
}
/* demo01 */
#demo01+label {
background-color: #eee;
}
#demo01+label::before {
color: #aaa;
}
#demo01:checked+label {
background-color: #3498db;
}
#demo01:checked+label::before {
color: #fff;
}
/* demo02 */
#demo02+label {
background-color: #e74c3c;
}
#demo02:checked+label {
background-color: #1abc9c;
}
<input type="checkbox" id="checkbox01" name="demo" /><label for="checkbox01">foo</label>
<input type="checkbox" id="checkbox02" name="demo" /><label for="checkbox02">bar</label>
<input type="checkbox" id="checkbox03" name="demo" /><label for="checkbox03">baz</label>
/*チェックボックスをCSSのみで装飾する #1*/
/* custom01 */
input[type="checkbox"] {
display: none;
}
label {
position: relative;
display: inline-block;
padding: 3px 3px 3px 22px;
cursor: pointer;
}
label::before {
position: absolute;
content: '';
top: 50%;
left: 0;
width: 14px;
height: 14px;
margin-top: -8px;
background: #fff;
border: 1px solid #ccc;
}
input[type="checkbox"]:checked + label::after {
position: absolute;
content: '';
top: 5px;
left: 3px;
width: 13px;
height: 5px;
border-left: 2px solid #3498db;
border-bottom: 2px solid #3498db;
-webkit-transform: rotate(-45deg);
transform: rotate(-45deg);
}
/* custom02 */
input[type="checkbox"] {
display: none;
}
label {
position: relative;
display: inline-block;
padding: 3px 3px 3px 22px;
cursor: pointer;
-webkit-transition: all .2s;
transition: all .2s;
}
label::before,
label::after {
position: absolute;
content: '';
-webkit-transition: all .2s;
transition: all .2s;
}
label::before {
top: 50%;
left: 0;
width: 14px;
height: 14px;
margin-top: -8px;
background: #f4f4f4;
border: 1px solid #ccc;
border-radius: 3px;
}
label::after {
opacity: 0;
top: 50%;
left: 3px;
width: 8px;
height: 4px;
margin-top: -4px;
border-left: 2px solid #3498db;
border-bottom: 2px solid #3498db;
-webkit-transform: rotate(-45deg) scale(.5);
transform: rotate(-45deg) scale(.5);
}
label:hover::before {
background: #fff;
}
input[type="checkbox"]:checked + label::before {
background: #fff;
border: 1px solid #3498db;
}
input[type="checkbox"]:checked + label::after {
opacity: 1;
-webkit-transform: rotate(-45deg) scale(1);
transform: rotate(-45deg) scale(1);
}
/*IEのinput要素に出てくるマークを消去*/
/* text */
input::-ms-clear {
visibility: hidden;
}
/* password */
input::-ms-reveal {
visibility: hidden;
}
/*上記 display: none; でも可能*/