---
title: "Comment Form"
description: "Collects a new comment or reply - a growing Textarea beside the avatar, guest name and email fields, native validation, a compact reply form."
slug: "comment-form"
---

import { DocPage, PageHeader, SkillPanel, SourceFiles, DocLink, SkillField, SkillText, SkillRow, ApiPill } from "../../lib/components/index";

<DocPage meta={meta}>

<PageHeader slug="comment-form" title="Comment Form" component="comment-form">
  Collects a new comment or reply - a growing Textarea beside the avatar, guest name and email fields, native validation, a compact reply form. Part of the <DocLink href="website-comments.html">Comments</DocLink> blocks.
</PageHeader>

<SkillPanel component="comment-form">
  <SkillField label="Native basis">
<SkillText>A <code>&lt;form&gt;</code>: the writer's Avatar beside a Textarea that grows with its text (<code>field-sizing: content</code>), optional name / email Inputs for guests, a hint and the submit Button. Native validation - <code>required</code>, <code>minlength</code>, <code>type="email"</code>.</SkillText>
  </SkillField>

  <SkillField label="Built from">
<SkillText><DocLink href="textarea.html">Textarea</DocLink>, <DocLink href="input.html">Input</DocLink>, <DocLink href="label.html">Label</DocLink>, <DocLink href="avatar.html">Avatar</DocLink>, <DocLink href="button.html">Button</DocLink>, <DocLink href="tabs.html">Tabs</DocLink>, <DocLink href="toggle-group.html">Toggle Group</DocLink>, <DocLink href="separator.html">Separator</DocLink>, <DocLink href="kbd.html">Kbd</DocLink>, <DocLink href="alert.html">Alert</DocLink>, <DocLink href="rating.html">Rating</DocLink>.</SkillText>
  </SkillField>

  <SkillField label="Web Platform APIs">
<SkillRow>
    <ApiPill href="https://developer.mozilla.org/en-US/docs/Web/CSS/field-sizing" title="the textarea grows"><code>field-sizing: content</code></ApiPill>
    <ApiPill href="https://developer.mozilla.org/en-US/docs/Web/CSS/:user-invalid" title="errors after an attempt"><code>:user-invalid</code></ApiPill>
    <ApiPill href="https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/required" title="native validation"><code>required / minlength</code></ApiPill>
    <ApiPill href="https://developer.mozilla.org/en-US/docs/Web/CSS/:has" title="the layout without an avatar"><code>:has()</code></ApiPill>
  </SkillRow>
  </SkillField>

  <SkillField label="Classes">
<SkillRow>
    <code>.mk-comment-form</code>
    <code>.mk-comment-form-main</code>
    <code>.mk-comment-form-fields</code>
    <code>.mk-comment-form-footer</code>
    <code>.mk-comment-form-hint</code>
    <code>.mk-comment-form-toolbar</code>
  </SkillRow>
  </SkillField>

  <SkillField label="Data attributes">
<SkillText><code>data-variant="reply"</code>; <code>.mk-comment-form-fields</code> for guest name + email; <code>data-variant="framed"</code>; <code>.mk-comment-form-toolbar</code>.</SkillText>
  </SkillField>
</SkillPanel>

```html example label="Default" hint="The avatar beside a Textarea that grows with its text (field-sizing: content), a hint and the submit Button. Native validation: required + minlength - submit empty and the browser says so." previewStyle="align-items:stretch;"
<form class="mk-comment-form" aria-label="Add a comment" style="max-width: 42rem">
  <span class="avatar"><img class="avatar-image" src="images/mk-portrait.png" alt="" /></span>
  <div class="mk-comment-form-main">
    <textarea
      class="textarea"
      name="comment"
      rows="3"
      required
      minlength="2"
      placeholder="Join the discussion…"
      aria-label="Comment"
      aria-describedby="comment-hint"
    ></textarea>
    <div class="mk-comment-form-footer">
      <p class="mk-comment-form-hint" id="comment-hint">Markdown works. Be kind.</p>
      <button type="submit" class="btn">Post comment</button>
    </div>
  </div>
</form>
```

```html example label="Guest" hint="No avatar - the form takes the full width (:has()). Name and email Inputs for guests sit above the textarea; type=email validates natively." previewStyle="align-items:stretch;"
<form class="mk-comment-form" aria-label="Leave a reply" style="max-width: 42rem">
  <div class="mk-comment-form-main">
    <div class="mk-comment-form-fields">
      <div
        ><label class="label" for="guest-name">Name</label
        ><input class="input" id="guest-name" name="name" autocomplete="name" required
      /></div>
      <div
        ><label class="label" for="guest-email">Email</label
        ><input
          class="input"
          id="guest-email"
          name="email"
          type="email"
          autocomplete="email"
          required
          aria-describedby="guest-hint"
      /></div>
    </div>
    <textarea class="textarea" name="comment" rows="3" required placeholder="Your reply…" aria-label="Reply"></textarea>
    <div class="mk-comment-form-footer">
      <p class="mk-comment-form-hint" id="guest-hint">Your email is never shown.</p>
      <button type="submit" class="btn">Post reply</button>
    </div>
  </div>
</form>
```

```html example label="Reply" hint="data-variant='reply': compact, under a comment - a small avatar and a cancel button." previewStyle="align-items:stretch;"
<form class="mk-comment-form" data-variant="reply" aria-label="Reply to Hannah Lee" style="max-width: 36rem">
  <span class="avatar" data-size="sm"><img class="avatar-image" src="images/mk-portrait.png" alt="" /></span>
  <div class="mk-comment-form-main">
    <textarea
      class="textarea"
      name="reply"
      rows="2"
      required
      placeholder="Reply to Hannah…"
      aria-label="Reply to Hannah Lee"
    ></textarea>
    <div class="mk-comment-form-footer">
      <button type="reset" class="btn" data-variant="ghost" data-size="sm">Cancel</button>
      <button type="submit" class="btn" data-size="sm">Reply</button>
    </div>
  </div>
</form>
```

