Use a new CSS file to override current website's CSS

前端 未结 12 875
梦如初夏
梦如初夏 2020-11-30 10:04

My website has currently 3 CSS files that are automatically included as a part of the website and I do not have access to the source i.e. index.html

12条回答
  •  隐瞒了意图╮
    2020-11-30 10:29

    Here's a fun solution no one has mentioned.

    Facts:

    1. You cannot modify the HTML of the page at all - no problem!

    2. You can modify the CSS files, but the developers may modify them again later and remove any changes you made - not a worry.

    3. You cannot/do not want to use Javascript and JQuery - fine by me.

    4. You can add more files on to the server - Excellent!

    Let's do some .htacess hacking for fun and profit!

    Document root .htaccess file:

    Options +FollowSymlinks
    RewriteEngine on
    RewriteBase /
    
    RewriteRule ^(.*?)css3.css(.*?) $1hackedCSS3.php$2 [L]
    

    Result: hackedCSS3.php is silently served instead of css3.css on every request.

    REF: http://httpd.apache.org/docs/2.2/howto/htaccess.html

    hackedCSS3.php file:

    
    
    // Add your CSS here with any neat !important or override tricks (read: specificity)
    div { ... }
    

    Bonus:

    You could expand this solution to include all three .css files in this one .php file (but only serve, say, css3.css and send the css1.css and css2.css to a black hole with .htaccess), and use clever regular expressions to remove/modify those developer's CSS without touching any of their files. The possibilities are tantalizing.

    Addendum:

    Can you be a bit more specific on where to include these files?

    The .htaccess file should be in the document root directory of the website. This is where www.example.com/index.html would load index.html

    Should the hackedCSS3.php file be in the same directory as the other css files?

    It can be in any directory you specify in the .htaccess file. The document root is fine. Change

    RewriteRule ^(.*?)css3.css(.*?) $1hackedCSS3.php$2 [L]
    

    to

    RewriteRule ^(.*?)css3.css(.*?) /folders/you/want/hackedCSS3.php$2 [L]
    

    Should our css content (where you specified // Add your CSS here...) should be within html style tags?

    No. Treat your CSS code in that section as if it were a .css file. You do not need