HTML Element styling not working in scoped style - html

For some reason my element styling isn't working when using scoped. When inspecting the element the styling is not applied when using scoped. I need to use scoped because I want the styling only be applied within this component. I'm using nuxt.js, no idea if this has anything to do with the problem.
Not working:
<style scoped>
a {
color: red !important;
text-decoration: underline !important;
}
</style>
Working:
<style>
a {
color: red !important;
text-decoration: underline !important;
}
</style>
Any ideas?

As explained in the relevant documentation, scoped CSS applies a data- attribute to all selectors in the provided CSS so that it only applies to the elements of the component, not outside of it, nor to nested sub-components.
For example, with this markup:
<app>
<a>outside link</a>
<Parent>
<a>parent link</a>
<Child>
<a>child link</a>
</Child>
</Parent>
</app>
scoped CSS of <Parent> will only affect parent link and will not affect the outside link nor the child link.
From what you're describing, you are trying to style a sub-component link.
To make your scoped CSS selectors affect deeply (apply to sub-components as well) you have to use the deep >>> combinator:
<style scoped>
* >>> a {
color: red;
text-decoration: underline;
}
</style>
To see it in action, consider this example.

Related

How to set styles to a slotted element in a template tag

<template id="element-details-template">
<link href="./src/components/lit-button/button.scss" rel="stylesheet">
<svg ..</svg>
<span slot="buttonLabel">span1</span>
<span class="name" slot="buttonSubLabel">span2</span>
</template>
I want to style just the second span in an external css file. But the styles are not getting affected/reflected. I am confused as to what the problem might be?
The below code is my css:
:host {
#element-details-template {
span.name {
text-transform:lowercase;
}
}
If the class is in an external css file (like the global one styles.css, or a parent one) just try removing the :host directive. To be sure it should affect the span element, use also !important directive. You can also specify the only class/classes you want to affect with the css class like:
(if the class were there's the html code is called for example "test.html")
app-test{
#element-details-template{
span.name {
text-transform:lowercase !important;
}
}
}
While if you want this class to affect the entire application (if you put this css class in the styles.css) when you use those id and class name, you can remove the app-test block:
#element-details-template{
span.name {
text-transform:lowercase !important;
}
}
I tried and with the :host block it didn't work but in this way it should work. Hope it's helpful :)

The proper way to restyle a core paper element globally in Polymer

The easy answer is, just include
paper-button{
background: black;
}
, but that wouldn't restyle the element if it is contained in another element. The solution used to be
html /deep/ paper-button{
background: black;
}
which still works fine, but is deprecated from the Shadow DOM spec. So what is the proper solution?
PS. Purely to be complete in case it somehow matters: What I actually want to reproduce properly is
html /deep/ paper-button.main{
[...]
}
You can use CSS custom properties to change the paper-button style globally.
Since paper-button exposes the --paper-button mixin, you can try the following inside your document -
<style is="custom-style">
simple-dialog, paper-button {
--paper-button: {
background-color: black;
color: white;
};
}
</style>
Have a look at this plunker.

Selector in CSS is affecting other elements

This is my CSS code that is supposed to reformat links:
a {
color: #120000;
text-decoration: underline;
}
This is my HTML code:
<div id="intro" class="grid_9">
<h1>This site just might change your life</h1>
<p><a href="#" class=button>Browse Our Features</a></p>
</div>
The problem is that the header (but only this one) is being affected in the same way as the links. How can I fix this?
Just add a class to the <a> tags that should not follow it or use the existing one depending on whether or not that class is specifically for that purpose. Then, use the :not() selector:
a:not(.button) {
color: #120000;
text-decoration: underline;
}
Fiddle: Fiddle
Also, the header would only follow the CSS for the <a> if it were wrapped in an <a> tag. If this is true, give that <a> tag the set class.

Apply <style> rules to another element instead

I have a <style> element that applies some global styles, like A { color: red } as opposed to my stylesheet that styles A { color: green } (purely as an example).
How can I modify all of the styling in my <style> element so that are "contained" and only apply the styling to children of a parent element I specify.
A { color: red } becomes #myelem A { color: red }
.myclass { display: none; } becomes #myelem .myclass { display: none }
Would I have to find the <style> element, parse the contents, replace each selector then update the element contents with it fixed?
Is there an alternative to modifying the contents of my <style> element?
Why am I doing this? I have HTML that is stored in a database that I want to edit (this is a CMS). The HTML can contain <style> elements, and I cannot trust who writes the CSS to write it in scope. I could prevent users from using the <style> element, but I'd rather not. I have no control over the original CSS. Only what I get back in my server/clientside code.
If all else fails, I might have to load it into an iFrame... :(
Ooooooo Firefox supportes <style scoped> which only applies to the parent where the <style> element is located in the DOM. Too bad it's not supported in any other browser. :(
As you mentioned, you can use:
<style scoped>
...
</style>
Although it is only supported natively in Firefox, you can use this jQuery polyfill to get it to work in other browsers.
Use the child selector:
#yourElement > a { color: green; }
JQuery:
$( "parent_type > children_type" ).css("param","value");
example
$( "#myid > div" ).css("display","none");
here's a link
JSFiddle

Is it possible to select all elements which do not have any CSS class?

The W3C Recommendations defines the ability to select any anchor which has a defined class by using A[CLASS]{/*Code*/}, but is there an antecedent?
Can I select all anchors that do NOT have a class?
My instinct is to use A[~CLASS]{/*Code*/}, but that isn't documented, and A[CLASS=]{/*Code*/} selects all anchors which have a class name which is an empty string (for instance, <A CLASS="" HREF="http://example.com/">This anchor has a class, but it's empty.</A>).
Example usage
In this code, I only want classless links to be green.
<HTML>
<HEAD>
<LINK REL="stylesheet" TYPE="text/css" HREF="http://example.com/externalUneditableStyles.CSS" />
<STYLE>
A.deadLink{
color:#FF0000 !important;
}
A{
color:#00FF00;
}
</STYLE>
</HEAD>
<BODY>
This link is red, as specified by the CSS above.
This link is green, as specified by the CSS above.
This is a link to a child page, and is styled by external uneditable CSS. That CSS doesn't specify a color, so it is green, even though that might not be the intended effect.
</BODY>
</HTML>
Use something like this:
a:not([class]) {
/*some styling here*/
}
Little demo: little link.
That's exactly what cascading is for, if you do:
a { color: #000; } /* this targets all anchors */
Then after this in your stylesheet you can do
a.classname { color: #ffcc00; } /* to override the color for the once with the classname class defined. */
I hope that's clarifies your question.
Try to play with the .foo:not(.bar)-pseudo selector. I also advise you not to code tags in caps. I believe it is allowed in HTML5, but not in XHTML. Even so, the practice is frowned upon.