Applying a stylesheet to only a certain region of an HTML file

Viewed 5948

I'm using bootstrap for a navbar that I like and I use the style.css from bootstrap, but I also want to implement some elements from another framework that has its own style.css. The problem is that the elements appears distorted because the second style rewrites the first.

Is there a way to specify the influence of a style.css?

For example, style_1.css to have influence over:

<header>...</header>

and style_2.css to have influence over:

<main>...</main> 
5 Answers

I had the same problem and I resolved it like this:

  1. copy the CSS rules you want to use in a specific region.

  2. convert them to SCSS by pasting them in this link: css2scss and then

    Click on the arrow (choose SCSS).

  3. copy the SCSS rules result you got, and paste them in this link: scss2css.

  4. wrap the entire SCSS rules with this rule: .wrapper {}

    like this:

    .wrapper {
               a  {
                     color: #007bff;
                     text-decoration: none;
                     background-color: transparent;
                   }
    
                 /*all other rules*/
             }
    
  5. click on the 'compile' button and wait until you will get all your CSS.

    the above SCSS will result like this:

    .wrapper a {
    
                color: #007bff;
                text-decoration: none;
                background-color: transparent;
    
                }
    

    and so All your other CSS rules will be prefixed with the .wrapper class.

  6. Click download button to download your CSS, and then link it to your HTML

    page.

  7. to use this CSS only in certain regions warp that region with a div

    and give this div a class "wrapper".

    <div class = "wrapper">
      <a class = "a_Class_From_The_Downloaded_CSS_File"/>
      <!-- put here all other HTML tags you want
          and add all the class etc. you want from the 
          CSS file you created.
          it will not collide with other CSS class from other
          CSS files because of the div.wrapper tag 
          -->
    
    </div>
    
Related