i'm writing a sophisticated visual effect, that changes "box-shadow" property.
Let's for short, just name the value for shadow (1pt 1pt 3pt rgba(...)), like "shadow".
I have a stylesheet, and HTML element in question.
Element has, say, "shadow1" value defined for normal state, and "shadow2" for hovered state:
.elem {
box-shadow: #333 1pt 1pt 3pt; /* shadow1 */
}
.elem:hover {
box-shadow: #666 3pt 3pt 5pt; /* shadow2 */
}
My script adds it's own shadow to existing one. So:
box-shadow: shadow1, shadow2
This is simple to implement. I just need to do following, using jQuery:
var defaultShadow = elem.css("box-shadow");
elem.css("box-shadow", defaultShadow + ", " + anotherShadow);
So, the algorithm is following:
var defaultShadow = elem.css("box-shadow");
Do something in a setInterval. Add another shadow to default one
The problem arises, when element gets hovered, having effect-script running. In this case, effect is already working with "defaultShadow", that was obtained in step 1.
It's more harder, because shadows, stated in script appears in the "style" attribute, and all rules, declared with external CSS are getting overridden.
Is there a way to get CSS styles, declared in a .css file, for element in question, using JavaScript. I need also styles for states of the element, like ":hover", ":active" etc.
Is there a way to get CSS styles,
declared in a .css file, for element
in question, using JavaScript. I need
also styles for states of the element,
like "hover", "active" etc.
You should be able to modify the cssRules object with javascript to create your own rules instead of relying on inline styles.
See this blog post for a bit more information.
Example of IE:
document.styleSheets[0].addRule("p .myclass", "font-size: 120%");
Example for Firefox:
document.styleSheets[0].insertRule("p{font-size: 20px;}", 0);
Reference doc these were found.
document.getElementById("id").className = 'arrow_box
Related
I'm building JavaScript widgets that are supposed to be added onto other people's websites.
I style my widgets by dynamically adding a CSS to the pages they're on.
For example,
My CSS code below applies to a DIV inside my widget:
.myWidget { background-color: red; }
But a CSS file outside my own on a remote page might have:
div { border: 5px solid green; }
The CSS above would also apply to my widgets. How can I disable all other CSS outside my own?
Thanks
You could be Using shadow DOM
Shadow DOM MDN Web Docs
An important aspect of web components is encapsulation — being able to keep the markup structure, style, and behavior hidden and separate from other code on the page so that different parts do not clash, and the code can be kept nice and clean. The Shadow DOM API is a key part of this, providing a way to attach a hidden separated DOM to an element.
You can use the all shorthand property and the unset keyword to set each property's value to its initial value.
.myWidget {
all:unset;
background-color: red;
}
div {
background-color:yellow;
}
<div class="myWidget">Hello World!</div>
Okay so I've been making website for my mom's business and stumbled onto an interesting thing I can't really find difference on, right now, since website is still in early development.(tried googling but no help)
So basically my question is:
What is the difference in css(and html) between;
container a:hover;
and
container:hover;
I mean it shouldn't be any different. In both cases, as long as there is only <a></a> type used like here, it will apply to everything:
#name of the button
The thing is here:
.nav-link-wrapper:hover {
border-bottom: 1px solid black;
}
.nav-link-wrapper a:hover {
color:black;
}
Why do I have to specify a that is within the class, to apply color, when I don't have to for manipulating borders.
Sry if it was asked. This is more of a beginner's question and I can't find anybody who already asked this.
If you set color in .nav-link-wrapper:hover, it will not affect since a tag has its own style. So in order to add style for that a tag, you will need to specify a.
If you see why you have to explicitly use a color property for your a tag is because of the Browser specific color property being applied on it. color and text-decoration is applied to it explicitly as shown in the image snippet below:-)
And that is true only with color property not with any other property. In the code snippet below I am setting font-size for the body and it is applied on the a tag.
body{
color:orange; /* Parent Not applied */
font-size:2rem; /* Applied to Child*/
}
<body>
Test Link
</body>
I'd like to be able to tell if specific CSS properties (width, height, margin, padding, font-size, …) were set by the page author for a DOM element. My goal is to not change elements that have had their dimensions explicitly set, but to change those that have not.
function isPropertySet(elem, "width") should return true if the page author set the width CSS property either inline (style="width: 100px;"), or via a stylesheet.
This is not straightforward because the layout engine infers these values, and it seems that however I try to access them the browser has supplied values.
For instance, I've tried getComputedStyle(elem).getPropertyValue("width"), but this returns the computed width (not surprising given the name).
The style property, e.g. elem.style.width, isn't sufficient because it doesn't include properties set in stylesheets.
Before I go to the immense pain of searching through the stylesheets, does anyone have a better way?
Thanks!
If you want to supply default style set for the elements which were not customized, than the easiest way would be to create your own stylesheet and put it at the top, before any other CSS files. This way, every element customized elsewhere will overwrite your default styles. Be careful with the cascading order: not only your styles should precede every other, but the selectors should also be general enough.
If, on the other hand, for some reason you want to know through JavaScript whether the element was customized, then it's not possible, unless you want to compare the particular style with the default one, given that default styles may vary from browser to browser. For example, in Firefox, the default style for <h1/> is:
h1 {
display: block;
font-size: 2em;
font-weight: bold;
margin: .67em 0;
}
while Chrome has a slightly different style:
h1 {
display: block;
font-size: 2em;
-webkit-margin-before: 0.67em;
-webkit-margin-after: 0.67em;
-webkit-margin-start: 0px;
-webkit-margin-end: 0px;
font-weight: bold;
}
This creates a problematic edge case. Imagine I want all <h1/> be font-weight:normal; font-size: 200%;, but there is one specific title on one specific page which I want to be exactly 2em and be displayed in bold. You'll think that the title is not customized, and override its style, while in fact, it was customized, the size and weight being set on purpose.
If you aren't worried about inherited styles and only the specific DOM element then maybe you can dynamically create the specific element&classname (so it only has CSS styles) and then examine it's width using the above methods and compare it?
The best way I have found to answer this question from JavaScript is to create a temporary element, that is not attached to the dom, and read my default values from that. I then test the default values against the values read from the element I'm testing (using jQuery or getComputedStyle) - if they compare then it's a best guess they haven't been set. Obviously this has a downside in the fact that if the element has had it's property set to the exact same value as the default you can't tell the difference.
Something like this
function hasDefaultStyle(elm, prop) {
var def = $('<'+$(elm).attr('tagName')+' />').css(prop);
return $(elm).css('prop') == def;
}
When dealing with different dimension metrics i.e. percent, cm, em and so on - these have to be dealt with in a different way—on browsers other than FireFox at least—due to the computed problem you mention.
FireFox does the right thing in my opinion and that when you request styles from an element that hasn't been placed in the dom it returns the original values i.e. like 50%.
For more information on how to solve at least the percent problem you can see my answer here:
Determine whether element has fixed or percentage width using JavaScript
It is rather ridiculous that such methods are necessary however :/
This question already has answers here:
How to reset/remove CSS styles for a specific element or selector only
(17 answers)
Closed last month.
I know this question was asked before, but before marking it as a duplicate, I want to tell you that my situation is a little different from what I found on the internet.
I'm building and embedded script that people can put it on their sites. This script creates a div with a certain width/height and some information in it.
My problem is that some websites declare styles for div that are inherited by my div as well.
for example:
div{
background-color:red;
}
so if I don't set any background color to my div, it will show red even if I don't want that.
The only solutions I come along is to overwrite as many css proprieties, this way my div will show exactly as I want.
The problem with this solution is that there are too many css proprieties to overwrite and I want my script to be as light as it can be.
So my question is if you know another solution to my problem.
It can be in css/javascript /jQuery.
Thanks
"Resetting" styles for a specific element isn't possible, you'll have to overwrite all styles you don't want/need. If you do this with CSS directly or using JQuery to apply the styles (depends on what's easier for you, but I wouldn't recommend using JavaScript/JQuery for this, as it's completely unnecessary).
If your div is some kind of "widget" that can be included into other sites, you could try to wrap it into an iframe. This will "reset" the styles, because its content is another document, but maybe this affects how your widget works (or maybe breaks it completely) so this might not be possible in your case.
Only set the relevant / important CSS properties.
Example (only change the attributes which may cause your div to look completely different):
background: #FFF;
border: none;
color: #000;
display: block;
font: initial;
height: auto;
letter-spacing: normal;
line-height: normal;
margin: 0;
padding: 0;
text-transform: none;
visibility: visible;
width: auto;
word-spacing: normal;
z-index: auto;
Choose a very specific selector, such as div#donttouchme, <div id="donttouchme"></div>. Additionally, you can add `!important before every semicolon in the declaration. Your customers are deliberately trying to mess up your lay-out when this option fails.
You could try overwriting the CSS and use auto
I don't think this will work with color specifically, but I ran into an issue where i had a parent property such as
.parent {
left: 0px;
}
and then I was able to just define my child with something like
.child {
left: auto;
}
and it effectively "reset" the property.
Technically what you are looking for is the unset value in combination with the shorthand property all:
The unset CSS keyword resets a property to its inherited value if it inherits from its parent, and to its initial value if not. In other words, it behaves like the inherit keyword in the first case, and like the initial keyword in the second case. It can be applied to any CSS property, including the CSS shorthand all.
.customClass {
/* specific attribute */
color: unset;
}
.otherClass{
/* unset all attributes */
all: unset;
/* then set own attributes */
color: red;
}
You can use the initial value as well, this will default to the initial browser value.
.otherClass{
/* unset all attributes */
all: initial;
/* then set own attributes */
color: red;
}
As an alternative:
If possible it is probably good practice to encapsulate the class or id in a kind of namespace:
.namespace .customClass{
color: red;
}
<div class="namespace">
<div class="customClass"></div>
</div>
because of the specificity of the selector this will only influence your own classes
It is easier to accomplish this in "preprocessor scripting languages" like SASS with nesting capabilities:
.namespace{
.customClass{
color: red
}
}
Try this: Create a plain div without any style or content outside of the red div. Now you can use a loop over all styles of the plain div and assign then to your inner div to reset all styles.
Of course this doesn't work if someone assigns styles to all divs (i.e. without using a class. CSS would be div { ... }).
The usual solution for problems like this is to give your div a distinct class. That way, web designers of the sites can adjust the styling of your div to fit into the rest of the design.
As long as they are attributes like classes and ids you can remove them by javascript/jQuery class modifiers.
document.getElementById("MyElement").className = "";
There is no way to remove specific tag CSS other than overriding them (or using another element).
you may use this below option.
<style>
div:not(.no_common_style){
background-color:red;
}
</style>
now , if their any place where you do not want to apply default style you can use 'no_common_style' class as class.
ex:
<div class="no_common_style">
It will not display in red
</div>
From what I understand you want to use a div that inherits from no class but yours. As mentioned in the previous reply you cannot completely reset a div inheritance. However, what worked for me with that issue was to use another element - one that is not frequent and certainly not used in the current html page. A good example, is to use instead of then customize it to look just like your ideal would.
area { background-color : red; }
One simple approach would be to use the !important modifier in css, but this can be overridden in the same way from users.
Maybe a solution can be achieved with jquery by traversing the entire DOM to find your (re)defined classes and removing / forcing css styles.
In my css i have
* {
font-family: "Meiryo UI" , Verdana;
font-size:13px;
}
input, select, textarea {
font-size: 12px;
border-color: #333;
color: #333;
border-style: solid;
border-width: 1px;
}
But, i want one DIV without CSS.
I tried
$('#preview').removeClass()
$('#preview').removeAttr("style")
$('#preview').children().removeAttr/Class
But...but without result.
Help me to remove all style from this div with Jquery or just some javascript.
He come from stylesheet.
This is preview pic for my question: https://emailinvest.com/preview.jpg what i want.
Inheritance in CSS is more often helpful than not helpful, therefore take another route by either:
a) Override the styles you specified
#preview .input, #preview select, #preview textarea { }
or
b) Make the styles you specified target a different area using a prefixed selector, eg
#selector * { font: 13px "Meiryo UI", Verdana; }
#selector input, #selector select, #selector textarea { }
If you want to use removeClass you must specify what class to remove or it won't work:
$('#preview input').removeClass('classToRemove')
$('#preview button').removeClass('classToRemove')
You can check with Firebug what class is given to that button and try to remove it like that.
Or you can set your styles directly to that:
$('#preview button').css('background','#ccc');
Or this one.. but I'm not sure it works:
$('#preview').children().removeAttr('class'); // or 'style'
Your stylesheet does not specify classes or specific elements (IDs) and therefore the styles are being applied to all elements which match by tag.
Specifically, your button is being styled by the "input" element style specified in the stylesheet.
This means you do not have any classes you can easily remove to reset the style.
Buttons in particular are very difficult to set back to their original style once they have been set to something different, particularly in a cross-browser-friendly way.
You have a few options, of which only one is sensible:
As meder has suggested above don't set the style for this div in the first place. This is the best option. You can do this by either setting explicit class names or ids for your other divs and listing them in the stylesheet, OR add a class for the div you want to ignore, and use the "not" selector, eg input:not(.myUnstyledButtonClass) (this only works in modern browsers)
Manually construct your DIV inside an IFrame so it is not subject to the main document's styling. This seems like overkill though
I haven't tested this one, but you could try creating an iFrame, rendering a button (which would be of the unstyled form) and then iterate and recurse through and copy all properties and styles of the unstyled button to the button in your div. There's a very slim possibility this would work. I wouldn't even bother trying it however....
Go with 1 - what meder has suggested. It's probably worth posting up the code you have tried using when you commented to him that "I tried...no result". Chances are you've simply misinterpreted his suggestion or overlooked something.
For an example, see:
http://jsbin.com/ikobe4
To use ":not()" you need to add unstyled (or whatever you choose) as a class on your button, eg:
<input type="button" class="unstyled" value="Button Text" />
And change the selector in the css file from
input, select, textarea {
to
input:not(.unstyled), select, textarea {
...but as mentioned, this wont work in all browsers, so your best bet is to add classes to all the other divs, and explicitly specify which divs you want to apply styles to, rather than specifying which you don't want to apply styles to