Card: Styling Forms

Styling Forms

Add a Touch of Brilliance with CSS

Want to turn mundane forms into true masterpieces? With the right magical incantations, your input fields, buttons, and text areas can transform into sparkling gems. A dash of padding, and your content floats as gracefully as clouds. A refined border, and your form looks as though it’s wrapped in the finest parchment. Add a background-color, and you’ll weave colors that make your page shine.

Here, you’ll learn how to enchant your form fields with a touch of magic, making them glitter with elegance and usability. Every input field may present a small challenge, but with the right spells, you’ll craft a form that’s a feast for the eyes!

Forms in CSS

Style, optimize, highlight – make inputs user-friendly and beautiful!

General Styles for Forms

Use What You’ve Learned

Borders and Spacing
A great form starts with a sturdy frame, much like the walls of a wizard’s academy holding everything together. Use border for the walls, padding for cozy interiors, and margin to keep curious visitors at bay.

Backgrounds, Colors, and Shadows
The background is the canvas for your magical scroll. Use background-color to set the mood—mystical, cheerful, or dramatic—and color to conjure words that dance upon it. Shadows (box-shadow) add depth, making your form stand out like an enchanted relic.

Fonts and Sizes
No epic tale is complete without majestic fonts. Wield font-family as your style wand, and let font-size ensure even the dragon in the farthest cave can read your words.

Alignment
Use text-align to line up your words neatly. If elements lose their place, vertical-align restores order, ensuring harmony across your design.

Interactions
With spells like :hover, :focus, and :active, bring your fields to life. A touch of magic, and they glow, shift, or invite interaction like portals to another realm.

Transitions and Animations
No mage enters the stage without a flair for showmanship. Use transition to make colors and shadows glide seamlessly from one state to another, as though a portal is opening before your very eyes.

Flexbox and Grid for Layout
Think of display: flex and grid-template-areas as the architects of your magic. They align every element, creating a harmonious and structured symphony of your form’s design.

Token: input:focus

Form Fields in Focus

Activate the Magical Glow

When a form field is activated by the user, the focus state comes into play. This is your chance to add a touch of magic and make the field shine with a special glow. Change the border color, background, or other styles to highlight it. After all, everyone should know that this field is now in the spotlight!

input:focus {
    border-color: orange;
    background-color: black;
}

Here, you style your inputs to change their border and background color in the focus state

Token: input::placeholder

Stylish Instructions

Grant the Placeholder Elegance

Placeholder text doesn’t have to stay invisible! With the ::placeholder pseudo-element, you can style it to blend elegantly into your website’s design. Adjust the color, font, and size to your liking. This transforms the placeholder into a whispering guide that gently directs the user without disrupting your site’s aesthetic.

input::placeholder {
    color: #9e9e9e;
    font-style: italic;
}

This example gives the placeholder text a gray color and italic font

Token: input type

Type

The Hidden Magic of Input Attributes

With the powerful attributes of input types, you can unleash precise styling spells. For example, input[type=”text”] lets you target text fields specifically without needing individual classes for each field. This precise magic allows you to style different input types like text, email, or password individually, ensuring every field gets the attention it deserves and your form radiates a harmonious design.

input[type="text"] {
    padding: 10px;
    border: 2px dotted #ccc;
    border-radius: 5px;
    background-color: purple;
    color: white;
}

Here, you style text input fields with specific styles

CSS
/* Basic Designs - Input-Fields */
input[type="text"],
input[type="number"],
textarea {
    width: 100%;
    padding: 10px;
    margin: 5px 0;
    border: 2px solid darkgreen;
    border-radius: 5px;
    background-color: #f0f0f0;
    color: black;
}

/* Placeholder */
input::placeholder,
textarea::placeholder {
    color: grey;
    opacity: 0.7;
}

/* Hover-Effect */
input[type="text"]:hover,
input[type="number"]:hover,
textarea:hover {
    border-color: green;
    background-color: #e0ffe0;
}

/* Focus-Effect */
input[type="text"]:focus,
input[type="number"]:focus,
textarea:focus {
    border-color: darkblue;
    background-color: white;
    outline: none;
}

/* Button*/
button {
    padding: 10px 20px;
    background-color: darkgreen;
    color: white;
    border: none;
    border-radius: 5px;
    cursor: pointer;
}

button:hover {
    background-color: green;
}
Scroll to Top