如何调整设置contenteditable属性的可编辑文本框的尺寸
问题原因
- CSS语法错误:editor.css中
font-family: "Montserrat"行末尾缺失分号,会导致后续高度相关属性无法被浏览器正常解析。 - 百分比高度无效:你给.editor设置的
height: 200%为相对高度,需要父元素body、html存在明确的高度定义才能生效,当前父元素无高度配置,所以该属性不会起作用。 - 布局属性适配问题:
display: inline-block更适合行内块级元素场景,作为全宽编辑器容器使用块级布局更合理。
修复方案
你可以根据需求选择固定高度或者自适应高度两种方案:
方案1:设置固定高度(最简单)
直接修改editor.css即可,调整后的代码如下:
//this is editor.css .editor{ background-color: #1d1f21; color: #c5c8c6; font-family: "Montserrat"; font-weight: 100; width: 100%; height: 600px; /* 按需调整数值即可控制编辑器高度 */ display: block; overflow: auto; /* 内容超出时自动出现滚动条,更适配编辑器场景 */ } ::selection{ background: #373b41; }
方案2:自适应占满视口剩余高度
如果需要编辑器自动占满浏览器窗口剩下的空间,需要同步修改page.css:
首先修改page.css:
//this is page.css html, body { height: 100%; margin: 0; padding: 16px; box-sizing: border-box; } .main{ background-color: #282a2e; color: #969896; font-family: "Raleway", "Montserrat"; font-weight: 100; display: flex; flex-direction: column; height: 100%; }
然后修改editor.css:
//this is editor.css .editor{ background-color: #1d1f21; color: #c5c8c6; font-family: "Montserrat"; font-weight: 100; flex: 1; /* 占满父容器剩余高度 */ width: 100%; display: block; overflow: auto; } ::selection{ background: #373b41; }
内容的提问来源于stack exchange,提问作者SlipperyDuck777
相关产品推荐
相关产品推荐

