如何消除表单标签前后的额外空格?


在本文中,我们将学习如何消除表单 HTML 元素前后的额外空格,以确保表单布局简洁紧凑。

在设计 Web 应用程序时,控制间距和布局至关重要,以创建视觉上令人愉悦且组织良好的用户界面。有时,表单标签 <form> 前后会出现额外的空格,这可能会影响表单元素的整体设计和对齐。

让我们通过一些示例了解如何去除额外的空格。

示例 1

在本例中,我们将通过将表单标签的边距和填充设置为 0 来去除额外的空格。

文件名: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>

示例 2

在本例中,我们将遵循上述代码模式,并向表单标签应用内联样式以去除其任何边距和填充。

文件名: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>

示例 3

在本例中,我们将遵循上述代码模式,并通过使用 Normalize.css CDN 重置 CSS 来去除表单标签的空格。

文件名: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>

示例 4

在本例中,我们将遵循上述代码模式,并使用 4 种不同的方法消除表单标签的额外空格,包括使用 CSS flexbox、将父元素的字体大小设置为 0px、使用负边距以及去除 HTML 中的换行符和空格。

文件名: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>

结论

总之,消除表单标签 <form> 前后的额外空格对于在 Web 设计中实现简洁紧凑的表单布局至关重要。通过理解和应用本文中讨论的技术,我们学习了如何去除表单标签前后任何额外的空格,以呈现干净的用户界面。

更新于: 2023年8月3日

690 次浏览

开启你的 职业生涯

通过完成课程获得认证

开始学习
广告