首先说一下textarea的高度随文字的内容自适应,用div模拟textarea。直接看代码。其中
contenteditable="true"表示div可以编辑。。主要是设置
overflow: auto;
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <style type="text/css"> .textarea{ width: 300px; border:1px solid #ccc; min-height:30px; overflow: auto; outline:0; } </style> </head> <body> <div class="textarea" contenteditable="true">textarea</div> </body> </html>
再说下外层的div随内层的div高度自适应。直接看代码。起到作用的是在外层div的样式上加display:table
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <style type="text/css"> .box{ width: 800px;height: 400px; border: 1px solid #000; position: relative; display: table; } .textarea{ width: 300px; border:1px solid #ccc; min-height:30px; overflow: auto; outline:0; } </style> </head> <body> <div class="box"> <div class="textarea" contenteditable="true">textarea</div> </div> </body> </html>
时间: 2024-09-29 09:10:40