Style Encapsulation in ASP.NET MVC Rich Text Editor Control

4 Mar 20254 minutes to read

Style encapsulation determines how styles are applied within the Syncfusion ASP.NET MVC Rich Text Editor. This feature helps control whether the component’s content inherits global styles from the application or remains isolated.

Encapsulation modes

The Rich Text Editor offers two rendering modes for controlling style encapsulation:

  1. Encapsulated Mode (Iframe Mode)
    • When enabled, the Rich Text Editor is rendered inside an <iframe>.
    • The application’s global CSS rules will not affect the content inside the editor.
    • This ensures that the editor’s content remains styled independently.
    • Usage: IframeSettings(iframeSettings => iframeSettings.Enable(true))
  2. Non-Encapsulated Mode (Default)
    • The Rich Text Editor is rendered without an <iframe>.
    • The application’s global CSS will apply to the content inside the editor.
    • This mode allows seamless integration with existing styles.
    • Usage: IframeSettings(iframeSettings => iframeSettings.Enable(false))

Default behavior

By default, the Rich Text Editor uses non-encapsulated mode (IframeSettings(iframeSettings => iframeSettings.Enable(false))), allowing the application’s styles to affect the editor’s content.

Below is a sample implementation of both encapsulated and non-encapsulated modes.

<div class="control-section">
    <div class="editor">
        <h6 class="header">With style encapsulation</h6>
        @Html.EJS().RichTextEditor("iframe").IframeSettings(iframeSettings => iframeSettings.Enable(true)).Value(ViewBag.value).Render()
    </div>
   
    <div class="editor">
        <h6 class="header">Without style encapsulation</h6>
        @Html.EJS().RichTextEditor("defaultRTE").Value(ViewBag.value).Render()
    </div>  
</div>

<style>
    .control-section {
        display: flex;
        justify-content: space-between;
        gap: 15px;
    }

    p {
        color: blue;
    }

    p strong {
        font-size: 120%;
        background-color: yellow;
    }

    li p {
        background-color: blue;
        color: white;
    }

    #richTextEditor {
        margin-left: 10px;
    }

    .header {
        display: inline-block;
        padding: 10px 15px;
        border: 1px solid #ccc;
        background-color: #f8f9fa;
        color: #333;
        font-size: 14px;
        font-weight: normal;
        text-align: center;
        width: 100%;
        box-sizing: border-box;
        margin-right: 10px;
    }
</style>
public class HomeController : Controller
{

    public ActionResult Index()
    {
        ViewBag.value = @"<p>The Syncfudion <strong>Rich Text Editor</strong>, a WYSIWYG (what you see is what you get) editor, is a user interface that allows you to create, edit, and format rich text content. You can try out a demo of this editor here.</p><p><b>Key features:</b></p><ul>
     <li>
        <p>Provides &lt;IFRAME&gt; and &lt;DIV&gt; modes.</p>
     </li>
     <li>
        <p>Bulleted and numbered lists.</p>
     </li>
     <li>
        <p>Handles images, hyperlinks, videos, hyperlinks, uploads, etc.</p>
     </li>
     <li>
        <p>Contains undo/redo manager. </p>
     </li>
  </ul><div style='display: inline-block; width: 60%; vertical-align: top; cursor: auto;'></div>";
        return View();
    }

}