CSS helps us to apply styles to the Forms created in HTML. You can check the HTML Forms tutorial we already have.
Just to recap, a Form could be your college admission form. Where you need to fill up your details and submit it(If it is an online form). And you can create that form in HTML.
In other words, the form which you fill and submit is an HTML form.
There are two ways using which we can apply styles to an HTML form :
Let us see them in detail.
We will be using the <input> element for applying styles to the form element.
<html> <head> <style> input { width: 100%; padding: 8px; border: 2px solid gray; background-color: MediumSeaGreen; color: white; } </style> </head> <body> <form action=" "> <label for="user_name">User Name :</label> <input type="text" id="user_name" name="User_Name"><br><br> <label for="password">Password :</label> <input type="password" id="password" name="Password"><br><br> </form> </body> </html>
So, if you look at the above example, we have <input> elements for the form.
<input type="text" id="user_name" name="User_Name">
And,
<input type="password" id="password" name="Password">
And we have applied styles for the <input> elements using the CSS properties.
input { width: 100%; padding: 8px; border: 2px solid gray; background-color: MediumSeaGreen; color: white; }
Now, if you look at the above output, both <input> elements have a solid gray border, green background color and other CSS properties defined.
Now, what if we want to apply CSS properties to the <input> element that accepts text input.
Let us see with the below example.
<html> <head> <style> input[type="text"] { width: 100%; padding: 8px; border: 2px solid gray; background-color: MediumSeaGreen; color: white; } </style> </head> <body> <form action=" "> <label for="user_name">User Name :</label> <input type="text" id="user_name" name="User_Name"><br><br> <label for="password">Password :</label> <input type="password" id="password" name="Password"><br><br> </form> </body> </html>
So, if you look at the above output, CSS styles are applied only to the <input> element that has type="text".
<input type="text" id="user_name" name="User_Name">
That is because in the CSS we have selected the <input> element along with its attributes(i.e. input[type="text"]).
input[type="text"] { width: 100%; padding: 8px; border: 2px solid gray; background-color: MediumSeaGreen; color: white; }
<html> <head> <style> input:focus { border: 2px solid gray; background-color: MediumSeaGreen; color: white; } </style> </head> <body> <form action=" "> <label for="user_name">User Name :</label> <input type="text" id="user_name" name="User_Name"><br><br> <label for="password">Password :</label> <input type="password" id="password" name="Password"><br><br> </form> </body> </html>
So, when you click on the textbox, the background color changes to green and the border changes to solid gray.
This is because we have used the :focus selector with the <input> element(i.e. input:focus).
input:focus { border: 2px solid gray; background-color: MediumSeaGreen; color: white; }