Styling elements inside slot in Polymer 2.x - polymer

I have created a polymer element which contains slots. Each slot is intended to be replaced/filled with a HTML element with children and not just plain text. How can I style elements inside the slot and not only the element at top level of the slot? Or maybe, is it wrong to use elements with children for slots?
In example:
Template file:
<dom-module id="my-element">
<template>
<style>
.class-1 {
/* Styles here works! */
}
.class-1 ::slotted(ul) {
/* Styles here works! */
}
.class-1 ::slotted(ul) a {
/* Styles here doesn't work */
}
</style>
<div class="class-1">
<slot name="s1"></slot>
</div>
</template>
<script>
class MyElement extends Polymer.Element {
static get is() { return 'my-element'; }
}
window.customElements.define(MyElement.is, MyElement);
</script>
</dom-module>
HTML file:
<my-element>
<ul slot="s1">
<li>Link 1</li>
<li>Link 2</li>
</ul>
</my-element>
Thanks for your help!

Slotted does not allow sub selectors. Which means that
.class-1 ::slotted(ul) a
simply can not work because the 'a' is a subselection.

try this, but it seems taking effect inside all instance of my-element
<my-element><style>li a {color:#333}</style>
<ul slot="s1">
<li>Link 1</li>
<li>Link 2</li>
</ul>
</my-element>

Related

CSS nth pseudo selector for non-direct children [duplicate]

I am a bit confused about the nth-of-type pseudo class, and how this is supposed to work - especially as compared to the nth-child class.
Maybe I have the wrong idea, but given this structure
<div class="row">
<div class="icon">A</div>
<div class="icon">B</div>
<div class="label">1</div>
<div class="label">2</div>
<div class="label">3</div>
</div>
..the third child element (first with class label) should (perhaps?) be selectable with
.row .label:nth-of-type(1) {
/* some rules */
}
However, at least in chrome here, it doesn't select it. It only appears to work if it is the first child in the row, which is the same as nth-child - therefore, what is the difference between this and nth-of-type?
The nth-child pseudo-class refers to the "Nth matched child element", meaning if you have a structure as follows:
<div>
<h1>Hello</h1>
<p>Paragraph</p>
<p>Target</p>
</div>
Then p:nth-child(2) will select the second child which is also a p (namely, the p with "Paragraph").
p:nth-of-type will select the second matched p element, (namely, our Target p).
Here's a great article on the subject by Chris Coyier # CSS-Tricks.com
The reason yours breaks is because type refers to the type of element (namely, div), but the first div doesn't match the rules you mentioned (.row .label), therefore the rule doesn't apply.
The reason is, CSS is parsed right to left, which means the the browser first looks on the :nth-of-type(1), determines it searches for an element of type div, which is also the first of its type, that matches the .row element, and the first, .icon element. Then it reads that the element should have the .label class, which matches nothing of the above.
If you want to select the first label element, you'll either need to wrap all of the labels in their own container, or simply use nth-of-type(3) (assuming there will always be 2 icons).
Another option, would (sadly) be to use... wait for it... jQuery:
$(function () {
$('.row .label:first')
.css({
backgroundColor:"blue"
});
});
in the picture out of added 10 elements, 8 are <p> and 2 are <i>, the two shaded part elements are indicating <i> remaining eight are <p>
the css for the page goes here:
<style>
* {
padding: 0;
margin:0;
}
section p {
width: 20px;
height: 20px;
border:solid 1px black;
border-radius: 15px;
margin:20px;
float: left;
}
section i {
width: 20px;
height: 20px;
border:solid 1px black;
border-radius: 15px;
margin:20px;
float: left;
}
section p:nth-child(1) {
background-color: green; /*first-child of p in the flow*/
}
section i:nth-child(1) {
background-color: red; /*[first-child of i in the flow]NO */
}
section i:nth-of-type(1) {
background-color: blue; /*the type i of first child in the flow*/
}
</style>
</head>
<body>
<section>
<p></p>
<p></p>
<p></p>
<p></p>
<i></i>
<p></p>
<i></i>
<p></p>
<p></p>
<p></p>
</section>
</body>
the first green bulb indicates
section p:nth-child(1) {
background-color: green; /*first-child of p in the flow*/
}
and second red bulb in the code does not work because i is not first elements in the flow
section i:nth-child(1) {
background-color: red; /*[first-child of i in the flow]NO */
}
and the blue bulb in the picture indicates the first type of i in the flow
section i:nth-of-type(1) {
background-color: blue; /*the type i of first child in the flow*/
}
.label:nth-of-type(1)
"type" here refers to the type of element. In this case, div, not to the class. This does not mean the first element which is .label, it instead means the first element of its type which also has a class of label.
There are no elements with a class of label which are at index 1 of their type.
Heres's a simple example which shows the difference between nth-child vs nth-of-type.
Consider the following html
<div>
<p>I am first</p>
<div>I am secong</div>
<p>I am 3rd</p>
</div>
Using nth-of-child
p:nth-of-child(2){
background:red;
}
The red background will be applied to the 2nd p element inside the div.
This happens because nth-of-child bascially means to find nth p tag(in this case 2nd p tag) inside a container
Using nth-child
p:nth-child(2){
background:red;
}
Here no css is applied.
This happens because nth-child basically means to find nth tag inside a container(in this case 2nd tag is div) and check if it is p tag
Here is an example:
<div>
<div >0</div>
<div >1</div>
<div >2</div>
<div >3</div>
<div >4</div>
<div >5</div>
</div>
this selector: div div:nth-child(1) will select the first child of the div but with another condition that child must be a div.
here first child is a <div>0</div> but if the first child was a paragraph p: <p>0</p> this selector will not effect the page because there is no first child div the first child is p.
but this selector: div div:nth-of-type(1) if the first child was a <div>0</div> will effect it, but if the first child is <p>0</p> now he will effect the second child <div>1</div> because it's the first child of his type div.
element:nth-of-type(p) //p=integer , basically return the DOM Element with respect of body.
Let us understand this with an example
<html>
<head>
</head>
<body>
<div>
<p> This is paragraph in first div</p>
<input type="text" placeholder="Enter something"/>
<p>This is a paragraph </p>
</div>
<div>
<p>This is paragraph in second div</p>
<ul>
<li>First Item</li>
<li>Second Item</li>
<li>
<label> This is label <strong>inside Unordered List</strong></label>
</li>
</ul>
</div>
</body>
</html>
**This above html will look like this.**
Now suppose We have to style Second Item in UnOrderd list.
In this case we can use nth-of-type(index) selector wrt DOM body.
we can achieve styling like this
<style type="text/css">
div:nth-of-type(2) li:nth-of-type(2){
color: red;
font-weight: bold;
}
</style>
explanation : div:nth-of-type(2) by this we are trying to tell find the second div in html body,after that we have second div accessible ,now we can dig inside div using same nth-of-type selector,next we are using li:nth-of-type(2) so now we can find second list inside second div and styling it .
Final Code :
<html>
<head>
<style type="text/css">
div:nth-of-type(2) li:nth-of-type(2){
color: red;
font-weight: bold;
}
</style>
</head>
<body>
<div>
<p> This is paragraph in first div</p>
<input type="text" placeholder="Enter something"/>
<p>This is a paragraph </p>
</div>
<div>
<p>This is paragraph in second div</p>
<ul>
<li>First Item</li>
<li>Second Item</li>
<li>
<label> This is label <strong>inside Unordered List</strong></label>
</li>
</ul>
</div>
</body>
</html>
**And Final output will look like this**
:nth-of-type is used to select a sibling of a particular type.
By type I mean a type of tag as in <li>, <img>, <div> etc.
:nth-child is used to select children of a particular parent tag without regard to a type
Example of :nth-of-type
HMTL:
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li>Item 4</li>
<li>Item 5</li>
<li>Item 6</li>
<li>Item 7</li>
<li>Item 8</li>
<li>Item 9</li>
<li>Item 10</li>
<li>Item 11</li>
<li>Item 12</li>
<li>Item 13</li>
<li>Item 14</li>
<li>Item 15</li>
<li>Item 16</li>
</ul>
CSS:
Notice that there is no space between the <li> tag and the pseudo-class nth-of-type.
li:nth-of-type(odd) { background-color: #ccc; }
Result:
https://jsfiddle.net/79t7y24x/
Example of :nth-child
HTML:
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li>Item 4</li>
<li>Item 5</li>
<li>Item 6</li>
<li>Item 7</li>
<li>Item 8</li>
<li>Item 9</li>
<li>Item 10</li>
<li>Item 11</li>
<li>Item 12</li>
<li>Item 13</li>
<li>Item 14</li>
<li>Item 15</li>
<li>Item 16</li>
</ul>
CSS:
Notice here that there is a space between the <ul> tag and the :nth-child pseudo-class
ul :nth-child(even) { background-color: red }
Result: https://jsfiddle.net/o3v63uo7/

Trying toggle method to introduce new class on button click

I am trying to introduce new class for specific div element using nth-child() pseudo-class but it is not working!
Am I doing something wrong?
Note that menu, container, newClass are already defined in CSS section and I'm not showing them here.
Here is what I am trying:
$(function() {
$("button").click(function {
$("div:nth-child(2)").toggleClass("newClass");
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button>Toggle CSS</button>
<div class="container">
<div class="menu">
<lu>
<li>option 1</li>
<li>option2</li>
<li>option 3</li>
</lu>
</div>
</div>
There are lots of mistakes in your code. Which div are you want to add a class newClass? I think your code should be like this
$(function() {
$("button").on('click', function() {
$(".menu").toggleClass("newClass");
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button>Toggle CSS</button>
<div class="container">
<div class="menu">
<ul>
<li>option 1</li>
<li>option2</li>
<li>option 3</li>
</ul>
</div>
</div>
My mistake
I should not use
<script>
$("div:nth-child(2)").toggle Class("newClass");
</script>
this to introduce new class for div element, with the class menu , as it is not a second child of div . What given below is though working fine for me:
Correction
<script>
$("div.menu").toggleClass("newClass");
</script>
That is: to specify div element(whose class is going to be toggled) by tagging its class menu , to call jQuery for div with the class menu .
Also by mistake parentheses () were missing.

Odd/Even background color over image [duplicate]

I am a bit confused about the nth-of-type pseudo class, and how this is supposed to work - especially as compared to the nth-child class.
Maybe I have the wrong idea, but given this structure
<div class="row">
<div class="icon">A</div>
<div class="icon">B</div>
<div class="label">1</div>
<div class="label">2</div>
<div class="label">3</div>
</div>
..the third child element (first with class label) should (perhaps?) be selectable with
.row .label:nth-of-type(1) {
/* some rules */
}
However, at least in chrome here, it doesn't select it. It only appears to work if it is the first child in the row, which is the same as nth-child - therefore, what is the difference between this and nth-of-type?
The nth-child pseudo-class refers to the "Nth matched child element", meaning if you have a structure as follows:
<div>
<h1>Hello</h1>
<p>Paragraph</p>
<p>Target</p>
</div>
Then p:nth-child(2) will select the second child which is also a p (namely, the p with "Paragraph").
p:nth-of-type will select the second matched p element, (namely, our Target p).
Here's a great article on the subject by Chris Coyier # CSS-Tricks.com
The reason yours breaks is because type refers to the type of element (namely, div), but the first div doesn't match the rules you mentioned (.row .label), therefore the rule doesn't apply.
The reason is, CSS is parsed right to left, which means the the browser first looks on the :nth-of-type(1), determines it searches for an element of type div, which is also the first of its type, that matches the .row element, and the first, .icon element. Then it reads that the element should have the .label class, which matches nothing of the above.
If you want to select the first label element, you'll either need to wrap all of the labels in their own container, or simply use nth-of-type(3) (assuming there will always be 2 icons).
Another option, would (sadly) be to use... wait for it... jQuery:
$(function () {
$('.row .label:first')
.css({
backgroundColor:"blue"
});
});
in the picture out of added 10 elements, 8 are <p> and 2 are <i>, the two shaded part elements are indicating <i> remaining eight are <p>
the css for the page goes here:
<style>
* {
padding: 0;
margin:0;
}
section p {
width: 20px;
height: 20px;
border:solid 1px black;
border-radius: 15px;
margin:20px;
float: left;
}
section i {
width: 20px;
height: 20px;
border:solid 1px black;
border-radius: 15px;
margin:20px;
float: left;
}
section p:nth-child(1) {
background-color: green; /*first-child of p in the flow*/
}
section i:nth-child(1) {
background-color: red; /*[first-child of i in the flow]NO */
}
section i:nth-of-type(1) {
background-color: blue; /*the type i of first child in the flow*/
}
</style>
</head>
<body>
<section>
<p></p>
<p></p>
<p></p>
<p></p>
<i></i>
<p></p>
<i></i>
<p></p>
<p></p>
<p></p>
</section>
</body>
the first green bulb indicates
section p:nth-child(1) {
background-color: green; /*first-child of p in the flow*/
}
and second red bulb in the code does not work because i is not first elements in the flow
section i:nth-child(1) {
background-color: red; /*[first-child of i in the flow]NO */
}
and the blue bulb in the picture indicates the first type of i in the flow
section i:nth-of-type(1) {
background-color: blue; /*the type i of first child in the flow*/
}
.label:nth-of-type(1)
"type" here refers to the type of element. In this case, div, not to the class. This does not mean the first element which is .label, it instead means the first element of its type which also has a class of label.
There are no elements with a class of label which are at index 1 of their type.
Heres's a simple example which shows the difference between nth-child vs nth-of-type.
Consider the following html
<div>
<p>I am first</p>
<div>I am secong</div>
<p>I am 3rd</p>
</div>
Using nth-of-child
p:nth-of-child(2){
background:red;
}
The red background will be applied to the 2nd p element inside the div.
This happens because nth-of-child bascially means to find nth p tag(in this case 2nd p tag) inside a container
Using nth-child
p:nth-child(2){
background:red;
}
Here no css is applied.
This happens because nth-child basically means to find nth tag inside a container(in this case 2nd tag is div) and check if it is p tag
Here is an example:
<div>
<div >0</div>
<div >1</div>
<div >2</div>
<div >3</div>
<div >4</div>
<div >5</div>
</div>
this selector: div div:nth-child(1) will select the first child of the div but with another condition that child must be a div.
here first child is a <div>0</div> but if the first child was a paragraph p: <p>0</p> this selector will not effect the page because there is no first child div the first child is p.
but this selector: div div:nth-of-type(1) if the first child was a <div>0</div> will effect it, but if the first child is <p>0</p> now he will effect the second child <div>1</div> because it's the first child of his type div.
element:nth-of-type(p) //p=integer , basically return the DOM Element with respect of body.
Let us understand this with an example
<html>
<head>
</head>
<body>
<div>
<p> This is paragraph in first div</p>
<input type="text" placeholder="Enter something"/>
<p>This is a paragraph </p>
</div>
<div>
<p>This is paragraph in second div</p>
<ul>
<li>First Item</li>
<li>Second Item</li>
<li>
<label> This is label <strong>inside Unordered List</strong></label>
</li>
</ul>
</div>
</body>
</html>
**This above html will look like this.**
Now suppose We have to style Second Item in UnOrderd list.
In this case we can use nth-of-type(index) selector wrt DOM body.
we can achieve styling like this
<style type="text/css">
div:nth-of-type(2) li:nth-of-type(2){
color: red;
font-weight: bold;
}
</style>
explanation : div:nth-of-type(2) by this we are trying to tell find the second div in html body,after that we have second div accessible ,now we can dig inside div using same nth-of-type selector,next we are using li:nth-of-type(2) so now we can find second list inside second div and styling it .
Final Code :
<html>
<head>
<style type="text/css">
div:nth-of-type(2) li:nth-of-type(2){
color: red;
font-weight: bold;
}
</style>
</head>
<body>
<div>
<p> This is paragraph in first div</p>
<input type="text" placeholder="Enter something"/>
<p>This is a paragraph </p>
</div>
<div>
<p>This is paragraph in second div</p>
<ul>
<li>First Item</li>
<li>Second Item</li>
<li>
<label> This is label <strong>inside Unordered List</strong></label>
</li>
</ul>
</div>
</body>
</html>
**And Final output will look like this**
:nth-of-type is used to select a sibling of a particular type.
By type I mean a type of tag as in <li>, <img>, <div> etc.
:nth-child is used to select children of a particular parent tag without regard to a type
Example of :nth-of-type
HMTL:
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li>Item 4</li>
<li>Item 5</li>
<li>Item 6</li>
<li>Item 7</li>
<li>Item 8</li>
<li>Item 9</li>
<li>Item 10</li>
<li>Item 11</li>
<li>Item 12</li>
<li>Item 13</li>
<li>Item 14</li>
<li>Item 15</li>
<li>Item 16</li>
</ul>
CSS:
Notice that there is no space between the <li> tag and the pseudo-class nth-of-type.
li:nth-of-type(odd) { background-color: #ccc; }
Result:
https://jsfiddle.net/79t7y24x/
Example of :nth-child
HTML:
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li>Item 4</li>
<li>Item 5</li>
<li>Item 6</li>
<li>Item 7</li>
<li>Item 8</li>
<li>Item 9</li>
<li>Item 10</li>
<li>Item 11</li>
<li>Item 12</li>
<li>Item 13</li>
<li>Item 14</li>
<li>Item 15</li>
<li>Item 16</li>
</ul>
CSS:
Notice here that there is a space between the <ul> tag and the :nth-child pseudo-class
ul :nth-child(even) { background-color: red }
Result: https://jsfiddle.net/o3v63uo7/

What priority are CSS styles applied? Example provided that is confusing me

I know this should be a stupidly easy topic, but I'm confused and I spent way longer than I should trying to understand this example that my professor gave us.
From what I understand, the rules are supposed to be that you go first for inline css, then document css, then external css, and then priority. The priority was from what I thought 100*IDs+10*Class+1*Element reference.
Here is the code that's really confusing me below:
<div id="id1">
<ul>
<li class="c1" id="id2">Item 1</li>
<li class="c2 c3">Item 2</li>
<li class="c3">Sublist:
<ul>
<li class="c1">Subitem 1</li>
<li class="c2">Subitem 2</li>
<li class="c2" id=”id3”>Subitem 3</li>
</ul></li>
<li>Item 4</li>
</ul>
<div id="id4">
<ul>
<li id="id5">One thing</li>
<li id="id6" class="c2">And another thing</li>
<li id="id7" class="c1">A third thing.</li>
</ul>
</div>
</div>
<style>
div > li
{
color: yellow;
}
.c2
{
color: red;
}
ul li+li+li
{
color: green;
}
#id1, #id4
{
color: orange;
}
#id7
{
color: blue;
}
</style>
In the above code though, item 2 is red, sublist(+subitem1) are green, etc. How is that possible? Why isn't everything coming out orange? Shouldn't the #id1 style be applied to everything automatically since it is the only one with an ID specifier(and everything is a child of div with id="id1"?
The priority formula you wrote is ok if you have two rules to same element.
In css child element inherits from parents the colors, like in your example.
If new rules, despite having a lower specificity number, is applied to child, this override the inheritance.
Anyway read this: https://css-tricks.com/specifics-on-css-specificity/
An element's style overrides it's inheritance.In cases when a style was not defined we will prefer inherited value over default.
There is misunderstanding.
It did orange at first place to all tags .but later as some of the tags are defined different according to css, then it changes to specified color that you mentioned in css.
For CSS types:
http://webdesign.about.com/od/css/qt/aatypesofcss.htm
edited.

How do I rewrite this HTML to validate to XHTML 1.0 Strict?

How do I rewrite this HTML to validate to XHTML 1.0 Strict?
<div>
<a href="link.html">
<p>Some text</p>
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
</a>
</div>
My intent is to have the entire div serve as a clickable link. The content within simply describes the contents of the destination page. W3 Validator says I can't have a block element (<p>) inside a span tag (<a>).
How do I rearrange this so that my DIVs remain block links?
You're not allowed to wrap a block level element in an inline level element so you have a few alternatives.
You can wrap every line that you want linked in the ...
<div>
<p>Some text</p>
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
</div>​
You can add a javascript onclick function to reproduce the same results.
//jQuery
​$('div').click(function() {
window.location = 'link.html';
});​​​​​​
//Non jQuery
document.getElementById('yourDiv').onclick = function() {
window.location = 'link.html';
}
If you use the Javascript, make sure you use CSS to make it apparent that the contents are links. I'd recommend pseudo classes
div {
text-decoration: underline;
color: #0000FF;//or whatever your link color is
}
div li:hover, p:hover {
color: #CC00FF;
cursor: pointer;
}
​
You can't rearrange it to make the block a link. What you could do is to make every single element in the block a link, or you can use javascript.
<div style="cursor:pointer" onclick="location.href='link.html'">
<!-- ... -->
</div>
As is, your fragment is valid HTML5. Use that instead and your problem vanishes. All you have to do is change the doctype to <!doctype html>.
Your <UL> is also a block element, so it won't work there either. How about:
<div onclick="location.href = 'link.html'">
<p>Some text</p>
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
</div>