How to protect css files [closed]

别等时光非礼了梦想. 提交于 2019-12-19 13:36:44

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/17344821/how-to-protect-css-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!