Style Forms in CSS


Styling forms in CSS involves applying styles to various form elements like <input>, <textarea>, <select>, and <button>


Styling Input Fields

<style>
/* Basic styling for all input fields */
input[type="text"], input[type="email"] {
    width: 100%;
    padding: 10px;
    margin: 8px 0;
    box-sizing: border-box;
    border: 2px solid #ccc;
    border-radius: 4px;
    font-size: 16px;
}

/* Change border color on focus */
input[type="text"]:focus, input[type="email"]:focus {
    border-color: #4CAF50;
    outline: none;
}
</style>
<form>
    <label for="name">Name:</label>
    <input type="text" id="name" name="name">
    
    <label for="email">Email:</label>
    <input type="email" id="email" name="email">
</form>
  • width: 100%;: Makes the input fields take the full width of the container.
  • padding: 10px;: Adds space inside the input fields.
  • box-sizing: border-box;: Ensures padding is included in the element's total width and height.
  • border-radius: 4px;: Rounds the corners of the input fields.
  • :focus: This pseudo-class applies when the input field is focused, changing the border color.


Styling a Textarea

<style>
textarea {
    width: 100%;
    height: 150px;
    padding: 10px;
    border: 2px solid #ccc;
    border-radius: 4px;
    font-size: 16px;
    resize: vertical; /* Allow users to resize vertically only */
}

/* Change border color on focus */
textarea:focus {
    border-color: #4CAF50;
    outline: none;
}
</style>
<form>
    <label for="message">Message:</label>
    <textarea id="message" name="message"></textarea>
</form>

resize: vertical;: Allows resizing the textarea only vertically (you can also use none to disable resizing).


Styling a Select Dropdown

<style>
select {
    width: 100%;
    padding: 10px;
    border: 2px solid #ccc;
    border-radius: 4px;
    font-size: 16px;
    background-color: #fff;
}

/* Change border color on focus */
select:focus {
    border-color: #4CAF50;
    outline: none;
}
</style>
<form>
    <label for="options">Options:</label>
    <select id="options" name="options">
        <option value="option1">Option 1</option>
        <option value="option2">Option 2</option>
        <option value="option3">Option 3</option>
    </select>
</form>
  • background-color: #fff;: Sets a white background for the select dropdown.
  • border-radius: 4px;: Gives rounded corners to the select box.


Styling Buttons

<style>
button {
    background-color: #4CAF50; /* Green background */
    color: white; /* White text */
    padding: 10px 20px;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    font-size: 16px;
}

/* Change background on hover */
button:hover {
    background-color: #45a049;
}
</style>
<form>
    <button type="submit">Submit</button>
    <button type="reset">Reset</button>
</form>
  • background-color: #4CAF50;: Sets the background color of the button.
  • cursor: pointer;: Changes the cursor to a pointer when hovering over the button.
  • :hover: Changes the button's background color when the user hovers over it.


Styling the Form Layout

<style>
form {
    max-width: 600px;
    margin: auto;
    padding: 20px;
    background-color: #f2f2f2;
    border-radius: 8px;
}

/* Style the labels */
label {
    font-size: 18px;
    margin-bottom: 8px;
    display: block;
}

/* Add spacing between form elements */
input, textarea, select, button {
    margin-bottom: 20px;
}
</style>
<form>
    <label for="name">Name:</label>
    <input type="text" id="name" name="name">
    
    <label for="email">Email:</label>
    <input type="email" id="email" name="email">
    
    <label for="message">Message:</label>
    <textarea id="message" name="message"></textarea>
    
    <label for="options">Options:</label>
    <select id="options" name="options">
        <option value="option1">Option 1</option>
        <option value="option2">Option 2</option>
        <option value="option3">Option 3</option>
    </select>
    
    <button type="submit">Submit</button>
</form>
  • max-width: 600px;: Limits the form's width to 600 pixels.
  • margin: auto;: Centers the form horizontally on the page.
  • background-color: #f2f2f2;: Adds a light background color to the form.
  • border-radius: 8px;: Rounds the corners of the form.
  • label: Makes sure that the label is displayed above the input field and adds some spacing.

You can also style the overall layout of the form.