
When styling web forms and inputs, we can use CSS to make them more visually appealing and user-friendly.
For example, we can use the `box-shadow` property to add a subtle shadow effect to form inputs, making them stand out from the rest of the page. This can be seen in the example where the input field has a `box-shadow` of `0 0 10px rgba(0, 0, 0, 0.2)`.
We can also use the `border-radius` property to give form inputs a rounded corner effect, making them more visually appealing. This can be seen in the example where the input field has a `border-radius` of `5px`.
To add some color to our form inputs, we can use the `background-color` property. For instance, we can set the `background-color` to `#f2f2f2` to give it a light gray background.
Explore further: Add Text in Css
Basic Styling
Basic Styling is a great place to start when it comes to input CSS styling. You can use attribute selectors to style specific input types, like input[type=text] for text fields.

To style a checkbox, you can add CSS properties like width, height, and margin-right. For example, setting width and height to 20px will give the checkbox a square shape.
Here are some examples of how you can style a checkbox:
- width: 20px sets the width of the checkbox to 20px
- height: 20px sets the height of the checkbox to 20px
- margin-right: 10px sets a 10px margin to the right side of the checkbox
- cursor: pointer sets the cursor to a pointer on hovering the checkbox
Example
You can style specific input types using attribute selectors, which is super helpful when you want to customize the look of certain fields without affecting others.
For example, you can use `input[type=text]` to select only text fields, and `input[type=password]` to select only password fields.
If you want to style number fields, you can use `input[type=number]`. It's that simple!
Here are some examples of attribute selectors for different input types:
- input[type=text]
- input[type=password]
- input[type=number]
- input[type=email]
- input[type=tel]
These selectors will help you target specific input types and apply custom styles to them.
Pseudo-Elements
Pseudo-Elements are a powerful tool in CSS, but they have some limitations when it comes to form inputs. Specifically, form inputs do not support insertion with ::before and ::after pseudo-elements.
Expand your knowledge: Css Form Styling
The only pseudo-elements supported by inputs are primarily used for decorating elements rather than inserting content. These include ::placeholder and ::selection, which are supported by text inputs to style placeholder text and text selection, respectively.
You can use these pseudo-elements to give your form inputs a unique look. For example, you can use ::placeholder to style the placeholder text in a text input.
Here's a list of the pseudo-elements supported by inputs:
- ::placeholder: Supported by text inputs to style placeholder text
- ::selection: Supported by text inputs to style text selection
- ::file-selection-button: Supported only by native HTML file inputs to style the button in the file input
Note that vendor-specific pseudo-elements are not included in this list, as they lack standardization and can't guarantee consistent appearance across different browsers.
Padding
Padding is a crucial aspect of basic styling, and it's used to add space inside the text field. The padding property is what you need to use to achieve this.
You can also add some margin to inputs that are placed after each other to create more space outside of them. This is especially useful when you have many inputs in a row.
Suggestion: Css Grid Space between
Setting the box-sizing property to border-box makes sure that the padding and eventually borders are included in the total width and height of the elements. This is a good practice to follow.
A padding of 12px is a common example of how to add space between the input field content and the border. This will give your input fields a bit of breathing room.
If this caught your attention, see: Html Css Box Model
Border
The border property adds a border around the input field. For example, a solid black border of 4px.
You can also use the border-bottom property to add a border only to the bottom of the input field.
The border-radius property adds rounded corners to the input field. For example, a 10px border radius.
The box-shadow property adds a shadow to the input field. For example, a shadow effect around the input field when it is being hovered.
In addition to the box-shadow, you can also add a transition to animate the box-shadow for a more visually appealing effect.
In Windows High Contrast mode, the box-shadow and border color change won't be visible, so it's a good idea to include a transparent outline for those users.
You might enjoy: Dropdown Styling Css
Interactive Elements
Interactive elements are a crucial part of any form, and styling them can make a big difference in the user experience.
You can remove the default blue outline around an input field by adding outline: none; to the input. This is especially useful for creating a clean and minimalist design.
To make input fields more interactive, you can use the :focus selector to do something with the input field when it gets focus, such as changing its color or adding an animation. For example, you can animate the width of the search input when it gets focus using the CSS transition property.
Here are some pseudo-classes you can use to quantify the validation and perceive different states of input elements:
- :hover
- :active
- :focus
These pseudo-classes can help you create a more dynamic and engaging user experience.
Focused
Focused inputs can be customized to remove the default blue outline by adding `outline: none;` to the input.
You can use the `:focus` selector to do something with the input field when it gets focus, such as animating the width of the search input.
The `:focus` state is an important aspect of interactive elements, and it's essential to handle it properly to create a seamless user experience.
By adding a transition to the `:focus` state, you can animate the box-shadow, making the input field more visually appealing.
For example, you can use the `calc()` function to lighten the original value by 40% and add a bit of alpha transparency to the box-shadow.
In Windows High Contrast mode, the box-shadow and border color change may not be visible, so it's essential to include a transparent outline for those users.
To create a combo effect that changes the border color and adds a box-shadow, you can define the border color by constructing the full hsl value from the individual CSS variable values.
By using the `hsl` color model, you can create a variant of the border color by updating just the lightness value, making it easier to achieve a 3:1 contrast ratio.
With Icon
You can add an icon inside an input field by using the background-image property and positioning it with background-position. This allows you to reserve space for the icon by adding a large left padding.
To achieve this, you'll need to add a background-image to the input field, which will serve as the icon. The background-position property will then be used to position the icon correctly.
The background-image property is used to add the icon, while the background-position property is used to place it in the right spot. By adding a large left padding, you're essentially reserving space for the icon to fit inside the input field.
Using this method, you can easily add icons to your input fields and create a visually appealing interactive element.
Intriguing read: Background Styling Css
Responsive Elements
Responsive elements are crucial for creating a smooth user experience, especially on smaller screens. By combining width and max-width, you can ensure that elements stay within a defined max-width for a responsive design (RWD) approach.
Suggestion: Responsive Tables Css
To prevent an awkwardly wide but short appearance, consider setting a minimum height for textareas. This simple adjustment can make a big difference in the overall look and feel of your interactive elements.
Using a responsive design approach can help you create a more cohesive and user-friendly interface. By setting a maximum width, you can ensure that elements don't become too large or overwhelming.
Here are some popular CSS frameworks that can help you achieve responsive elements:
- Tailwind CSS provides a dedicated plugin for implementing beautiful forms.
- Bootstrap offers comprehensive form documentation and an overview page dedicated to forms.
- Bulma's documentation covers form elements and provides ready-made examples.
By choosing the right framework and using it effectively, you can create responsive elements that enhance the user experience and make your interactive elements shine.
Elements
Elements are the building blocks of any form, and understanding how to style them is crucial for creating a seamless user experience.
Responsive form elements are sized using width and max-width, ensuring they stay within the defined max-width for a responsive design approach. This prevents awkwardly wide but short appearances.
For your interest: Css Responsive
To add some style to form elements, consider setting a minimum height for textareas. This will prevent them from looking out of place.
Styling form elements using CSS frameworks like Tailwind CSS, Bootstrap, and Bulma can be a great way to quickly implement beautiful forms in your design prototypes. However, it's essential to carefully consider the utility classes and resets provided by these frameworks.
Form elements can be selected in various ways, including by referring to their element names or attributes using element attribute selectors. For example, input[type=text] selects all input fields having type attribute set to text.
Here are some common form elements and their uses:
By default, some browsers will add a blue outline around the input when it gets focus. You can remove this behavior by adding outline: none; to the input.
Radio and Checkbox
Radio and Checkbox elements are an essential part of interactive design, and optimizing their appearance can make a big difference in user experience.
Nesting radio and checkbox buttons within their label elements creates a cleaner and more efficient appearance.
You can style radio buttons with CSS, setting their width and height to match your design, such as 20px for both width and height.
Here are some specific CSS styles you can use for radio buttons:
- width: 20px sets the width of radio button to 20px
- height: 20px sets the height of radio button to 20px
- margin-right: 10px sets the 10px margin to the right side
Checkboxes can also be styled with CSS, and their default appearance can be customized to fit your design.
You can set the width and height of checkboxes to 20px, just like radio buttons, for a consistent look.
Here are some specific CSS styles you can use for checkboxes:
- width: 20px sets the width of check box to 20px
- height: 20px sets the height of check box to 20px
- margin-right: 10px sets the 10px margin to the right side
- cursor: pointer sets the cursor as pointer on hovering checkbox
Theming and Defaults
You can write custom default styles or use a pre-built CSS reset like normalize.css for consistent form styling across browsers. This involves inheriting font and color properties from parent elements, typically the body element, to create a consistent base style.
Setting font and color properties to inherit from parent elements helps to prevent inconsistent styles across different form elements. You can then specify properties that require explicit definitions, such as line height, to differentiate elements from their parent's styles.
Consider reading: Css Font Styling
To standardize the shape, size, and spacing of form elements, you should avoid styling the input tag directly, as this can apply unconventional styles to all input types. Instead, specify properties for specific input types, such as text fields, select boxes, and buttons, to create a consistent look and feel.
Prioritizing CSS custom properties can simplify organization, maintenance, and customization of your styles. This is especially useful when creating themes, as you can easily switch between different color schemes by updating the custom properties.
Discover more: Css Selector That Styles Child If Parent Has an Attribute
Setting Defaults
You can write custom default styles or use a pre-built CSS reset like normalize.css for consistent form styling across browsers.
Typically, you'll want to inherit font and color properties from the parent elements, like the body element's font-family and font-size.
Inheriting from the parent elements helps keep your code organized and prevents unnecessary duplication.
To differentiate elements from their parent's styles, specify properties that require explicit definitions, such as a shorter line height for form elements.
Take a look at this: Tailwindcss Fonts
A shorter line height can improve the overall look and feel of your form elements.
To standardize the shape, size, and spacing of elements, add CSS properties without styling the input tag directly, which can apply unconventional styles to all input types.
Specify the input type to apply CSS to similar input elements, like text fields, select boxes, and buttons.
Adding a solid border for these elements keeps them equal in height, making your form more visually appealing.
A slight border-radius can make your form elements look more polished and refined.
Prioritize using CSS custom properties to simplify organization, maintenance, and customization.
Basic Theming
Basic theming is where you can get creative and make your design stand out. You can manage color themes using CSS custom properties, which is a great way to simplify organization and maintenance.
Targeting and theming text, borders, outlines, and backgrounds using relevant properties is straightforward. You can use the accent-color CSS property to alter the appearance of elements that rely on browser or OS accent colors for their default styling.
Check this out: Css How to Override Style Class Using Stylesheet

