TinyMCE is removing <style> tags

CssTinymce

Css Problem Overview


I'm facing a problem with TinyMCE. When I enter a style tag in the HTML editor of TinyMCE, it removes the tags when I click update.

<style type="text/css"> 
     .newclasss { color:#c9c9c9; } 
</style>

My valid elements are follows:

<script type="text/javascript">

var valid_elms = "hr[class|width|size|noshade]";
valid_elms    += "span[class|align|style],";
valid_elms    += "font[face|size|color|style],";
valid_elms    += "img[href|src|name|title|onclick|align|alt|title|";
valid_elms    += "width|height|vspace|hspace],";
valid_elms    += "iframe[id|class|width|size|noshade|src|height|";
valid_elms    += "frameborder|border|marginwidth|marginheight|";
valid_elms    += "target|scrolling|allowtransparency],style";

extended_valid_elements: valid_elms
</script>

Can anyone help please??

Css Solutions


Solution 1 - Css

Since style tags are not valid XHTML, TinyMCE disabled the ability to add them outside of the tags.

You have to add style tags to the valid children configuration

valid_children : "+body[style]"

Edit: This solution applies to version 3.4.2

Source

Solution 2 - Css

"hr[class|width|size|noshade],font[face|size|color|style],span[class|align|style],img[href|src|name|title|onclick|align|alt|title|width|height|vspace|hspace],iframe[id|class|width|size|noshade|src|height|frameborder|border|marginwidth|marginheight|target|scrolling|allowtransparency],style[type]"

Let me know how that works for you

Solution 3 - Css

If you put tinyMCE in fullpage mode, you can put styles in the <head>

<script type="text/javascript">
tinyMCE.init({
        plugins : "fullpage",
});
</script>

Solution 4 - Css

It's a bit hacky but I'm using TinyMCE 4 and was in a pinch, so I added a <script> tag in the TinyMCE editor's Source Code view and used jQuery to change the styles.

<script>$("#signup").css("font-weight", "bold");</script>

This assumes that TinyMCE has been setup not to disallow script tags, and your page already has jQuery included in it like mine did, if not you could use standard javascript to change styles.

Solution 5 - Css

For me, I need to do the following in TinyMCE 4.5.7:

tinyMCE.schema.addValidChildren("body[style]")

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjyotiView Question on Stackoverflow
Solution 1 - CssYazView Answer on Stackoverflow
Solution 2 - CssJohnView Answer on Stackoverflow
Solution 3 - CssErik van VelzenView Answer on Stackoverflow
Solution 4 - CssMatthew LockView Answer on Stackoverflow
Solution 5 - CssWalty YeungView Answer on Stackoverflow