HTML & CSSEasy
What is the <form> element?
<form> groups interactive controls that collect user input and submits them to a URL (action) using an HTTP method (method="get" or "post"). It gives you built-in behaviors: submission on Enter, native validation, FormData serialization, reset, and accessibility semantics, without JavaScript.
Key attributes
action: where to send the data (defaults to the current URL).method:get(data in the query string, for searches and filters) orpost(data in the body, for changes).dialogcloses a parent<dialog>.enctype:application/x-www-form-urlencoded(default) ormultipart/form-data(required for file uploads).novalidate: skip native validation (when you render custom messages).autocomplete:on/offfor the whole form.target,name,rel,accept-charset.
How submission works
- The user presses a submit button or Enter in a text field.
- Constraint validation runs (
required,type,pattern,min/max). - A
submitevent fires; JavaScript can callevent.preventDefault()to handle it. - Controls with a
nameare serialized; disabled controls and those withoutnameare skipped.
Working with JavaScript
new FormData(form)reads all values;Object.fromEntries(formData)makes an object.form.requestSubmit()submits with validation;form.submit()skips validation and the event.form.reset()restores default values.- Frameworks build on this (React 19
<form action={fn}>, Next.js Server Actions, React Router<Form>).
Accessibility
Every control needs a label, group related controls with <fieldset> and <legend>, and use a real <button type="submit">.
Code example
html
1<style>2 body { font-family: system-ui; }3 form { display: grid; gap: 10px; max-width: 320px; }4 fieldset { border: 1px solid #cbd5e1; border-radius: 8px; }5</style>6<form id="signup" action="/api/signup" method="post">7 <label>Name <input name="name" required autocomplete="name"></label>8 <label>Email <input type="email" name="email" required autocomplete="email"></label>9 <fieldset>10 <legend>Plan</legend>11 <label><input type="radio" name="plan" value="free" checked> Free</label>12 <label><input type="radio" name="plan" value="pro"> Pro</label>13 </fieldset>14 <label><input type="checkbox" name="terms" required> I accept the terms</label>15 <button type="submit">Create account</button>16 <button type="reset">Reset</button>17</form>18<pre id="out"></pre>19<script>20 document.getElementById("signup").addEventListener("submit", (e) => {21 e.preventDefault(); // handle with JS for the demo (valid data only reaches here)22 document.getElementById("out").textContent = JSON.stringify(Object.fromEntries(new FormData(e.target)), null, 2);23 });24</script>
Practice this question
Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the HTML & CSS set.