使用 HTML 和 CSS 对齐文本和选择框的宽度


当我们在 度和 度中设置 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>

更新于:23-Nov-2023

1K+ 视图

开启您的 职业

完成课程认证获得资格

开始
广告
© . All rights reserved.