0

In my website, the users have to enter markdown in a textarea, for which I am using a markdown editor. The problem is: it uses icomoon font, and my websites too. Both uses the same class to define the fonts, but not both uses the same icons. The question is simple: is there a way to define the editor.css for a special div?

Like that:

<div css="editor.css"></div>
3
  • 1
    There will be, in a couple of years, with scoped style tags ? Commented Jan 2, 2014 at 14:21
  • The answer, something (a class/selector) has to change. You pick. Commented Jan 2, 2014 at 14:22
  • @schnawel007 has the best answer yet. Use that as a starting point and see if you can complete it from there. Commented Jan 2, 2014 at 14:27

7 Answers 7

3

Give the DIV a Class and then add a CSS file like this:

.markdown
{
color: red;
}

If you import a new css dynamic, the old styles will be overwritten.

Some help, for dynamic css loading: How to apply inline and/or external CSS loaded dynamically with jQuery

Sign up to request clarification or add additional context in comments.

Comments

1

Namespace your editor styles

You can add a selector that namespaces your editor and allows you to style it:

<div class="editor-style">
  <div id="cool-custom-editor">...</div>
</div>

In your css:

.editor-style .icon-thumbs-up { color: green; }

Using Scoped Styles (needs polyfill)

As mentioned in @adeneo's comment below your question there is the option of using scoped style tags.

Supposing your editor looks like this:

<div id="cool-custom-editor">...</div>

You can apply a specific style using the scoped attribute like so:

<div>
  <style scoped>@import url(editor.css);</style>
  <div id="cool-custom-editor">...</div>
<div>

Caveats

According to can I use the scoped attribute is only natively supported by Firefox 26+.

If you want to use it you will have to polyfill:

Further Reading

Comments

1

You dont need multiple files. You can give the div an id or class like so

  <div class="div1">
      <span></span
    ...
  </div>

and now in you css you do this

   .div1 span{
        font:(whatever font);
     } 

Comments

0

I don't think so, no. At least not without using any js workarounds. The best solution would be to use kind of namespace for user-specific css classes.

Comments

0

No you can't do that.. I think you should solve the conflit by changing the icomoon class names in this file.

Comments

0

OK solved: renaming the classes in editor for icomoon was a lot easier than I dared tough.

Comments

0

not a good way but it can help:

$("[css]").each(function(i){
    var elm = $(this);
    $.when($.get(elm.attr("css"))).done(function(response) {
        var newresponse=response.replace(/(.+{)/gi,".customCss"+i+" $1");
        elm.addClass("customCss"+i);
        elm.before('<style>'+newresponse+'</style>');
    });
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.