Text Inputs
Materialize is a modern responsive CSS framework based on Material Design by Google.
Input fields
Text fields allow user input. The border should light up simply and clearly indicating which field the user is currently editing. You must have a
.input-field
div wrapping your input and label. This is only used in our Input and Textarea form elements.
The validate class leverages HTML5 validation and will add a valid
and invalid
class
accordingly. If you don't want the Green and Red validation states, just remove the validate
class from your inputs.
Input Types
Alternate Input Types such as email and password are also supported.
Inline Inputs
Also Inline Inputs are supported.
Floating Labels
Since MaterializeCSS v2, floating Labels are rendered with CSS only by default. However, it is required to use a placeholder with a single white space (" ") in the HTML (providing an empty string will not work!).
Important: If you provide a value different than a single white space, the CSS rules will treat it as a "important" placeholder value and will always render the labels in "active" state.
Icon Prefixes
You can add an icon prefix to make the form input label even more clear. Just add an icon with the class prefix
before the input
and label.
Icon Suffixes
You can also add an icon suffix. Just add an icon with the class suffix
before the input and label.
Custom Error or Success Messages
You can add custom validation messages by adding either data-error
or data-success
attributes
to your helper text element.
Changing colors
Here is a CSS template for modifying input fields in CSS. With Sass, you can achieve this by just changing a variable. The CSS shown below is unprefixed. Depending on what you use, you may have to change the type attribute selector.
/* label color */
.input-field label {
color: #000;
}
/* label focus color */
.input-field input[type=text]:focus + label {
color: #000;
}
/* label underline focus color */
.input-field input[type=text]:focus {
border-bottom: 1px solid #000;
box-shadow: 0 1px 0 0 #000;
}
/* valid color */
.input-field input[type=text].valid {
border-bottom: 1px solid #000;
box-shadow: 0 1px 0 0 #000;
}
/* invalid color */
.input-field input[type=text].invalid {
border-bottom: 1px solid #000;
box-shadow: 0 1px 0 0 #000;
}
/* icon prefix focus color */
.input-field .prefix.active {
color: #000;
}
Textarea
Textareas allow larger expandable user input. The border should light up simply and clearly indicating which field the user is currently editing. You must have a
.input-field
div wrapping your input and label. This is only used in our Input and Textarea form elements.
Textareas will auto resize to the text inside.
Advanced notes:
If you are adding Textarea form elements dynamically, you can use this to initialize them.
M.Forms.InitTextarea(document.querySelector('#textarea1'));
When dynamically changing the value of a textarea like setting HTMLElement#value
attribute, you must trigger an autoresize on it
afterwords because simply updating the element's value does not automatically trigger the events we've binded to the textarea.
document.querySelector("#textarea1").value = 'New Text';
M.Forms.textareaAutoResize(document.querySelector('#textarea1'));
Icon Prefixes
You can add an icon prefix to make the form input label even more clear. Just add an icon with the class prefix
before the input
and label.
File Input
If you want to style an input button with a path input we provide this structure.
You can also use the multiple
attribute to allow multiple file uploads.
Advanced note:
If you are adding File Input form elements dynamically, you can use this to initialize them.
M.Forms.InitFileInputPath(document.querySelector('#fileinput1'));
Character Counter
Initialization
There are no options for this plugin, but if you are adding these dynamically, you can use this to initialize them.
document.addEventListener('DOMContentLoaded', function() {
var elems = document.querySelectorAll('input#input_text, textarea#textarea2');
var instances = M.CharacterCounter.init(elems);
});