veinjs Usage Example

What is veinjs? Why?

veinjs is a library that injects CSS into the document (it's a perfectly legal procedure). Rather than using inline styles, the CSS is being pushed into a dynamically generated stylesheet that's injected into the header.

This approach gives us a couple of benefits:

  • Persistence - Say you want to dynamically make all your sub headers a certain color. You apply CSS properties with jQuery, and everything looks great. But then, you load some more stuff with AJAX, and you notice that your CSS is useless, because it has been only applied to the elements currently living in DOM. That's when injection can save you headache. Just apply any CSS code once, and it's valid - until the next refresh!
  • Performance - Our minions at jsperf.com tell us that injecting CSS rules for a vast number of elements is VERY fast, check it out for yourself:
    http://jsperf.com/inject-vs-apply/2
  • Media Query Support - You can inject media queries and work responsive magic with veinjs dynamically.

Media Queries CSS Usage Example

I'm a friendly cube!
Click here to inject CSS with media queries into our friendly cube!

(if you're on a small screen device, it will turn red. If you're on a big screen device, resize your browser and see the injected media query in action). This is acheived by running the following code:

vein.inject([{'@media (max-width: 768px)': ['#friendly-cube']}], {'background-color': '#F00'});

Dynamic CSS Usage Example

jQuery Inline Styles

Title

Lorem ipsum dolor sit amet

Title

Lorem ipsum dolor sit amet

veinjs Injection Awesomeness

Title

Lorem ipsum dolor sit amet

Title

Lorem ipsum dolor sit amet

Fork me on GitHub