-3

I saw a lot of methods to protect css files but none of them is 100% safe or in other words it is very easy to manipulate the block and get the css file. I was wondering what is the best method to protect css.

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
user2510306
  • 47
  • 2
  • 7
  • 3
    100% safety doesn't exist. Also, why do you want this? The client's browser needs to have complete access to the CSS or it cannot render the page correctly. – Bart Friederichs Jun 27 '13 at 13:42
  • 2
    What do you mean protect them? You can't. It's the same reason you can't protect an image on a website - if someone can see it, they can copy it. When anyone goes to your site, they download a copy of your .css – Nick R Jun 27 '13 at 13:43
  • "I saw a lot of methods to protect css files" - Have you seen such things? Do share them. Possibly you have mistaken something for protection. – Kangkan Jun 27 '13 at 13:45
  • Why would you want to do this? Isn't imitation a form of flattery? – Zahymaka Jun 27 '13 at 13:46
  • So iow there is no css protector alive on this planet ? I want to protect my work so no one would be able to copy my css. – user2510306 Jun 27 '13 at 13:46
  • 6
    In that case, don't put your site online ;-) – Nick R Jun 27 '13 at 13:47
  • 1
    You are a `macho` designer and you dont want anybody else to view your awesome styling isnt it buddy! – Moeed Farooqui Jun 27 '13 at 13:47
  • Just a warning: The word "best" on SO triggers a lot of 3k+ members to vote to close because often times there is no way to definitively say what the best is. I'd stray away from that word on SO. – Bailey Parker Jun 27 '13 at 13:47
  • If many people want to copy it, and do copy it, there will be people that want to pay you for work. Get known. Be the best. Let others see your work. – Bart Friederichs Jun 27 '13 at 13:48
  • Well, I got it now... I can't protect my css files by any kind of chance... :'( – user2510306 Jun 27 '13 at 13:50
  • 1
    You should mention what kinds of things you have seen in your question. You may have misunderstood something. – Kyle Jun 27 '13 at 14:02
  • If I see your website, I can "copy" your css without ever looking at it. Even if you by some odd miracle managed to "protect" your CSS (which I can see in my browser's dev console btw), what will stop me from copying it by just looking at your site? – N.B. Jun 27 '13 at 14:07
  • 1
    For all the close voters, I would argue that this question isn't unclear. In fact, it's very clear what is being asked. (It is a duplicate though, of many previous SO questions) – Bailey Parker Jun 27 '13 at 14:11
  • 1
    It doesn't matter what it is, it shows lack of knowledge about browsers and how WWW works. It should be closed. – N.B. Jun 27 '13 at 14:12
  • @N.B If lacking knowledge about browsers and how WWW works is not a prerequisite in being able to ask a question about browsers and how WWW works, what the hell is? – Kyle Jun 27 '13 at 14:14
  • 1
    @KyleSevenoaks - it isn't such a question, it's about protecting CSS. Googling "protect css" really gives answers to all questions one might ask, and expands their knowledge. 0 effort is what it is here. You can argue all you want, a fact is a fact, and a bad question is a bad question. – N.B. Jun 27 '13 at 14:18
  • Not arguing that it's a bad question, it could definitely do with some improving and some proof of research beforehand. It was just your comment gave off the impression that if you want to know something about how something works, you can't ask a question. – Kyle Jun 27 '13 at 14:20
  • @KyleSevenoaks Even if it were about how browsers/the WWW works, it would be an off topic question for SO. SO is for solving specific programming problems. – cimmanon Jun 27 '13 at 14:20

1 Answers1

4

CSS files, like javascript and HTML, can be minified. For all three, their whitespace can be removed to reduce filesize. For CSS and Javascript (and class and id names in HTML), their tokens can be obfuscated (this means instead of logical class names like .nav-link a meaningless one is chosen like .bz0). The trouble with this is that it is very unmaintainable, because class and id names in CSS must correspondingly be changed the in relevant HTML and Javascript so that the obfuscation doesn't break anything. This typically makes obfuscating your code more of a hassle then a benefit (Although, if you have GMail viewing its source will reveal that Google does it, although this is arguably to save space).

However, if you are interested check out this SO thread: tools for obfuscating html and css

Bottom line, though--whether you use obfuscation or not--is that the browser must be able to parse your CSS, which means it needs access to the file itself. And obfuscated or not, someone who was determined to steal your CSS could do so. Although, I wouldn't see why this would be beneficial because CSS is usually domain-specific (meaning individual files are only made for one website or a small subset of websites).

A nice touch that I think could act as a deterrent is a polite comment at the top of all of your CSS files.

/*
 *      #####
 *     #### _\_  ________
 *     ##=-[.].]| \      \
 *     #(    _\ |  |------|
 *      #   __| |  ||||||||
 *       \  _/  |  ||||||||
 *    .--'--'-. |  | ____ |
 *   / __      `|__|[o__o]|
 * _(____nm_______ /____\____ 
 * 
 * Hey you! Looks like you found our main css file through some l33t h4x0ry.
 * You're welcome to give it a read-through if you'd like. But if you want to
 * barrow something clever and use it in your projects we'd really appreciate a
 * mention. Thanks!
 */

Of course, code that you write is copyrighted (I'm not a lawyer so don't hold me to that), so you could try putting a copyright notice:

/*
 * (c) 2013 Your Company. All rights reserved.
 */

But I think that would be just ignored. The more-conversational message will likely be heeded more.


EDIT: Jeff Atwood isn't a lawyer either, but he has way more experience than I do and according to his blog post on open source licenses:

Without a license, the code is copyrighted by default. People can read the code, but they have no legal right to use it. To use the code, you must contact the author directly and ask permission.

Community
  • 1
  • 1
Bailey Parker
  • 15,599
  • 5
  • 53
  • 91