top of page

Form in CSS

  • Writer: Siddharth Sharma
    Siddharth Sharma
  • Mar 2, 2025
  • 2 min read

In CSS, a form can be styled to enhance its appearance, improve user experience, and maintain consistency across a website. Below are different aspects of styling a form using CSS:

1. Basic Form Styling

You can use CSS to control the appearance of form elements like text fields, buttons, checkboxes, radio buttons, and more.

Example: Basic Form Styling

/* Styling the form container */
form {
    width: 50%;
    margin: auto;
    padding: 20px;
    background-color: #f8f8f8;
    border-radius: 8px;
    box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.1);
}

/* Styling labels */
label {
    font-weight: bold;
    display: block;
    margin-bottom: 5px;
}

/* Styling input fields */
input[type="text"],
input[type="email"],
input[type="password"],
textarea {
    width: 100%;
    padding: 10px;
    margin-bottom: 10px;
    border: 1px solid #ccc;
    border-radius: 5px;
    font-size: 16px;
}

/* Styling submit button */
button {
    background-color: #28a745;
    color: white;
    padding: 10px 15px;
    border: none;
    border-radius: 5px;
    cursor: pointer;
    font-size: 16px;
}

button:hover {
    background-color: #218838;
}

HTML Form Example

<form>
    <label for="name">Name:</label>
    <input type="text" id="name" placeholder="Enter your name">

    <label for="email">Email:</label>
    <input type="email" id="email" placeholder="Enter your email">

    <label for="message">Message:</label>
    <textarea id="message" placeholder="Type your message"></textarea>

    <button type="submit">Submit</button>
</form>

2. Styling Form Inputs (Focus, Hover, Placeholder)

Adding Focus Effects

input:focus, textarea:focus {
    border-color: #007bff;
    outline: none;
    box-shadow: 0 0 5px rgba(0, 123, 255, 0.5);
}

Styling Placeholder Text

input::placeholder, textarea::placeholder {
    color: #888;
    font-style: italic;
}

3. Styling Checkboxes & Radio Buttons

Custom checkboxes and radio buttons can make forms more visually appealing.

/* Hide default checkbox */
input[type="checkbox"], input[type="radio"] {
    display: none;
}

/* Custom checkbox */
input[type="checkbox"] + label::before {
    content: "✔";
    display: inline-block;
    width: 20px;
    height: 20px;
    border: 2px solid #007bff;
    text-align: center;
    line-height: 20px;
    color: transparent;
    font-weight: bold;
    margin-right: 5px;
}

/* Checked state */
input[type="checkbox"]:checked + label::before {
    color: #007bff;
}

4. Responsive Form Design

To make the form responsive, use CSS media queries.

@media (max-width: 600px) {
    form {
        width: 90%;
    }
}

5. Styling File Upload Input

input[type="file"] {
    border: none;
    background-color: #f8f8f8;
    padding: 10px;
    cursor: pointer;
}

Conclusion

Using CSS, you can improve the visual appeal of forms by customizing input fields, buttons, checkboxes, and layout. Proper styling ensures a better user experience and mobile responsiveness.

 
 
 

Comments


bottom of page