DNN HTML编辑器中CSS选择器特殊符号转义导致样式失效求助
I’ve run into this exact issue with DNN’s HTML editor before—TinyMCE (the default editor in most DNN versions) automatically encodes special characters like > into > to prevent unintended HTML parsing, but this breaks CSS selectors that rely on the direct child combinator. Here are a few reliable solutions:
Best Practice: Add CSS to your theme’s stylesheet
This avoids the editor’s encoding entirely and is the cleanest approach. If you have access to your DNN theme files:- Navigate to your theme’s folder (typically under
/Portals/_default/skins/[YourThemeName]/or your portal-specific skins directory). - Open your main CSS file (e.g.,
style.cssortheme.css). - Paste your CSS rule directly into the file:
.overlaybox > .overlaytext { border: 1px solid #fff; padding: 40px; max-width: 550px; margin: 0 auto; } - Save the file and clear your DNN cache (go to Settings > System > Clear Cache) to see the changes take effect immediately.
- Navigate to your theme’s folder (typically under
Workaround: Use alternative selector syntax in the HTML editor
If you can’t edit the theme’s CSS, try these workarounds that won’t get escaped:- Descendant selector: If
.overlaytextelements only exist inside.overlaybox(no nested instances), replace the direct child combinator with a space:.overlaybox .overlaytext { border: 1px solid #fff; padding: 40px; max-width: 550px; margin: 0 auto; } - Target a custom class: Add a unique class directly to your
.overlaytextHTML element, then style that class instead:
Then update your HTML to use.centered-overlay-text { border: 1px solid #fff; padding: 40px; max-width: 550px; margin: 0 auto; }<div class="overlaytext centered-overlay-text">...</div>.
- Descendant selector: If
Advanced: Adjust editor settings to allow unencoded characters
If you have admin access, you can tweak the editor’s configuration to stop encoding>in style tags:- Go to Persona Bar > Settings > Site Settings > Site Behavior > HTML Editor.
- Select the editor profile you’re using (usually "Default").
- For TinyMCE, look for the
valid_elementsorextended_valid_elementssetting, and addstyle[*]to allow all content within style tags without encoding. - Warning: This can introduce security risks if untrusted users have access to the HTML editor, so only use this if you control all content on the site.
内容的提问来源于stack exchange,提问作者Hardik Masalawala

