Preventing inline CSS

Preventing inline CSS

arvinsimarvinsim Posts: 20Questions: 1Answers: 0
edited September 2011 in General
I noticed that Datatables sometimes generates inline CSS (style='....'). Is there are way to remove them? Or at least make the code not add styles inline?

Replies

  • GregPGregP Posts: 500Questions: 10Answers: 0
    edited September 2011
    As far as I know, DataTables only adds inline CSS style when it's part of a dynamic JavaScript function. This is common practice. Do you have a particular scenario in which it becomes inconvenient or a problem?

    Anything that uses jQuery's show/hide functions, for example, will ALWAYS end up applying style="display:none" for hide and then style="display:block" (or whatever other value it needs to be to restore visibility); this is jQuery rather that DT, and I can't imagine that particular example changing nor a good reason for it to change.

    But I'm with you in avoiding inline styles wherever possible, so if there are places where this could be addressed, I'm sure Allan would be all ears.

    Remember, though, that requiring someone to write particular CSS rules to accomodate certain functionality is just as bad. "What? I ditched the default DT CSS in order to write my own. Now you're telling me I *have* to have a class called 'foo' and another one called 'bar' just to make it work?"
  • arvinsimarvinsim Posts: 20Questions: 1Answers: 0
    Ok. The problem was that the column widths are automatically calculated. I need only set the setting "bAutoWidth" to false to stop it from adding inline CSS. I assume that its the case with other CSS then(that it can be set via settings).
  • GregPGregP Posts: 500Questions: 10Answers: 0
    Right. Auto-Width by necessity imposes its settings into the DOM post-render, and since it is a flexible numerical value, it would be impossible to use a class or ID.

    There's no reason to fear inline-styles that are put there by JavaScript. When you are learning web development and tutorials, books, and mentors tell you "inline styles are evil" they mean that inline styles are evil for writing static markup (HTML/CSS without any JavaScript). They are not saying that inline styles are inherently evil for all things.

    My only point is: don't fear them just because you've heard to avoid inline styles. When they're added by JavaScript (or even server-side scripts in some cases) for specific purposes, you don't really need to avoid them.
This discussion has been closed.