Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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 of the website but I do have access to the CSS files of my website.

I am trying to use my own style to override my websites CSS files and create a new CSS file that would contain all the styling that I would like to overwrite on my website.

I have tried using @import url(css4.css) and I have placed that at the top of my last CSS file but that wouldn't overwrite the last CSS file's styling.

How can I achieve this?

<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 to add this below just by using CSS? --> <link rel="stylesheet" type="text/css" href="newCSS4.css">  
like image 951
Neophile Avatar asked Mar 23 '15 09:03

Neophile


People also ask

How do I import CSS into another CSS file?

The @import rule allows you to import a style sheet into another style sheet. The @import rule must be at the top of the document (but after any @charset declaration). The @import rule also supports media queries, so you can allow the import to be media-dependent.

What is CSS rules overriding?

CSS allows you to apply styles to web pages. More importantly, CSS enables you to do this independent of the HTML that makes up each web page. Overriding: Overriding in CSS means that you are providing any style property to an element for which you have already provided a style.


1 Answers

Besides using !important that most answers are advising you to use, this is a matter of CSS specificity

The concept

Specificity is the means by which a browser decides which property values are the most relevant to an element and gets to be applied. Specificity is only based on the matching rules which are composed of selectors of different sorts.

How is it calculated?

The specificity is calculated on the concatenation of the count of each selectors type. It is a weight that is applied to the corresponding matching expression.

In case of specificity equality, the latest declaration found in the CSS is applied to the element.

Some rules of thumb

  • Never use !important on site-wide css.
  • Only use !important on page-specific css that overrides site-wide or foreign css (from ExtJs or YUI for example).
  • Never use !important when you're writing a plugin/mashup.
  • Always look for a way to use specificity before even considering !important

can be represented by 4 columns of priority:

inline = 1|0|0|0

id = 0|1|0|0

class = 0|0|1|0

element = 0|0|0|1

Left to right, the highest number takes priority.


Here is a snippet with a Full example of a CSS specificity

/*demo purposes*/  body {margin: 0;padding: 0}  div,article {min-height: 200px;height: 100%;width: 100%}    /*CSS Specificity */    /* SPECIFICITY: 0/1/0/0 */  #id {    background-color: green  }    /* SPECIFICITY: 0/0/1/0 */  .class {    background-color: yellow   }    /* SPECIFICITY: 0/0/0/1 */  section {    background-color: blue   }      /* ------------ override inline styles ----------- */    /*to override inline styles we  now use !important */    /* SPECIFICITY  0/0/1/0 */    .inline {    background-color: purple !IMPORTANT /*going to be purple - final result */   }
<article>    <div id="id">      <div class="class">        <section>          <div class="inline" style="background-color:red">            <!--SPECIFICITY 1/0/0/0 - overridden by "!important -->          </div>        </section>      </div>    </div>  </article>


Now here is the Full snippet step by step

ID: GREEN

/*demo purposes*/  body {margin: 0;padding: 0}  div,article {min-height: 200px;height: 100%;width: 100%}    /*CSS Specificity */    /* SPECIFICITY 0/1/0/0 */  #id {    background-color: green  }     
<article>    <div id="id">      <div class="class">        <section>          <div>                       </div>        </section>      </div>    </div>  </article>

CLASS: YELLOW

/*demo purposes*/  body {margin: 0;padding: 0}  div,article {min-height: 200px;height: 100%;width: 100%}    /*CSS Specificity */    /* SPECIFICITY  0/0/1/0 */  .class {    background-color: yellow  }
<article>    <div id="id">      <div class="class">        <section>          <div>          </div>        </section>      </div>    </div>  </article>

ELEMENT: BLUE

/*demo purposes*/  body {margin: 0;padding: 0}  div,article {min-height: 200px;height: 100%;width: 100%}    /*CSS Specificity */    /* SPECIFICITY  0/0/0/1 */  section {    background-color: blue  }
<article>    <div id="id">      <div class="class">        <section>          <div>          </div>        </section>      </div>    </div>  </article>

INLINE STYLE: RED

/*demo purposes*/  body {margin: 0;padding: 0}  div,article {min-height: 200px;height: 100%;width: 100%}     
<article>    <div id="id">      <div class="class">        <section>          <div style="background-color:red">          <!--SPECIFICITY 1/0/0/0 -->          </div>        </section>      </div>    </div>  </article>


OVERRIDDEN INLINE STYLE: PURPLE

/*demo purposes*/  body {margin: 0;padding: 0}  div,article {min-height: 200px;height: 100%;width: 100%}  /*CSS Specificity */    /* SPECIFICITY  1/0/0/1 */    section > div {    background-color: purple !IMPORTANT  }     
<article>    <div id="id">      <div class="class">        <section>          <div style="background-color:red">          <!--SPECIFICITY 1/0/0/0 -->          </div>        </section>      </div>    </div>  </article>

You can calculate the specificity of your element(s) here


Note:

A must read on this subject

  • Inheritance and cascade
  • CSS Specificity
  • Specifics on CSS Specificity
like image 56
dippas Avatar answered Nov 17 '22 14:11

dippas