- CSS Forms
- Styling Input Fields
- Example
- Padded Inputs
- Example
- Bordered Inputs
- Example
- Example
- Colored Inputs
- Example
- Focused Inputs
- Example
- Example
- Input with icon/image
- Example
- Animated Search Input
- Example
- Styling Textareas
- Example
- Styling Select Menus
- Example
- Styling Input Buttons
- Example
- Responsive Form
- Aligned Form
- How to Style HTML with CSS (Quick Hacks)
- How to Style Textarea Placeholder
- How to Style Text in Textarea
- How to Add Default Text in Textarea
- Remove Resize Handler in Textarea
- How to Resize Textarea Vertically
- How to Resize Textarea Horizontally
- How to Remove The Glow Around Textarea
- Textarea Nowrap
- How to Make Textarea Responsive
- How to Style Scrollbar in Textarea
- How to Remove Default Scrollbars in Internet Explorer
- All HTML Textarea Attributes
- Conclusion
CSS Forms
The look of an HTML form can be greatly improved with CSS:
Styling Input Fields
Use the width property to determine the width of the input field:
Example
The example above applies to all elements. If you only want to style a specific input type, you can use attribute selectors:
- input[type=text] — will only select text fields
- input[type=password] — will only select password fields
- input[type=number] — will only select number fields
- etc..
Padded Inputs
Use the padding property to add space inside the text field.
Tip: When you have many inputs after each other, you might also want to add some margin , to add more space outside of them:
Example
Note that we have set the box-sizing property to border-box . This makes sure that the padding and eventually borders are included in the total width and height of the elements.
Read more about the box-sizing property in our CSS Box Sizing chapter.
Bordered Inputs
Use the border property to change the border size and color, and use the border-radius property to add rounded corners:
Example
If you only want a bottom border, use the border-bottom property:
Example
Colored Inputs
Use the background-color property to add a background color to the input, and the color property to change the text color:
Example
Focused Inputs
By default, some browsers will add a blue outline around the input when it gets focus (clicked on). You can remove this behavior by adding outline: none; to the input.
Use the :focus selector to do something with the input field when it gets focus:
Example
Example
Input with icon/image
If you want an icon inside the input, use the background-image property and position it with the background-position property. Also notice that we add a large left padding to reserve the space of the icon:
Example
input[type=text] <
background-color: white;
background-image: url(‘searchicon.png’);
background-position: 10px 10px;
background-repeat: no-repeat;
padding-left: 40px;
>
Animated Search Input
In this example we use the CSS transition property to animate the width of the search input when it gets focus. You will learn more about the transition property later, in our CSS Transitions chapter.
Example
input[type=text] <
transition: width 0.4s ease-in-out;
>
input[type=text]:focus width: 100%;
>
Styling Textareas
Tip: Use the resize property to prevent textareas from being resized (disable the «grabber» in the bottom right corner):
Example
textarea <
width: 100%;
height: 150px;
padding: 12px 20px;
box-sizing: border-box;
border: 2px solid #ccc;
border-radius: 4px;
background-color: #f8f8f8;
resize: none;
>
Styling Select Menus
Example
select <
width: 100%;
padding: 16px 20px;
border: none;
border-radius: 4px;
background-color: #f1f1f1;
>
Styling Input Buttons
Example
input[type=button], input[type=submit], input[type=reset] <
background-color: #04AA6D;
border: none;
color: white;
padding: 16px 32px;
text-decoration: none;
margin: 4px 2px;
cursor: pointer;
>
/* Tip: use width: 100% for full-width buttons */
For more information about how to style buttons with CSS, read our CSS Buttons Tutorial.
Responsive Form
Resize the browser window to see the effect. When the screen is less than 600px wide, make the two columns stack on top of each other instead of next to each other.
Advanced: The following example uses media queries to create a responsive form. You will learn more about this in a later chapter.
Aligned Form
An example of how to style labels together with inputs to create a horizontal aligned form:
How to Style HTML with CSS (Quick Hacks)
Styling a is not the same as styling an HTML because both have different types of usages. We use when we need to write multiple lines of text.
That’s why sometimes it seems difficult to style a and also does not match with other boxes. Textarea also has some extra things to handle and customize like, scrollbar, font, and placeholder, we can resize the textarea, etc.
In this article, you will learn how you can style HTML very easily. You will get detailed information about this topic.
How to Style Textarea Placeholder
You can use many attributes with this tag. For example, title, cols, rows, wrap, etc. You will find a list of attributes that accepts at the end of this article.
In order to, add a placeholder like any other tag you have to use placeholder attribute in .
To add style to the placeholder in you can use the following code. We select ::placeholder in the CSS file to add any style to the placeholder.
/* Modern Browsers */ textarea::placeholder < font-size: 1rem; color: #9c9c9c; font-family: 'Roboto', sans-serif; >/* WebKit, Edge */ textarea::-webkit-input-placeholder < font-size: 1rem; color: #9c9c9c; font-family: 'Roboto', sans-serif; >/* Firefox 4-18 */ textarea:-moz-placeholder < font-size: 1rem; color: #9c9c9c; font-family: 'Roboto', sans-serif; >/* Firefox 19+ */ textarea::-moz-placeholder < font-size: 1rem; color: #9c9c9c; font-family: 'Roboto', sans-serif; >/* IE 10-11 */ textarea:-ms-input-placeholder < font-size: 1rem; color: #9c9c9c; font-family: 'Roboto', sans-serif; >/* Edge */ textarea::-ms-input-placeholder
Most of the modern browsers support just ::placehoder but to support other browsers we can use other versions like, -ms- , -moz- , -webkit- etc.
In the demo, I have used font-size , color, and font-family but you can use any CSS style you want. From now on you can customize your textarea placeholder according to your need.
How to Style Text in Textarea
You can style text inside textarea similarly as you can do with any other HTML inputs. You can also use any CSS property like font-size , font-family , color , etc.
To add the fonts to your website Google Fonts would be a great choice because they provide a lot of options and this is absolutely free. When someone writes text inside then the text will use that font.
How to Add Default Text in Textarea
You can add default text in your input. This text will be shown automatically inside textarea input box. Users can change that or completely replace it with their own text.
This is especially helpful if users have to write a similar type of text all the time. In this case, you can set a default text and users will edit the default text. It will save them time.
To add default text in textarea input you just have to place your text in between the opening tag . It is done.
Remove Resize Handler in Textarea
By default, browsers add resize handler in to the bottom right corner. You can click and drag to increase or decrease the size of the textarea input.
If you don’t need this feature, you can disable this with just one line of CSS.
How to Resize Textarea Vertically
If you don’t want to remove the resize handler completely then you have other options as well. You can allow your users to resize the vertically.
If you add the following CSS property then users will be able to resize your textarea only vertically.
How to Resize Textarea Horizontally
On the other hand, if you don’t want to change the size of your vertically but horizontally, you can do that too.
Following CSS property will make your textarea horizontally resizable.
How to Remove The Glow Around Textarea
In most browsers, when you focus or write inside the a glow or border appears. Some people like it but some don’t.
If this glow border does not fit with your design and you want to remove it, you can do that.
You can also apply this style using :focus pseudo-class as well both work the same.
Textarea Nowrap
Normally we use white-space: nowrap; to prevent text from wrapping. In this way, you can write in an input box as long as you want without any line breaks. The browser will show a horizontal scrollbar.
You can customize this default scrollbar if you want. I will show you how you can style textarea scrollbar in the next sections.
When you press Enter , you move to the next line. But this CSS property does not work with . To get a similar result, you have to use the wrap=»off» attribute in your input.
How to Make Textarea Responsive
You can make the responsive in many ways. As you have seen, you can control resize handler in textarea.
You can also apply width , height , min-width , max-width , min-height and max-height properties to make it responsive.
In the above code, we have set width: 100%; so by default textarea will take the full area. But max-width: 900px; means textarea will not be bigger than 900px.
If screen size increases, textarea size will increase up to 900px not more than that. It is also helpful if you set resize: horizontal; .
Users can resize textarea input but when you set max-width: 900px; users will be able to increase the textarea up to 900px. You can also set min-width if you want. Then users will not be able to decrease the size more than that.
It also works a similar way when you use the height property. Here default height of the will be 300px.
But when you set resize: vertical; then users will be able to increase its height up to 500px and decrease its height up to 200px.
How to Style Scrollbar in Textarea
When we add height to the and users write multiple lines of text then the browser shows a default scrollbar. Scrollbar may come horizontally and vertically.
If you don’t like the default look of those, you can style them according to your need. You can change their color, width, background, etc. And you don’t have to do this separately. You can customize both of them at the same time.
textarea::-webkit-scrollbar < width: 15px; background-color: #f5f5f5; box-shadow: inset 0 0 2px rgba(0, 0, 0, 0.1); -moz-box-shadow: inset 0 0 2px rgba(0, 0, 0, 0.1); -webkit-box-shadow: inset 0 0 2px rgba(0, 0, 0, 0.1); >textarea::-webkit-scrollbar-thumb
This will add a custom scrollbar to your input on both sides. Here, textarea::-webkit-scrollbar will style the background of the scrollbar and textarea::-webkit-scrollbar-thumb will style the scrollbar thumb, the part that moves.
But the above code will not work on the Firefox browser. Don’t worry you have another option.
You should add this code along with the above code then users will be able to see a custom scrollbar in both Chrome and Firefox browsers.
Firefox does not provide much customization options as Chrome does. You can not set a custom width for the scrollbar. The property scrollbar-width accepts only 3 values auto , thin , and none .
Here, auto is the default value and none will remove your scrollbar in the Firefox browser. This is also an experimental technology. So I would suggest you not use this part on your main website.
If you want to know more about scrollbars, you can follow our ultimate guide on scrollbar customization.
But you can use 1 st part textarea::-webkit-scrollbar and textarea::-webkit-scrollbar-thumb , this will work just fine.
How to Remove Default Scrollbars in Internet Explorer
Internet Explorer adds a vertical scrollbar by default in the textarea even if there is no text in it. It does not look good.
You can hide the default scrollbar using overflow: hidden; property. But the problem is you don’t get any scrollbar at all when a user writes multiple lines of text. So, it is not an ideal solution.
Therefore, we should use overflow: auto; this to remove the default scrollbar and it appears when needed.
All HTML Textarea Attributes
Here is the list of attributes that you can use in the HTML tag.
Conclusion
In this article, we have seen multiple ways how you can style your HTML textarea. I hope from now on you will be able to customize your textarea input according to your need.