使用 HTML 和 CSS 将文本和选择框对齐到相同宽度

htmlweb developmentfront end technology更新于 2025/8/18 7:08:17

当我们在 CSS 中设置元素的宽度和高度时,元素通常看起来比实际大小要大。这是因为默认情况下,元素的内边距和边框会添加到元素的宽度和高度上,然后元素才会显示。

盒子尺寸属性包含元素的内边距和边框以及实际宽度和高度,这样元素看起来就不会比实际大小要大。使用此属性的格式为"box-sizing: box-border"

示例

您可以尝试运行以下代码,将文本和选择框对齐到相同宽度 −

<html>
   <head>
      <style>
         input, select {
            width: 250px;
            border: 2px solid #000;
            padding: 0;
            margin: 0;
            height: 22px;
            -moz-box-sizing: border-box;
            -webkit-box-sizing: border-box;
            box-sizing: border-box;
         }
         input {
            text-indent: 3px;
         }
      </style>
   </head>
   <body>
      <input type = "text" value = "Name of Candidate"><br>
      <select>
         <option>Select Choice</option>
         <option>Student</option>
         <option>Teachers</option>
         <option>Head</option>
      </select>
   </body>
</html>

相关文章


有用资源