How to style input and submit button with CSS?
Here's how to spruce up your input fields and submit buttons with CSS:
Just copy-paste these snippets to give your form a quick makeover. Feel free to tweak the padding
, border
, and background-color
to match your theme.
Specific targeting: Dial the CSS aim
Pinpoint styling with attribute selectors
Avoid friendly fire, use attribute selectors to accurately style the right form elements. Use type
attribute to explicitly target text inputs:
Guide user's attention with focus styling
Guide user attention with a beacon - the :focus
pseudo-class. It highlights the currently active text field. Like a lighthouse in the night:
Submit buttons: Just got an upgrade
Submit buttons require a mix of contrast, padding, and perhaps even gradients. Here's how to achieve that:
Refining interactivity: Don't keeps users guessing
Hover effect: Form elements playing peekaboo
A :hover
state shouldn't just be an afterthought. It guides users through your form with a welcome mat.
Button element: The Swiss army knife
Consider replacing <input type="submit">
with <button>
for more styling options. It opens up possibilities like placing images or SVG icons inside the button.
Labels: No more guessing games!
Good label styling helps users quickly understand what information goes where. Here's a quick example:
Responsive like a cat: Make forms fit all screens
Keeping form fluid: Avoid spilling outside the viewport
Use relative units (%
, em
, rem
) instead of pixels (px
) to keep your form fitting snugly in all screen sizes.
Flexing with media queries: Not just posing, it's practical!
Media queries are your form's secret weapon for transforming seamlessly across all device sizes:
Test and share: Show off your form prowess with JSFiddle
JSFiddle is a fantastic tool to get feedback from the community and keep improving. Share your work-in-progress and grow together!
Was this article helpful?