```html example label="Write and preview" hint="data-variant='framed': one box with Tabs (Write / Preview), a formatting Toggle Group, a Separator and a link Button, the growing textarea and a footer with a Kbd shortcut hint." previewStyle="align-items:stretch;"
<form class="mk-comment-form" data-variant="framed" aria-label="Add a comment" style="max-width: 44rem">
  <span class="avatar"><img class="avatar-image" src="images/mk-portrait.png" alt="" /></span>
  <div class="mk-comment-form-main">
    <div class="tabs">
      <div class="mk-comment-form-toolbar">
        <div class="tab-list" role="tablist" aria-label="Editor">
          <button
            class="tab-trigger"
            role="tab"
            type="button"
            aria-selected="true"
            aria-controls="cf-write"
            id="cf-tab-write"
            >Write</button
          >
          <button
            class="tab-trigger"
            role="tab"
            type="button"
            aria-selected="false"
            aria-controls="cf-preview"
            id="cf-tab-preview"
            tabindex="-1"
            >Preview</button
          >
        </div>
        <div
          class="separator"
          data-orientation="vertical"
          role="separator"
          style="height: 1.25rem; margin-inline: 0.25rem"
        ></div>
        <div class="toggle-group" role="group" aria-label="Formatting" data-type="multiple" data-size="sm">
          <button class="toggle" type="button" aria-pressed="false" aria-label="Bold"
            ><i data-lucide="bold"></i
          ></button>
          <button class="toggle" type="button" aria-pressed="false" aria-label="Italic"
            ><i data-lucide="italic"></i
          ></button>
          <button class="toggle" type="button" aria-pressed="false" aria-label="Code"
            ><i data-lucide="code"></i
          ></button>
        </div>
        <button class="btn" data-variant="ghost" data-size="icon-sm" type="button" aria-label="Insert link"
          ><i data-lucide="link"></i
        ></button>
      </div>
      <div class="tab-content" role="tabpanel" id="cf-write" aria-labelledby="cf-tab-write">
        <textarea
          class="textarea"
          name="comment"
          rows="3"
          required
          minlength="2"
          placeholder="Join the discussion… Markdown works."
          aria-label="Comment"
        ></textarea>
      </div>
      <div class="tab-content" role="tabpanel" id="cf-preview" aria-labelledby="cf-tab-preview" hidden>
        <p style="margin: 0; padding: 0.75rem; font-size: 0.875rem"
          >The preview renders your <strong>Markdown</strong> here.</p
        >
      </div>
    </div>
    <div class="mk-comment-form-footer">
      <p class="mk-comment-form-hint"><kbd class="kbd">⌘</kbd> <kbd class="kbd">Enter</kbd> to post</p>
      <button type="submit" class="btn" data-size="sm">Post comment</button>
    </div>
  </div>
</form>
```

```html example label="Review with a rating" hint="A review form: the Rating component (native radios) above the growing textarea - required, so posting without stars is blocked by the browser." previewStyle="align-items:stretch;"
<form class="mk-comment-form" aria-label="Write a review" style="max-width: 40rem">
  <span class="avatar"><img class="avatar-image" src="images/mk-portrait2.png" alt="" /></span>
  <div class="mk-comment-form-main">
    <fieldset class="rating" aria-label="Your rating">
      <label class="rating-item"
        ><input class="rating-input" type="radio" name="stars" value="1" aria-label="1 star" required
      /></label>
      <label class="rating-item"
        ><input class="rating-input" type="radio" name="stars" value="2" aria-label="2 stars"
      /></label>
      <label class="rating-item"
        ><input class="rating-input" type="radio" name="stars" value="3" aria-label="3 stars"
      /></label>
      <label class="rating-item"
        ><input class="rating-input" type="radio" name="stars" value="4" aria-label="4 stars"
      /></label>
      <label class="rating-item"
        ><input class="rating-input" type="radio" name="stars" value="5" aria-label="5 stars"
      /></label>
    </fieldset>
    <textarea
      class="textarea"
      name="review"
      rows="3"
      required
      placeholder="What did you like? What should change?"
      aria-label="Review"
    ></textarea>
    <div class="mk-comment-form-footer">
      <button type="submit" class="btn">Post review</button>
    </div>
  </div>
</form>
```

```html example label="Signed out" hint="No form for guests - an Alert with the actions in its place." previewStyle="align-items:stretch;"
<div class="alert" role="note" style="max-width: 44rem">
  <svg
    class="alert-icon"
    xmlns="http://www.w3.org/2000/svg"
    width="16"
    height="16"
    viewBox="0 0 24 24"
    fill="none"
    stroke="currentColor"
    stroke-width="2"
    stroke-linecap="round"
    stroke-linejoin="round"
    aria-hidden="true"
  >
    <path d="M7.9 20A9 9 0 1 0 4 16.1L2 22Z" />
  </svg>
  <div class="alert-content">
    <h5 class="alert-title">Join the discussion</h5>
    <p class="alert-description">Sign in to comment - it takes ten seconds with a passkey.</p>
  </div>
  <div class="alert-action"><a class="btn" data-size="sm" href="#">Sign in</a></div>
</div>
```

<SourceFiles component="comment-form" section="css" />

</DocPage>
