Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> CSS

วิธีการเปลี่ยนสีตัวยึดตำแหน่งสำหรับกล่องข้อความใน CSS


การใช้ ::placeholder pseudo-element เราสามารถเปลี่ยนสีข้อความตัวยึดตำแหน่งสำหรับกล่องข้อความได้

ไวยากรณ์ของ CSS ::placeholder pseudo-element มีดังนี้ -

::placeholder {
   attribute: /*value*/
}

ตัวอย่าง

ตัวอย่างต่อไปนี้แสดง CSS ::placeholder pseudo-element

<!DOCTYPE html>
<html>
<head>
<style>
input:last-child::placeholder {
   color: cornflowerblue;
}
</style>
</head>
<body>
<input type="text" placeholder="Default color" />
<input type="text" placeholder="Check my new color :D" />
</body>
</html>

ผลลัพธ์

สิ่งนี้จะให้ผลลัพธ์ดังต่อไปนี้ -

วิธีการเปลี่ยนสีตัวยึดตำแหน่งสำหรับกล่องข้อความใน CSS

ตัวอย่าง

<!DOCTYPE html>
<html>
<head>
<style>
input::placeholder {
   color: fuchsia;
}
input {
   margin: 2%;
   width: 100%;
}
div {
   display: flex;
   flex-direction: column;
   margin: 3%;
   padding: 3%;
   text-align: center;
   align-items: center;
   box-shadow: inset 0 0 30px brown;
}
button {
   width: 40%;
}
</style>
</head>
<body>
<div>
<input type="text" placeholder="Normal placeholder" />
<input type="email" placeholder="somenone@somewhere.com" />
<button>dummy btn</button>
</div>
</body>
</html>

ผลลัพธ์

สิ่งนี้จะให้ผลลัพธ์ดังต่อไปนี้ -

วิธีการเปลี่ยนสีตัวยึดตำแหน่งสำหรับกล่องข้อความใน CSS