Dynamically adding and removing AngularJS templates, while scoping the CSS narrowly - css

So I have a few different templates which get put into ng-view, and each template is animated into being using the slick new ng-animate directive. Each template has its own CSS, and I need to figure out a way to prevent the CSS from colliding, so that an h1 declaration in template1 only applies to template1, and not template2.
The options for which I'm currently aware are either to rewrite the CSS as it's about to be served to the page to target a parent element, which I could define as an ID which matches the template. The other, less desirable option is to use iframes, which means i'd have several instances of the app running at once, each in their own iframe.
Are there any other options out there? Which way is best to solve the goal of isolating each CSS declaration?

You could have your controller assign a body className and use that to scope out the CSS styles per template.

Related

Gatsby.js: I set up a css rule for body for a template but it works throughout the entire app

I set up a background image with css (background-image) in the body tag of the template so that only shows in the pages generated with it, however that's affecting all 'body's in the entire gatsby.js website.
This is normal behavior. It's not a Gatsby issue. It's how React's templating/code-splitting works.
You are defining a CSS rule in your isolated CSS but it's bundled when the project is compiled (because of webpack) and because of the specificity, it affects all body tag. In the end, your template will be also injected into the output HTML so all the imports in it will also merge in the final output.
The easiest and most straightforward solution I think is to define a <section> (or another tag) just as a direct child of the body for each template/page you want to customize and give a specific class name to apply the CSS only to that template/page. Increasing the specificity is the easiest way to apply.
Soon, in the new Chromium version (99) we will be able to define layered components in order to enhance the specificity and improve that kind of behaviors you've described.

styling a customElement like an existing element

How to style a custom element like an existing element? (For example, apply the document <pre> style to a <special-pre> custom element)
BACKGROUND
typical custom elements are based on the generic HTMLElement, in parts because the is="special-pre" attribute is not supported in all browsers (ie. iOS).
page css detailed rules are not always accessible through code, for example when the main page uses a stylesheet through a CDN (CORS restriction)
I've tried through javascript to filter applicable pre style selectors and create a new rule for the new custom element but I hit a wall with CDN provided stylesheets - CORS restriction prevent access to individual rules.
I also thought of inserting a dummy element and use getComputedStyle(preElement) but it feels like a hack and messes with the user's HTML markup.
Other solutions (::part, css custom properties) require advance knowledge of the document style that will use the custom element.
Note that <pre> is just an example. Looking for a solution in general to make a customElement use a defined user style so that a customElement can be generic enough to be used in different pages with different stylesheets.

CSS - Is it possible for html in an include to ignore styles from a file referenced in the main page?

I've developed a dynamic ad banner that consits of html and styles loaded into the host site via javascript. On one particular site, certain styles in the stylesheet for the main page are affecting the html that I'm dynamically loading.
Is there a technique for having the dynamically loaded html only render styles from the css I have loaded along with the html, and ignoring any styles in the host page?
Any advice appreciated.
Put your banner into an iframe.
Add !important to your CSS like
p { color: #ff0000 !important; }
Yeah there is a real easy way. Why dont you have your classes separated form the main page HTML. Give them a unique identification if you want there to be no conflict.
Example
Your main page has a css class .input
Give your dynamically loading page as .Dybamically_input this will server something as a namespace.Also you can use !important to the properties which you definitely want to added.
There is an evolving standard to introduce scope blocks to CSS but that isn't yet supported enough to be of any use. You can use the !important directive, but that is likely also to affect the underlying document if you don't apply it carefully.
The best solution is to create a scope by including all the the HTML in your add banner inside a div with a uniquely named class (and use your own namespace eg. 'cog_myAd' to try to guarantee uniqueness. Then apply styles just to that class, using !important where you might need to override styles that could be changed lower down the cascade of styles.
If you have attached your CSS file to the HTML page then the only solution to it would be using !important for all conflicting CSS properties -
.className{
color: red !important;
}
Use inline styles (the style attribute on all your banner elements you want to style) instead of external css file - this way you will never have a conflict.
The other option as others suggested is to use IFrame.

CSS gets messed when script is injected

I built an extension which, whenever user visits some specific sites, I inject my script on the top of there web pages. I used
document.body.insertBefore(wrapperDiv, document.body.firstChild)
to do so.
Now problem: CSS of injected script gets messed up for each and every site(differ from one site to another).
How should I maintain single css structure for all sites?
You should be able to solve this problem by using unique IDs for your html tags with CSS.
That is, if your DIV CSS properties are interfering with their DIV CSS add a #uniqueNameHere ID to your DIV and set the CSS for the #ID.
This page on the use of the !important keyword may be useful too.
http://css-tricks.com/9462-when-using-important-is-the-right-choice/
Use unique selectors for your elements (be it classes with specific prefix or similarly constructed IDs), but you probably try to include CSS along with your script, which may not be a good idea.
In some cases the inline styling is the best idea - it will overwrite all the styles for your elements and will make sure the outlook of these elements is consistent across different pages.
So, I would say, go with inline styling.
For documentation on how the styles are overwritten in CSS 2.1, please see the following page: http://www.w3.org/TR/CSS21/cascade.html#specificity

wikia template style attribute

I have made some templates on wikia.com, which contain only CSS code (key:value;).
My problem is having another template use these style templates in a style attribute tag.
style="{{MyTemplateStyle}}"
This code does not evaluate as expected. The CSS code is outputted before the element and the style attribute is not included inside the element.
Am I trying something not possible for a wiki ?
I merely want to be able to change styling on certain templates in one place, like regular HTML & CSS pages.
CSS styling specified from the style="" attribute always takes priority over any other css, even if you use !important in a CSS specification.
Therefore any edits you make to your CSS on Wikia will not ever override the CSS specified inside an attribute.
Kim, you were right to switch to classes instead of embedding in-line styles via templates.
The very idea of using templates suggest that this was going to be re-used in more than one place, applying styles to a group or, in fact, a class of elements.
This approach is much simpler to read and maintain (as you only have one, central place to edit), and also, if done right, will enable you to seamlessly change the colour scheme via Special:ThemeDesigner.

Resources