码迷,mamicode.com
首页 > Web开发 > 详细

DIV实现CSS 的placeholder效果

时间:2016-09-23 12:55:14      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:

placeholder 是HTML5中input的属性,但该属性并不支持除input以外的元素
 
但我们可以使用Css before选择器来实现完全相同的效果
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Div+placeholder</title>
	<style>
	.rich{
	    color:balck;
	    width:100px;
	    height:100px;
	    border: 1px solid red;
	    margin: 20px 50%;
	}
	/*为空时显示 element attribute content*/
	.rich:empty:before{
	    content: attr(placeholder);   /* element attribute*/
	    /*content: ‘this is content‘;*/
	    color:#red;
	}
	/*焦点时内容为空*/
	.rich:focus:before{
	    content:none;
	}
	</style>
</head>
<body>
	<div class=‘rich‘ contenteditable="true" placeholder=‘当 element 内容为空时,我就会出现‘></div>
</body>
</html>

  

 
效果:

技术分享

 
参考:
 

 



DIV实现CSS 的placeholder效果

标签:

原文地址:http://www.cnblogs.com/huangtailang/p/5899278.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!