Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

html

css

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> 
like image 427
AMayer Avatar asked Oct 20 '25 15:10

AMayer


1 Answers

It is not possible to do it directly using those CSS files that are distributed, but you can create namespaces for each CSS framework library (or CSS file) and use that wherever you want to use that framework features.

See How to namespace Twitter Bootstrap so styles don't conflict and Is there any ready to use Bootstrap css file with prefix for more details on how to namespace your style-sheets.

If you're using less, then you can create a namespace by adding a pregfix to bootstrap like this:

.bootstrap-styles {
  @import 'bootstrap';
}

/* OR */

.bootstrap-styles {
  @import (less) url("bootstrap.css");
}

You can use http://www.css-prefix.com/ to prefix any CSS file and then use it like this:

<header class="bootstrap-ns-prefix> (some bootstrap code inside) </header>

<main class="style2-ns-prefix"> (some other framework/css styles that don't get affected by bootstrap) </main>

EDIT

It does not work automatically, you have to namespace each of your CSS and then use those CSS files instead of the initials. The generator www.css-prefix.com works for me, but it adds some extra classes/namespaces at the beginning/end and before/after each comment; you should check that and correct/delete any errors before you proceed. As I mentioned above, you can use LESS or SASS frameworks to generate those namespaces.

Here is an example of using both Bootstrap and jQuery UI together:

<head>
...
<link rel="stylesheet" href="css/bootstrap_ns.css">
<link rel="stylesheet" href="css/jqueryui_ns.css">
...
</head>
<body>

<button class="btn btn-primary">Test Button</button>

<div class="bootstrap-ns">
 <button class="btn btn-primary">Bootstrap Button</button>
</div>

<div class="jqui-ns">
 <button id="jqbtn" class="btn btn-primary">jQuery UI Button</button>
</div>

<script type="text/javascript">
jQuery(function($) {
    $('#jqbtn').button();
});
</script>
</body>

And the result is this one:

CSS namespaces

As you can see, all three buttons have the bootstrap button classes btn btn-primary but only the button inside bootstrap-ns container uses the bootstrap styles.

Here you can see a demo page: http://zikro.gr/dbg/html/bootstrap-ns/

Here you can check bootstrap.css and jquery.ui.css generated by www.css-prefix.com and manual cleaned.

like image 186
Christos Lytras Avatar answered Oct 23 '25 07:10

Christos Lytras