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

UED給出了一個需求,但願<input>被選中時,將光標變爲綠色。我趕忙搜索解決方案,發現思路大可能是:css

input {
    color: #0f0;
}

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

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

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;
}

完整代碼以下:spa

<!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/.net

相關文章
相關標籤/搜索