Created
May 30, 2012 21:43
-
-
Save MauMaGau/2839136 to your computer and use it in GitHub Desktop.
HTML/CSS/JQUERY: Nice styling
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- Le html --> | |
<form class='form form-horizontal' action='' method=''> | |
<!-- declaring a class of form looks odd, but it leaves me the option | |
of having non-standard forms without having to overwrite standard form css --> | |
<!-- Inputs and labels are grouped in fieldsets. This makes it much easier to | |
arrange them with css --> | |
<fieldset> | |
<label class='label placeholder'>First name</label><!-- This label serves as | |
a fallback, it will be removed with js because it has a class of placeholder --> | |
<input class='input input-small' placeholder='First name'> | |
</fieldset> | |
<fieldset> | |
<input type='submit' class='btn btn-large' value='Save'><!-- btn can be applied to | |
input submits, anchors, and buttons, to unify --> | |
</fieldset> | |
</form> | |
<!-- Le CSS --> | |
<style> | |
/* Global form styles */ | |
.form{} | |
.input[type=text]{} /* We don't _need_ to prefix the selector with its parent .form, so | |
let's not. It will only make it more flexible. */ | |
.label{} | |
.label.placeholder{} | |
/* Horizontal forms */ | |
.form-horizontal{} | |
.form-horizontal .fieldset{} | |
/* Global button styles*/ | |
.btn{} | |
.btn-large{} | |
/* ...etc */ | |
</style> | |
<!-- Le js --> | |
<script> | |
/* Placholders (there should be a minimizer check here, to skip placeholder-ing for browsers | |
that support the placeholder attribute) */ | |
$('.placeholder').hide(); | |
$('.input'){ | |
if( $(this).attr('placeholder') ){ | |
$(this).val( $(this).attr('placeholder') ); | |
} | |
} | |
$('.input').focus(function(){ | |
if( $(this).val() == $(this).attr('placeholder') ){ | |
$(this).val(''); | |
} | |
}); | |
$('.input').focusout(function(){ | |
if( $(this).val() == '' ){ | |
$(this).val( $(this).attr('placeholder') ); | |
} | |
}) | |
/* ...etc */ | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment