HTML5 Placeholder實現input背景文字提示效果

來源:互聯網
上載者:User

標籤:style   blog   http   java   color   使用   

這篇文章我們來看看什麼是input輸入框背景文字提示效果,如所示:

這種效果現在網上非常的普遍流行,但大部分是使用JavaScript實現的。但HTML5給我們提供了新的純HTML的實現方式,不需要任何的JavaScript,只需要在你的input文字框的標記上添加HTML5規範裡新增的placeholder屬性,然後在屬性值裡輸入你需要的提示資訊。

文法基本是這個樣子:<input placeholder=”提示資訊...”>

HTML代碼
<form><input type="text" placeholder="你的姓名..." name="lname"><input type="password" placeholder="你的密碼..." name="pass"><input type="submit" value="提交"></form>
用CSS美化Placeholder提示資訊的樣式

CSS3裡有相應的通用的對Placeholder提示資訊美化的方法。你可以設定提示資訊文字的顏色,透明度,背景色等。

為了最大化的相容所有瀏覽器,給CSS裡的placeholder屬性添加瀏覽器引擎首碼還是有必要的。

- Google瀏覽器(Webkit): ::-webkit-input-placeholder pseudo-element;

- IE10: :-ms-input-placeholder pseudo-class;

- Firefox瀏覽器(Gecko18-): :-moz-placeholder pseudo-class;

- Firefox瀏覽器(Gecko19+): ::-moz-placeholder pseudo-element;

- Opera(Presto): 無。

/* WebKit browsers */::-webkit-input-placeholder {    color: red;}/* Mozilla Firefox 4 to 18 */:-moz-placeholder {    color: red;    opacity: 1;}/* Mozilla Firefox 19+ */::-moz-placeholder {    color: red;    opacity: 1;}/* Internet Explorer 10+ */:-ms-input-placeholder {    color: red;}
對Placeholder的瀏覽器支援情況

各種瀏覽器對HTML5/CSS3裡出現的新特徵、新屬性的支援是逐步提高的,最終所有的瀏覽器都會完全支援HTML5/CSS3,這是毫無疑問的。目前,對HTML5的placeholder屬性支援的瀏覽器有Google瀏覽器、Firefox瀏覽器、Safari、Opera,還有IE10+。

原文來自:webhek

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.