How to eliminate extra space before and after a form tag?


In this article, we will learn how to eliminate extra space before and after a form HTML element so as to ensure a clean and compact form layout.

When designing web applications, it's essential to have control over the spacing and layout to create a visually pleasing and well-organized user interface. Sometimes, there is extra space available before and after a <form> tag, which can affect the overall design and alignment of your form elements.

Let’s understand how to remove the extra space with the help of some examples.

Example 1

In this example, we will remove the extra space from the form tag by setting its margin and padding to 0.

Filename: index.html

<html lang="en">
   <head>
      <title>How to eliminate extra space before and after a form tag?</title>
      <style>
         form {
            margin: 0;
            padding: 0;
         }
      </style>
   </head>
   <body>
      <h3>How to eliminate extra space before and after a form tag?</h3>
      <form>
         <label for="name">Name:</label>
         <input type="text" name="name" placeholder="Enter your name" />
         <label for="email">Email:</label>
         <input type="email" name="email" placeholder="Enter your email" />
         <input type="submit" value="Submit" />
      </form>
   </body>
</html>

Example 2

In this example, we will follow the above code pattern and apply inline styles to the form tag to remove any margin and padding from it.

Filename: index.html

<html lang="en">
   <head>
      <title>How to eliminate extra space before and after a form tag?</title>
   </head>
   <body>
      <h3>How to eliminate extra space before and after a form tag?</h3>
      <form style="margin: 0; padding: 0;">
         <label for="name">Name:</label>
         <input type="text" name="name" placeholder="Enter your name" />
         <label for="email">Email:</label>
         <input type="email" name="email" placeholder="Enter your email" />
         <input type="submit" value="Submit" />
      </form>
   </body>
</html>

Example 3

In this example, we will follow the above code pattern and remove the space from the form tag by resetting the CSS with the help of Normalize.css CDN.

Filename: index.html

<html lang="en">
   <head>
      <title>How to eliminate extra space before and after a form tag?</title>
      <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/8.0.1/normalize.min.css">
   </head>
   <body>
      <h3>How to eliminate extra space before and after a form tag?</h3>
      <form>
         <label for="name">Name:</label>
         <input type="text" name="name" placeholder="Enter your name" />
         <label for="email">Email:</label>
         <input type="email" name="email" placeholder="Enter your email" />
         <input type="submit" value="Submit" />
      </form>
   </body>
</html>

Example 4

In this example, we will follow the above code pattern, and eliminate extra space from form tag with the help of 4 different methods, using CSS flexbox, setting font size to 0px on parent element, using negative margin, and removing linebreaks and white spaces in HTML.

Filename: index.html

<html lang="en">
<head>
   <title>How to eliminate extra space before and after a form tag?</title>
   <style>
      /* Example 1: Using CSS Flexbox */
      .flex-container {
         display: flex;
         flex-direction: column;
      }

      /* Example 2: Setting font size to 0 on parent elements */
      .font-zero {
         font-size: 0;
      }

      /* Example 3: Using Negative Margin */
      .negative-margin {
         margin: -10px;
      }

      /* Example 4: Removing line breaks and white spaces in HTML */
         form.no-spacing {
            line-height: 0;
         }
   </style>
</head>
<body>
   <h3>How to eliminate extra space before and after a form tag?</h3>

   <h4>Using CSS Flexbox</h4>
   <!-- Example 1: Using CSS Flexbox -->
   <div class="flex-container">
      <form>
         <label for="name">Name:</label>
         <input type="text" name="name" placeholder="Enter your name" />
         <label for="email">Email:</label>
         <input type="email" name="email" placeholder="Enter your email" />
         <input type="submit" value="Submit" />
      </form>
   </div>
   <br />
   <br />
   <br />

   <h4>USetting font size to 0 on parent elements</h4>
   <!-- Example 2: Setting font size to 0 on parent elements -->
   <div class="font-zero">
      <form>
         <label for="name">Name:</label>
         <input type="text" name="name" placeholder="Enter your name" />
         <label for="email">Email:</label>
         <input type="email" name="email" placeholder="Enter your email" />
         <input type="submit" value="Submit" />
      </form>
   </div>
   <br />
   <br />
   <br />
   <h4>Using Negative Margin</h4>
   <!-- Example 3: Using Negative Margin -->
   <div class="negative-margin">
      <form>
         <label for="name">Name:</label>
         <input type="text" name="name" placeholder="Enter your name" />
         <label for="email">Email:</label>
         <input type="email" name="email" placeholder="Enter your email" />
         <input type="submit" value="Submit" />
      </form>
   </div>
   <br />
   <br />
   <br />
 
   <h4>Removing line breaks and white spaces in HTML</h4>
   <!-- Example 4: Removing line breaks and white spaces in HTML -->
   <form class="no-spacing">
      <label for="name">Name:</label><input type="text" name="name" placeholder="Enter your name" /><label for="email">Email:</label><input type="email" name="email" placeholder="Enter your email" /><input type="submit" value="Submit" />
   </form>
</body>
</html>

Conclusion

In conclusion, eliminating extra space before and after a <form> tag is essential for achieving a clean and compact form layout in web design. By understanding and applying the techniques discussed in this article, we learned how to remove any extra spaces available before and after a form tag to represent a clean User Interface.

Updated on: 03-Aug-2023

456 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements