多行文本框深度掌握:overflow 滾動條、scrollbar 顏色 (舊 IE)、resize 調整、HTML5 新屬性
`
1. 滾動條控制 (overflow 系列)
| 屬性 | 作用 | 值 | 示例 |
overflow | 綜合控制 | `visible`(默認) `hidden` `scroll` `auto` | `overflow:auto` 內容超出才顯示滾動條 |
overflow-x | 水平滾動 | 同上 | `overflow-x:hidden` 禁止水平滾動 |
overflow-y | 垂直滾動 | 同上 | `overflow-y:scroll` 強制垂直滾動條 |
<!-- 頁面級禁用滾動 -->
<body style="overflow:hidden"> ... </body>
<body style="overflow-x:hidden; overflow-y:hidden"> ... </body>
<!-- textarea 禁用滾動 -->
<textarea style="overflow:hidden"></textarea>
<textarea style="overflow-x:hidden; overflow-y:hidden"></textarea>
2. IE 專有滾動條顏色屬性 (已廢棄,僅 IE/舊 Edge 支持)
| 屬性 | 作用部位 | 示例 |
scrollbar-3d-light-color | 立體滾動條亮邊 | `#fff` |
scrollbar-arrow-color | 上下按鈕三角箭頭 | `#000` |
scrollbar-base-color | 基礎顏色 (一鍵套用) | `red` / `lightsalmon` |
scrollbar-dark-shadow-color | 強陰影 | `#333` |
scrollbar-face-color | 凸出部分 (滑塊) | `#ccc` |
scrollbar-highlight-color | 空白部分/高光 | `#fff` |
scrollbar-shadow-color | 陰影 | `#999` |
<!-- 單獨設置基礎色 -->
<body style="scrollbar-base-color:red">
<textarea style="scrollbar-base-color:lightsalmon"></textarea>
<!-- 組合效果 -->
<body style="scrollbar-arrow-color:yellow; scrollbar-base-color:lightsalmon">
<!-- CSS 類複用 -->
<style>
.coolscrollbar { scrollbar-arrow-color:yellow; scrollbar-base-color:lightsalmon; }
</style>
<textarea class="coolscrollbar"></textarea>
3. 現代 CSS 滾動條樣式化 (標準化,Chrome/Edge/Firefox/Safari 支持)
<style>
/* WebKit/Blink (Chrome, Edge, Safari) */
::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-track { background: #f1f1f1; border-radius: 4px; }
::-webkit-scrollbar-thumb { background: #c1c1c1; border-radius: 4px; }
::-webkit-scrollbar-thumb:hover { background: #a1a1a1; }
::-webkit-scrollbar-corner { background: transparent; }
/* Firefox 64+ */
* { scrollbar-width: thin; scrollbar-color: #c1c1c1 #f1f1f1; }
/* thin/auto/none */
/* textarea 專用 */
textarea {
scrollbar-width: thin;
scrollbar-color: #c1c1c1 #f1f1f1;
}
textarea::-webkit-scrollbar { width: 8px; }
textarea::-webkit-scrollbar-thumb { background: #c1c1c1; border-radius: 4px; }
</style>
4. textarea 尺寸與外觀控制
<textarea
style="border:1px solid red; border-width:1px; color:#CCCCCC;"
rows="5" cols="30"></textarea>
<!-- HTML5 新屬性 -->
<textarea
name="content"
rows="5"
cols="30"
maxlength="500" <!-- 最大字符數 -->
minlength="10" <!-- 最小字符數 -->
required <!-- 必填 -->
placeholder="請輸入內容..."
wrap="soft" <!-- soft(默認)/hard(提交時硬換行) -->
autocomplete="off" <!-- 關閉自動填充 -->
spellcheck="false" <!-- 禁用拼寫檢查 -->
dirname="content.dir" <!-- 提交書寫方向 -->
readonly <!-- 只讀 -->
disabled <!-- 禁用 -->
></textarea>
5. 自適應高度 (隨內容增長) - 現代最佳方案
<style>
.auto-resize {
width: 100%;
min-height: 100px;
border: 1px solid #ddd;
border-radius: 4px;
padding: 10px;
font-family: inherit;
font-size: 14px;
line-height: 1.5;
box-sizing: border-box;
resize: vertical; /* 允許用戶垂直拖拽 */
overflow: hidden; /* 關鍵:隱藏滾動條,配合 JS 撐開 */
}
</style>
<textarea class="auto-resize" id="auto-textarea" placeholder="輸入內容,高度自適應..."></textarea>
<script>
const textarea = document.getElementById('auto-textarea');
function autoResize() {
textarea.style.height = 'auto'; // 重置
textarea.style.height = textarea.scrollHeight + 'px'; // 設置為內容高度
}
// 初始化 + 輸入監聽
textarea.addEventListener('input', autoResize);
autoResize(); // 初始內容高度
</script>
6. 表單提交與驗證整合
<form id="article-form">
<textarea name="content" required minlength="50" maxlength="5000"
placeholder="至少 50 字"></textarea>
<span class="error" aria-live="polite"></span>
<button type="submit">發布</button>
</form>
<script>
document.getElementById('article-form').addEventListener('submit', (e) => {
const textarea = e.target.content;
if (!textarea.checkValidity()) {
e.preventDefault();
textarea.reportValidity(); // 瀏覽器原生提示
}
});
</script>