2 回答

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>

TA貢獻1798條經驗 獲得超7個贊
通過使用檢查工具的短視圖,我發現文本區域的默認包含padding: 2px;
會影響文本區域的位置和寬度。
與不包含任何 pervious 屬性的 div 不同。
添加回答
舉報