You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让MudExRichTextEdit编辑器按需扩大并自适应内容高度?

解决MudExRichTextEdit自适应高度与打印内容截断问题

问题根源

你当前代码里硬设了Height="300",这直接把编辑器高度锁死,导致内容撑不开只能内部滚动;打印时内容截断也是因为固定高度加滚动限制的样式逻辑。

分步解决

1. 取消固定高度,实现自适应增长

先删掉Height="300",改用MinHeight保留初始最小高度,再通过CSS调整编辑器内部容器的溢出行为,让内容自然撑开容器:

修改后的组件代码:

<MudExRichTextEdit For="@(() => Model!.CurrentState)"
                   Value="@Model!.CurrentState"                    
                   Immediate="true"                    
                   MinHeight="300"                   
                   Placeholder=""
                   Class="auto-grow-rich-text">
</MudExRichTextEdit>

添加对应CSS(全局或组件级均可):

/* 让编辑器容器随内容自适应高度 */
.auto-grow-rich-text .mud-ex-rich-text-edit {
    height: auto !important;
    min-height: 300px;
}

/* 取消内容区内部滚动,让内容撑开容器 */
.auto-grow-rich-text .mud-ex-rich-text-edit-content {
    overflow: visible !important;
}

2. 修复打印内容截断问题

添加打印媒体查询样式,强制打印时取消所有高度和滚动限制:

@media print {
    .auto-grow-rich-text .mud-ex-rich-text-edit,
    .auto-grow-rich-text .mud-ex-rich-text-edit-content {
        height: auto !important;
        overflow: visible !important;
        max-height: none !important;
    }
}

3. 图片适配补充(可选)

如果编辑器内插入的图片有固定高度,可能影响自适应效果,可添加图片自适应样式:

.auto-grow-rich-text .mud-ex-rich-text-edit-content img {
    max-width: 100%;
    height: auto;
}

调整后编辑器会随文本、图片内容自动增高,不再出现内部滚动,打印时也能完整显示所有内容。

内容的提问来源于stack exchange,提问作者Anduel Karamani

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 23:22:05