使用 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+ 檢視

開啟您的 職業

完成課程認證獲得資格

開始
廣告