Create a Comment Box in HTML and CSS

This tutorial explains how to create a simple comment box input in HTML, CSS, and JavaScript. Basically, it is a frontend functionality (that can be handled in both jQuery and Vue JS) to quickly append a comment to the webpage. Further, it can be submitted to the server through JSON to publish a comment in realtime.

You can use this simple project for appending strings to the page in comment forms, to-do lists, or feedback forms. Similarly, you can use this technique in any web project to quickly append the text to the page. Before starting with coding I would suggest checking the demo page to check the comment form in action.

This comment form only appends the comment text to a specific div element without actually publishing it on the page. So, the entered text will be lost after refreshing the page. You have to submit (and store) the comment to the server for permanent.

Comment Box HTML Code

In HTML, create a section with an id "app" and place a textarea (for comment input) and p element for comment output.

<section id="app">
    <div class="container">
      <div class="row">
        <div class="col-6">
          <div class="comment">
        <p v-for="items in item" v-text="items"></p>
          </div><!--End Comment-->
          </div><!--End col -->
          </div><!-- End row -->
      <div class="row">
        <div class="col-6">
      <textarea type="text" class="input" placeholder="Write a comment" v-model="newItem" @keyup.enter="addItem()"></textarea>
          <button v-on:click="addItem()" class='primaryContained float-right' type="submit">Add Comment</button>
        </div><!-- End col -->
      </div><!--End Row -->
    </div><!--End Container -->
  </section><!-- end App -->

The CSS Styles

After creating the HTML structure, now it’s time to style it using CSS. So, target the very first element "container" class and define its max-width 640px. Similarly, define the background, margin, and padding property as follows:

.container {
	max-width: 640px;
	margin: 30px auto;
	background: #fff;
	border-radius: 8px;
	padding: 20px;

Now, target the "comment" class and display it as a block element. Likewise, specify min-height, border, and padding property for commentClicked class ad described below:

.comment {
	display: block;
	transition: all 1s;
.commentClicked {
	min-height: 0px;
	border: 1px solid #eee;
	border-radius: 5px;
	padding: 5px 10px

You can style the textarea/comment box according to your needs. Anyhow, the basic styles of the comment input are given below:

.container textarea {
	width: 100%;
	border: none;
	background: #E8E8E8;
	padding: 5px 10px;
	height: 50%;
	border-radius: 5px 5px 0px 0px;
	border-bottom: 2px solid #016BA8;
	transition: all 0.5s;
	margin-top: 15px;

The "primaryContained" class defined the style for the comment submit button. Specify its background color, padding, box-shadow, and border-radius property as follows. You can set the custom color and font-size in order to customize it.

button.primaryContained {
	background: #016ba8;
	color: #fff;
	padding: 10px 10px;
	border: none;
	margin-top: 0px;
	cursor: pointer;
	text-transform: uppercase;
	letter-spacing: 4px;
	box-shadow: 0px 2px 6px 0px rgba(0, 0, 0, 0.25);
	transition: 1s all;
	font-size: 10px;
	border-radius: 5px;

Target the button class with a hover pseudo-selector in order to define the hover styles. There you can set a different background color that will show on the hover event. You can also set the 3D button hover effect if you want to make a mindblowing hover effect for your comment form.

button.primaryContained:hover {
	background: #9201A8;

The JavaScript for Displaying Comment

You can handle the comment string in both jQuery and Vue JS to submit and append the comment. So, load the jQuery and Vue JavaScript libraries by adding the following CDN link to your HTML page.

<!-- Vue JS -->
<script src=''></script>
<!-- jQuery -->
<script src=''></script>

Finally, initialize the comment append function in the jQuery document ready function and done.

    $(".primaryContained").on('click', function(){
  });//end click
  $("textarea").on('keyup.enter', function(){
  });//end keyup
  });//End Function

new Vue({
    el: "#app",
       title: 'Add a comment',
      newItem: '',
      item: [],
      addItem  (){
        this.newItem = "";


That’s all! I hope you have successfully created a simple comment box using HTML, CSS, and JS. If you have any questions or suggestions, let me know by comment below.

You May Also Like

4 thoughts on “Create a Comment Box in HTML and CSS”

  1. Thank you so much for this tutorial. This is exactly what I was needing for my project.

    One question, what would be the easiest way to get the comments to be persistent? When I refresh the screen, they are gone.

    • Hi Nathaniel!
      In order to save comments on the server, you need to do a couple of things. First of all, create a table in your database named “comments”. You can run the following SQL query to create a table with the necessary columns.

      CREATE TABLE comments(
          username VARCHAR(50) NULL UNIQUE,
          email VARCHAR(255) NULL UNIQUE,
          comment TEXT NULL,

      After that create a “post-comment.php” file and paste the following script inside it to receive comments.

      //Define variables for requested input fields
      $username = $email = $comment = "";
      if ($_SERVER["REQUEST_METHOD"] == "POST") {
              $username = test_input($_POST["complainant-name"]);
              $email = test_input($_POST["bill-account"]);
              $comment = test_input($_POST["amount-payable"]);
              //connect to databse
              define('DB_SERVER', 'localhost');
              define('DB_USERNAME', 'root');
              define('DB_PASSWORD', '');
              define('DB_NAME', 'yourdbname');
              /* Attempt to connect to MySQL database */
              $link = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);
              // Check connection
              if($link === false){
                  die("ERROR: Could not connect. " . mysqli_connect_error());
              $sql = "INSERT INTO comments(
              VALUES (
              if ($link->query($sql) === TRUE) {
                  echo "Your comment has been submitted!";
              else {
                  echo "Error: " . $sql . "<br>" . $link->error;
      else {
      function test_input($data) {
          $data = trim($data);
          $data = stripslashes($data);
          $data = htmlspecialchars($data);
          return $data;

      Finally, add the action attribute with your comment form and done.

Leave a Comment