#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
CSS3 Fundamentals

CSS Forms

Styling forms with CSS turns plain browser inputs into fields that match your design and are comfortable to use on touch screens. Default form controls differ between browsers and are often small, so a few rules for size, spacing, borders and focus states make a big difference.

Selecting the right fields

Inputs share one tag but behave differently, so attribute selectors are very useful here. input[type="text"] targets only text fields, leaving checkboxes and radio buttons alone. You can group several types in one rule. Textareas and selects are separate elements and need to be included explicitly.

A clean base style

input[type="text"],
input[type="email"],
input[type="password"],
select,
textarea {
  width: 100%;
  box-sizing: border-box;
  padding: 10px 12px;
  margin: 6px 0 16px;
  font: inherit;
  border: 1px solid #b8b8c4;
  border-radius: 6px;
  background: #fff;
}

input:focus,
select:focus,
textarea:focus {
  outline: 2px solid #4057d6;
  outline-offset: 1px;
  border-color: #4057d6;
}

textarea {
  resize: vertical;
  min-height: 120px;
}

input::placeholder {
  color: #77778a;
}

input:invalid:not(:placeholder-shown) {
  border-color: #c0392b;
}

button[type="submit"] {
  padding: 10px 20px;
  border: 0;
  border-radius: 6px;
  background: #4057d6;
  color: #fff;
  cursor: pointer;
}

Why box-sizing matters so much here

Without box-sizing: border-box, a field set to width: 100% gets its padding and border added on top of that width. The input ends up wider than its container and pokes out on the right side, which on a phone causes horizontal scrolling. With border-box, padding and border are included inside the declared width. Many developers set this globally with *, *::before, *::after { box-sizing: border-box; }.

Inheriting fonts

Browsers do not let form controls inherit the page font by default. Inputs often appear in a smaller system font, which looks out of place. The font: inherit; line fixes this. Also keep the input font size at 16px or more on mobile; smaller sizes make iOS Safari zoom the page when the field is tapped.

Focus states and accessibility

The biggest styling mistake with forms is removing the focus outline with outline: none because it "looks ugly". Keyboard users rely on that outline to see which field is active. If you remove it, replace it with something equally visible, such as the coloured outline in the example. The :focus-visible pseudo-class lets you show the ring for keyboard users while keeping mouse clicks cleaner.

Styling validation and states

CSS can react to the browser's validation. :invalid matches fields that break their rules (such as a required field that is empty or an email without "@"). Applying a red border to :invalid alone paints every required field red before the user even starts, so combine it with :not(:placeholder-shown) or :user-invalid to wait until there is input. :disabled lets you grey out fields that cannot be used yet.

Checkboxes and radios

Modern browsers support accent-color, which recolours checkboxes, radio buttons and range sliders with a single line, for example accent-color: #4057d6;. That is far simpler than the older trick of hiding the real input and drawing a fake box with pseudo-elements.

Laying out labels and fields

On narrow screens, stacking each label above its field is the easiest layout to read and tap. On wider screens you can place labels beside fields with a two-column grid, for example grid-template-columns: 160px 1fr on the form, switched on inside a media query so phones keep the stacked version.

Takeaway: give fields full width with border-box sizing, inherit the page font, keep a clear focus style, and use :invalid carefully so users only see errors after they have typed something.

Ready to master real-world web development?

Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.

Explore Course