利用CSS改變輸入框的光標顏色


UED給出了一個需求,希望<input>被選中時,將光標變為綠色。我趕緊搜索解決方案,發現思路大多是:

input {
    color: #0f0;
}

這種方法的確能改變光標的默認顏色,但是負面作用是,輸入的文字也變成了綠色。

幸運的是在Stack overflow上找到了一個靠譜的答案:

input,
textarea {
    font-size: 24px;
    padding: 10px;
    
    color: #0f0;
    text-shadow: 0px 0px 0px #000;
    -webkit-text-fill-color: transparent;
}

input::-webkit-input-placeholder,
textarea::-webkit-input-placeholder{
    text-shadow: none;
    -webkit-text-fill-color: initial;
}

完整代碼如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style type="text/css">
        input,
        textarea {          
            color: #0f0;
            text-shadow: 0px 0px 0px #000;
            -webkit-text-fill-color: transparent;
        }

        input::-webkit-input-placeholder,
        textarea::-webkit-input-placeholder{
            text-shadow: none;
            -webkit-text-fill-color: initial;
        }
    </style>
</head>
<body>
    <input type="text" placeholder="test">
    <p></p>
    <textarea name="" cols="30" rows="10"></textarea>
</body>
</html>

參考自:http://jsfiddle.net/8k1k0awb/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM