Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resolve CSS class name conflicts from multiple third party libraries?

Tags:

html

css

Trying to help a friend with a project. Due to client demands, they're being forced to use two third-party CSS libraries which have conflicts with each other. For example, library A defines a class called .active and library B also defines a class called .active. You can see where this might go wrong.

One solution is to go in and manually rename the classes in one library or the other, but this seems clumsy to me, in case anyone in future maintenance tries to use a clean version of the original third party library, among other issues. Also, the number of conflicts is not yet known, but thought to be large. Namespacing is not very feasible for these reasons.

My solution personally would be to roll my own CSS for this project, and properly namespace it. But it sounds like the client refuses to allow this and demands on using both CSS libraries for no particular reason that they care to share.

Since we are not in control of either library, are there any other tricks to isolate HTML elements from one library or the other? For example, is there a way to tell an element to accept CSS styles with a given class, but only from a specific .css file? I'm making up fake syntax here, but something like:

<div class="myStyles.css[active]">styled content</div>
like image 338
Steverino Avatar asked Apr 21 '17 07:04

Steverino


People also ask

How do you avoid class conflict in CSS?

You need to keep the widget CSS rules as self-contained as possible, which means that you will need to specify every property that is critical to you design, and make sure that properties like float: none , position: static and so on are explicitly set.

How do you deal with conflict in CSS?

To resolve such conflicts, you have CSS Cascade. The CSS Cascade is a way for browsers to resolve conflicting CSS declarations. By specifying CSS rules, you specify where the CSS style is added to the cascade hierarchy. The further down a hierarchy a declaration falls, the less likely it will end up as the final style.

What happens when conflicting occurs in CSS?

What happens when conflicts occur? There may be times when two or more declarations are applied to the same element. It is also possible that there may be a conflict between them. When conflicts like this occur, the declaration with the most weight is used.


1 Answers

Your concern looks good.

Please find my findings below regarding your concern.

is there a way to tell an element to accept CSS styles with a given class, but only from a specific .css file

<div class="myStyles.css[active]">styled content</div>

This is not possible in HTML5/CSS.

One way you can resolve this conflict is,

Include your custom.css file along with the 3rd parties(after them in order) with necessary style for #my-wrapper .active { ... }.

You will obviously be having a wrapper selector to this div of yours like below

<div id="my-wrapper">
  <div class=".active">styled content</div>
</div>

Now, the styles for .active in your custom.css will be placed on priority due to order of linked CSSs

like image 60
anusreemn Avatar answered Oct 06 '22 14:10

anusreemn