Consider using the dark value for the color-theme property when implementing dark mode. This instructs the browser to utilize optimized UI decorations for various elements, including form inputs, in dark interfaces.
The transition property can be used to create smooth color transitions for text, borders, and backgrounds. This is a great way to add a touch of elegance to your design.
Applying the accent-color CSS property to elements like radio buttons and checkboxes can help you customize their appearance. This is especially useful when you want to create a consistent look and feel across your design.
Using CSS custom properties, you can create a range of color themes and easily switch between them. This is a great way to make your design more dynamic and engaging.
Text and Input Styling
Text and input styling is a crucial aspect of creating a visually appealing and user-friendly form. You can start by setting border colors and defining different borders for hover, active, and focus states to guide users regarding the various states of text inputs, including textareas.
To add a touch of personality to your form, you can style text areas with a background color, width, height, padding, and border radius. For example, you can add a pink background color, expand the text area to its full width, set the height to 100px, add a padding of 10px, and round the corner with a 4px radius.
Some key properties to consider when styling input fields include width, padding, margin, border, border radius, box shadow, font, and colored input. You can also use the padding property to add space inside the text field, and the border property to add a border around the input field. Additionally, you can use the background-color property to add a background color to the input, and the color property to change the text color.
Text
Text is a fundamental element of any form, and styling it can greatly enhance the user experience. To set border colors and define different borders for hover, active, and focus states, you can use borders to guide users regarding the various states of text inputs, including textareas.
If this caught your attention, see: Styling Borders Css
You can hide the default browser outline added on focus by specifying it through type-based selection.
Styling text areas is a breeze, and you can add different CSS stylings to the text area, such as a pink background color, expanding it to its full width, setting a specific height, adding padding, ensuring padding and borders are included in the height and width, rounding the corners, and setting the text color to purple.
To add a background color to the text area, you can use the background-color property, and to change the text color, you can use the color property.
Here are some common CSS stylings you can apply to text areas:
- background-color: pink
- width: 100%
- height: 100px
- padding: 10px
- box-sizing: border-box
- border-radius: 4px
- color: purple
By applying these stylings, you can create a visually appealing text area that guides users through the different states of the text input.
You can also allow the text area to retain the resize function under general circumstances, but you can limit it to just vertical resizing to prevent layout breakage from a user dragging it really wide.
If this caught your attention, see: Css Grid Area
Here's a summary of the common CSS stylings you can apply to text areas:
Readonly Style
The readonly attribute is a useful tool for preventing additional user input, but it doesn't prevent the value from being selected or discovered by assistive tech.
To give users a clear hint that a field is readonly, we can add some styles to the CSS. This can be done by targeting any .input that also has the [readonly] attribute.
Attribute selectors are a powerful method for styling specific elements, and they're definitely worth adding to your CSS toolbox. They allow you to target elements based on their attributes, which can be very useful.
To style readonly fields, we can swap the border for a dotted one, assign it the not-allowed cursor, and enforce a medium-grey text color. This gives users a clear visual cue that the field is readonly.
As seen in the example, the user cannot interact with the field except to highlight or copy the value.
Broaden your view: Position Css Attribute
Layout and Alignment
Establishing a clear layout hierarchy on larger forms is crucial. A clear separation is vital for this, so introduce it strategically within each form area by grouping similar inputs and elements using div containers.
This distinction ensures they visually stand apart from identical siblings, enhanced by appropriate margin application. Grouping similar inputs and elements helps the form feel more organized.
To arrange elements vertically when they don’t fit horizontally, wrap them in a flexbox and set its flex-wrap property to wrap. This automatically arranges elements vertically when they don’t fit horizontally.
Adding space around an element, such as an input field, can be done using the margin property. For example, adding 20px of vertical margin in the input field can be achieved with the following code: margin: 20px.
Related reading: Css Text Margin
Textarea and Button Styling
To style a textarea, you can use CSS variables to update the border color and background color to shades of grey, making it more discoverable by assistive tech. This is important for accessibility.
Related reading: Css Color Text
You can also use the resize property to specify which direction the textarea can be resized, or if it can be resized at all. Limiting it to vertical resizing can prevent layout breakage.
Here are some additional CSS stylings you can apply to a textarea:
- background-color: pink adds a pink color to the background of the text area
- width: 100% expands the text area to its full width
- height: 100px sets the height of the text area to 100px
- padding: 10px adds a padding of 10px in the text area
- box-sizing: border-box ensures padding and borders are included in the height and width of the text area
- border-radius: 4px rounds the corner of the text area with a 4px radius
- color: purple sets the text color to purple
For file input styling, you can't change the button created by the browser, but you can adjust the font size to make it more consistent with other field types.
Textarea Styles
Textarea Styles are crucial for a good user experience.
You can update the style for disabled textareas by attaching it to the .input class and using CSS variables to change the border color and background. This is especially important for accessibility reasons.
The resize property is unique to textareas and allows you to specify which direction the textarea can be resized. It's usually a good idea to allow vertical resizing to prevent layout breakage.
Here are some CSS properties you can use to style textareas:
- background-color: pink adds a pink color to the background of the textarea
- width: 100% expands the textarea to its full width
- height: 100px sets the height of the textarea to 100px
- padding: 10px adds a padding of 10px to the textarea
- box-sizing: border-box ensures padding and borders are included in the height and width of the textarea
- border-radius: 4px rounds the corner of the textarea with a 4px radius
- color: purple sets the text color to purple
You can also use the resize property to prevent textareas from being resized, which can be helpful in certain situations.
Buttons
Styling input buttons is a breeze with CSS. For more information, check out our CSS Buttons Tutorial.
You can style submit buttons with different CSS properties. Let's add some CSS stylings to the button and see how it looks.
File input styling is a bit more limited. We can't style the button created by the browser or change the prompt text, but we can use custom fonts and adjust the font size. A good starting point is to downsize the font to make it more consistent with other field types.
The font size is a great option to address the issue of a large inherited button. And don't forget to adjust the top padding to maintain a balanced look.
Additional reading: Remove Button Styling Css
Accessibility and Reset
To ensure our input styles are accessible, we need to maintain at least 3:1 contrast between the input border and its surroundings.
The :focus state must also have 3:1 contrast against the unfocused state, especially when changing the border color or thickness. A thickness of 2px or more is a good starting point for this.
To set a solid foundation for our input styles, we start with a reset that addresses common differences in browser rendering. This reset explicitly sets font-size and font-family to avoid inheritance issues from the document.
Accessible Contrast
Accessible Contrast is crucial for a seamless user experience.
All user interface elements, including the input border, need to have at least 3:1 contrast against their surroundings.
To meet this requirement, the input border should be distinguishable from its background, making it easier for users to interact with the interface.
The :focus state also needs to have 3:1 contrast against the unfocused state, especially when changing the border color or increasing its thickness to 2px or more.
This means that when a user interacts with an element, it should be visually distinguishable from its normal state, providing a clear indication of what's happening.
Reset Styles
Reset Styles is a crucial step in ensuring consistency across browsers. It's a modern best practice to include a reset in your CSS, just like we do in all our tutorials.
Some standout differences in the initial state of fields across browsers are in border type, background color, and font properties. This is why we need to explicitly set font-size and font-family, as they don't inherit from the document like typography elements do.
We should set our input's font-size to at least 16px to avoid zooming being triggered upon interaction in mobile Safari. This is because 1rem typically equals 16px, but we'll explicitly set it as a fallback.
A border set using the theme variable can help create consistency, and adding a slightly rounded corner can improve the user experience.
Featured Images: pexels.com


