Use a new CSS file to overwrite the existing website's styles
P粉504920992
2023-08-23 09:15:29
<p>My website currently has 3 CSS files that are automatically included as part of the website, I do not have access to the source code, i.e. the index.html file of the website, but I do have access to the CSS of my website document. </p>
<p>I'm trying to override my website's CSS file with my own styles and create a new CSS file that contains all the styles I want to override. </p>
<p>I tried using <code>@import url(css4.css)</code> and placing it on top of my last CSS file but this does not overwrite the last CSS file's styles . </p>
<p>How do I achieve this goal? </p>
<pre class="brush:php;toolbar:false;"><link rel="stylesheet" type="text/css" href="currentCSS1.css">
<link rel="stylesheet" type="text/css" href="currentCSS2.css">
<link rel="stylesheet" type="text/css" href="currentCSS3.css">
<!-- How can I add the code below using only CSS? -->
<link rel="stylesheet" type="text/css" href="newCSS4.css"></pre>
<p><br /></p>
Here's an interesting solution that no one has mentioned.
fact:
You cannot modify the HTML of the page - No problem!
You can modify CSS files, but the developer may modify them again later and remove any changes you made - Don't worry.
You can't/don't want to use Javascript and JQuery - No problem for me.
You can add more files to the server - Great!
Let's do some .htacess hacking for fun and profit!
.htaccess file in the document root directory:
Result: hackedCSS3.php will be served silently every time it is requested, instead of css3.css.
Reference: http://httpd.apache.org/docs/2.2/howto/htaccess.html
hackedCSS3.php file:
Additional rewards:
You can extend this solution to all three
.css
files in this one.php
file (but only css3.css, And use clever regex to remove/modify those developers' CSS without touching any files. The possibilities are exciting.Replenish:
The.htaccess file should be located in the document root directory of your website. This is where www.example.com/index.html loads index.html.
It can be located in any directory you specify in the .htaccess file. The document root works too. Change
for
unnecessary. Treat this part of the CSS code as a .css file. You don't need the
<style>
tag.In addition to using the
!important
suggested by most answers, this is a question about CSS specificitycan be represented by 4 columns of priorities:
This is a complete example of CSS-specific code snippets