CSS Forms
A form example, we use CSS to render HTML form elements:
CSS Examples
Try it Yourself »
Input (input) styles
Use the width property to set the width of the input box:
In the above examples, the width of all <input> elements is set to 100%. If you only want to set a specific type of input, you can use the following attribute selectors:
input[type=text]- Selects text input fieldsinput[type=password]- Selects password input fieldsinput[type=number]- Selects number input fields- ...
Input Field Padding
UsagepaddingThe property can add padding inside the input box.
CSS Examples
Try it Yourself »
Note that we setbox-sizingThe property isborder-boxThis ensures that the browser renders the input box with the specified width and height including the border and padding.
For more content, you can readCSS3 Box Sizing 。
Input (input) border
UsageborderThe property can modify the size or color of the input border. Useborder-radiusThe property can add rounded corners to the input:
If you only want to add a bottom border, you can useborder-bottomProperty:
Input (input) color
You can usebackground-colorproperty to set the background color of the input field,colorproperty is used to modify the text color:
Input (input) focus
By default, some browsers will have a blue outline when the input box gets focus (clicking the input box). We can set the input style tooutline: none;to ignore this effect.
Usage:focusThe selector can set the style of the input box when it gets focus:
Input (input) icon
If you want to add an icon in the input box, you can usebackground-imageproperty and the positioningbackground-positionproperty. Note set the left margin of the icon to give it some space:
CSS Examples
Try it Yourself »
Animated search box
The following example uses CSStransitionproperty, which sets the input box to extend to the right when it gets focus. You can useCSS Animationssection to see more content.
CSS Examples
Try it Yourself »
Text box (textarea) style
Note:Usageresizeproperty to disable the text box's ability to resize (usually dragging the bottom-right corner to resize).
CSS Examples
Try it Yourself »
Dropdown menu (select) styles
CSS Examples
Try it Yourself »
Button Styles
CSS Examples
Try it Yourself »
For more content, please refer to ourCSS ButtonsTutorial.
Responsive Forms
Responsive forms can rearrange elements based on the size of the browser window. We can resize the browser window to see the effect:
Advanced:The following example usesCSS3 Media Queriesto create a responsive form.
CSS Examples
Try it Yourself »