CSS代碼:
h2 { margin:0; padding:10px 0; font-size:14px; }
.mod-retweet { background:#E4EFF4; border:1px solid #A8D1E0; padding:3px 5px 5px; margin-top:5px; }
.mod-retweet .mod-retweet-textarea { background-color: #FFFFFF; border:1px solid #64B2D1; margin-bottom:5px; overflow:hidden; padding:2px; position:relative; zoom:1 }
.mod-retweet .mod-retweet-textarea textarea {padding: 0;margin: 0; border:0 none; font-size:12px; height:80px; line-height:1.5em; width:100%; display:block }
.mod-retweet .mod-retweet-textarea input { border:0 none; font-size:12px; height:20px; line-height:1.5em; width:100%; }
HTML代碼:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>模擬寬度自適應的輸入框</title>
</meta>
</head>
<body>
<h1>模擬寬度自適應的輸入框</h1>
<div class="mod-retweet">
<h2>模擬input的自適應:</h2>
<div class="mod-retweet-textarea">
<input type="text" name="textfield" id="textfield"/>
</div> </div> <hr />
<div class="mod-retweet"> <h2>模擬textarea的自適應:</h2> <div class="mod-retweet-textarea">
<textarea maxlength="500" rows="5" cols="45" name="bookcontent"></textarea> </div> </div>
</body>
</html>
缺點:
1、textarea 和 input 文本輸入框的邊框是用套在其外層的容器的border來模擬的,textarea 和 input 文本輸入框的本身邊框 border:0 none。這樣webkit下輸入框focus後,輸入框的邊框在模擬的邊框裏面,體驗上有點不爽。
2.textarea的padding和margin一定要重置爲0,否則在webkit和opera下會有細小的bug。