亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么 textarea 和普通 div 邊框的寬度存在差異?

為什么 textarea 和普通 div 邊框的寬度存在差異?

呼如林 2023-07-20 11:05:08
我正在嘗試制作一個簡單的 html div,其邊框與文本區域一起擴展和收縮。我設法編寫了所需的 js 代碼來做到這一點,但我總是在 2 個元素之間(在它們展開時在它們的默認位置處)出現大約 2-3 個像素的小間隙*。有什么想法如何解決它嗎?*代碼片段中似乎沒有間隙,但當我在 chrome、FF 和 Edge 中嘗試時有間隙。證明:<html><body>  <div id="small-box"></div>  <textarea id="textarea" onmousemove="resizeBoxWidth()"></textarea>  <style>    #small-box {      font-size: 30;      text-align: center;      line-height: 3.5;      top: 40px;      left: 40px;      width: 450px;      height: 100px;      border-color: black;      border-style: solid;      border-width: 3px;      position: absolute;    }        #textarea {      min-height: 100px;      max-height: 100px;      border-width: 3px;      border-color: black;      top: 150px;      left: 40px;      width: 450px;      height: 100px;      position: absolute;    }  </style>  <script>    function resizeBoxWidth() {      var smallBox = document.getElementById("small-box");      var textarea = document.getElementById("textarea");      smallBox.style.width = textarea.style.width;    }  </script></body></html>
查看完整描述

2 回答

?
大話西游666

TA貢獻1817條經驗 獲得超14個贊

這是由于box-sizing,它定義了如何width計算,

默認情況下,在 CSS 盒模型中,分配給元素的寬度和高度僅應用于該元素的內容框。如果元素有任何邊框或填充,則會將其添加到寬度和高度,以達到在屏幕上呈現的框的大小。

如果為每個元素提供相同的box-sizing設置,則將以相同的方式計算大小并顯示相同的大小。

function resizeBoxWidth() {

? var smallBox = document.getElementById("small-box");

? var textarea = document.getElementById("textarea");

? smallBox.style.width = textarea.style.width;

}

#small-box {

? font-size: 30;

? text-align: center;

? line-height: 3.5;

? top: 40px;

? left: 40px;

? width: 450px;

? height: 100px;

? border-color: black;

? border-style: solid;

? border-width: 3px;

? position: absolute;

? box-sizing: border-box;

}


#textarea {

? min-height: 100px;

? max-height: 100px;

? border-width: 3px;

? border-color: black;

? top: 150px;

? left: 40px;

? width: 450px;

? height: 100px;

? position: absolute;

? box-sizing: border-box;

}

<div id="small-box"></div>

<textarea id="textarea" onmousemove="resizeBoxWidth()"></textarea>


查看完整回答
反對 回復 2023-07-20
?
元芳怎么了

TA貢獻1798條經驗 獲得超7個贊

通過使用檢查工具的短視圖,我發現文本區域的默認包含padding: 2px;會影響文本區域的位置和寬度。

與不包含任何 pervious 屬性的 div 不同。

查看完整回答
反對 回復 2023-07-20
  • 2 回答
  • 0 關注
  • 181 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號