Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Using custom html tags with differnt name but exact styling as existing ones

Posted on 2016-10-25
5
Medium Priority
?
92 Views
Last Modified: 2016-11-30
Hi,
I am using <ul> , <li> tags in my javascript library. This will be used by some user.. My library also has styling info for ul and li tags...
What this library does is if a user has an html element like : <div id="filter"/>
It will replace it with some new html consisting of <ul> . <li> etc... tags...
This only happens when the user passes the id of the html element to my library.
But the issue with this is that my library has a particular css included with a particular styling for ul and li element.. which i want to use for this replaced html...

But in case if user also specifies the style for ul and li elements then that may interfere with my library styling.
So i was thinking if instead of specifying ul i could specify something like <temp:ul> ie. my custom name but has the same styling as ul. So if user provides some styling for ul it doesnt override my styling.

Is there any way to achieve this ?

Thanks
0
Comment
Question by:Rohit Bajaj
  • 2
  • 2
5 Comments
 
LVL 20

Accepted Solution

by:
Russ Suter earned 1000 total points
ID: 41859311
You can add the !important directive to your style elements. This won't completely prevent a user from overriding your style but they'll also have to use the !important directive for it to work. There is, in fact, no way to prevent the cascading function of CSS. After all, "Cascading" is in the name.

The other thing you might be able to do is to have your Javascript code inject the CSS reference after all other CSS references on the page. That way your style changes will take priority.

Both of these approaches have a tendency to annoy people who might use the library.
0
 
LVL 44

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 1000 total points
ID: 41859506
Why don't you just give your <ul> your own unique ID and then specify that in the CSS, for example:

<ul id="someUniqueId">
   ...
</ul>

ul#someUniqueId { margin: 0px; } 
ul#someUniqueId li { color: blue; }

Open in new window

0
 

Author Comment

by:Rohit Bajaj
ID: 41859734
giving an id i can prevent my style from overriding others...but not others styling overriding mine...
if the user puts some styling on ul tag then that wilp reflect on my ul tag also
0
 
LVL 20

Expert Comment

by:Russ Suter
ID: 41859741
Bottom line... There is no technique you can come up with that a coder with decent HTML, CSS, and Javascript knowledge cannot bypass, circumvent, or override if they really want to. Most people won't want to use a library or framework that uses inline styles when CSS options are being ignored.

You seem to be over constraining the problem. What are the requirements that are forcing you to use inline styles?
0
 

Author Comment

by:Rohit Bajaj
ID: 41868560
I am including a css in my js file... My js file basically provide a search filter widget with some styling. And this styling is bundled with my js file...
What other alternative ways do you suggest can i achieve this ?
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
This article discusses how to implement server side field validation and display customized error messages to the client.
In this tutorial viewers will learn how to style a decorative dropcap for the first letter in a paragraph using CSS. In CSS, create a new paragraph class by typing "p.fancy": Then, to style only the first letter of the first sentence, include the ps…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question