CSS 偽類 - :read-only



CSS :read-only 偽類選擇使用者無法編輯的元素,例如輸入欄位和文字區域。這包括具有 readonly 屬性的元素。

語法

:read-only {
   /* ... */
}
為了獲得更好的瀏覽器相容性,您可以使用 -moz 和 -webkit 字首。例如:input: -moz-read-only 和 input: -webkit-read-only。

CSS :read-only - 基本示例

以下示例演示瞭如何使用:read-only 偽類 -

<html>
<head>
<style>
   input:read-only {
      background-color: pink;
      border: 1px solid red;
   }
</style>
</head>
<body>
   <label for="readonlyInput">Read-only Input:</label>
   <input type="text" id="readonlyInput" value="This is read-only" readonly>

   <label for="editableInput">Editable Input:</label>
   <input type="text" id="editableInput" value="This is editable">
</body>
</html>

CSS :read-only - 確認表單資訊

這是一個簡單的預訂確認表單示例,它向用戶明確表示欄位為只讀 -

<html>
<head>
<style>
   .form-container {
      width: 300px;
      margin: 0 auto;
   }
   label {
      display: block;
      margin-top: 10px;
   }
   input:read-only {
      background-color: pink;
      border: none;
   }
   .submit-container {
      margin-top: 10px;
   }
   button {
      background-color: red;
      border: none;
      padding: 10px;
      color: white;
   }
</style>
</head>
<body>
   <div class="form-container">
   <h2>Form</h2>
      <form>
      <label for="name">Name:</label>
      <input type="text" id="name" name="name" value="John Doe" readonly>

      <label for="email">Email:</label>
      <input  type="email" id="email" name="email" value="john@example.com" readonly>

      <label for="password">Password:</label>
      <input type="password" id="password" name="password" value="124569763" readonly>
      
      <div class="submit-container">
         <button type="submit">Submit</button>
      </div>
      </form>
   </div>
</body>
</html> 

CSS :read-only - 樣式化只讀非表單控制元件

以下示例演示瞭如何使用:read-only 偽類選擇任何使用者無法編輯的元素。-

<html>
<head>
<style>
   div.read-only-para {
      background-color: pink;
      border: 1px solid red;
   }
   div.editable-para {
      background-color: violet;
      border: 1px solid blue;
   }
</style>
</head>
<body>
   <div class="read-only-para">This is read-only div element</div><br>
   <div class="editable-para" contenteditable>This is editable div element</div>
</body>
</html>
廣告

© . All rights reserved.