Mechanism I have to work with, that is not changable:
Page is rendered with some styles on them eg. class xy.
Some html is generated dynamically and injected into:
<div id="InternalContent"> /* injected here */ </div>
by CSS is applied by doing:
#InternalContent {
/* .less files here */
}
The goal is to be able to render some elements inside #InternalContent with original classes, not overwritten by #InternalContent.
I tried:
#InternalContent:not(.NotInherited) { ... }
#InternalContent:not(*:not(.NotInherited)) { ... }
and some others, but to no success.
The only way this works is if the classes themself have :not(.NotInherited).
#InternalContent .xy:not(.NotInherited) { }
but in my case there are far to many classes to change manually for this to be an acceptable solution.
Smallest (not) working example:
https://jsfiddle.net/nvhouq1k/
HTML:
<p class="xy">This to be orange</p>
<div id="InternalContent">
<p class="xy">This to be blue</p>
<div class="NotInherited">
<p class="NotInherited xy">This to be orange</p>
</div>
</div>
CSS:
.xy {
color: orange;
}
#InternalContent:not(*:not(.NotInherited)) .xy{
color: blue;
}
/* *:not(.NotInherited) */
/* :not(.NotInherited) */
Change the order of your not() selector so that it only targets .xy elements: You can try the following:
.xy {
color: orange;
}
#InternalContent .xy:not(.NotInherited) {
color: blue;
}
<p class="xy">This to be orange</p>
<div id="InternalContent">
<p class="xy">This to be blue</p>
<div class="NotInherited">
<p class="NotInherited xy">This to be orange</p>
</div>
</div>
Related
I'm trying to target an inner element with Sass using the :hover selector, but for some reason when using the partial selector with the underscore, the property gets ignored. The only workaround is to type the entire class name:
HTML:
<div class="cds__grid">
<div class="cds__card">
<img src="images/content/image.svg">
<div class="cds__card-overlay">
<h4>A Title</h4>
<p>Some Text</p>
</div>
</div>
</div>
SCSS:
.cds{
&__grid{
//Some Properties
}
&__card{
&-overlay{
background-color: grey;
}
&:hover{
//Ignored
&-overlay{
background-color: blue;
}
//Applied on Hover
.cds__card-overlay{
background-color: blue;
}
}
}
}
Here is the solution, you need to add & after hover to keep reference of the parent class
CodePen Link
.cds{
&__grid{
//Some Properties
}
&__card{
&-overlay{
background-color: grey;
}
&:hover &{
// Ignored
&-overlay{
background-color: blue;
}
}
}
}
<div class="cds__grid">
<div class="cds__card">
<img src="images/content/image.svg">
<div class="cds__card-overlay">
<h4>A Title</h4>
<p>Some Text</p>
</div>
</div>
</div>
To understand why this is ignored it helps to learn more about the ampersand in SCSS.
This is not some magic tool that sorts selectors, it just inserts the parent at the point where you write the &.
So if you write something like this:
.class {
&:hover {
&-detail {
}
}
}
This will be compiled into this:
.class {}
.class:hover {}
.class:hover-detail {}
Note the last selector, this is not a valid CSS selector, so this is why it get's ignored.
To achieve something like .class-detail:hover you would have to do something like this:
.class {
&:hover {
}
&-detail {
&:hover {
}
}
}
You simply missed a } at the end of your code, and also an & sign to refer to your original element.
See in codepen.io: https://codepen.io/basescriptnet/pen/KKqWweo
.cds{
&__grid {
//Some Properties
}
&__card{
&-overlay {
background-color: grey;
}
&:hover &{
//Ignored
&-overlay {
background-color: blue;
}
//Applied on Hover
.cds__card-overlay {
background-color: blue;
}
}
}
}
<div class="cds__grid">
<div class="cds__card">
<img src="images/content/image.svg">
<div class="cds__card-overlay">
<h4>A Title</h4>
<p>Some Text</p>
</div>
</div>
</div>
Update. I missed & sign after :hover, which led to wrong execution of the code. Instead of referring to parent, it was referring to .cds__card:hover-overlay.
I have a .header div with a span maindomain and a div otherdomains inside of it:
<div class="header"><span class="maindomain">LatestFooty.co.uk</span> is currently available for sale, along with:
<div class="otherdomains">
LatestFootie.com<br>
LatestFootie.co.uk
</div>
</div>
I'm trying to target the is currently available for sale, along with:, without touching the contents of .maindomain or .otherdomains. I understand that the best approach to this might be to wrap it in a span and target that instead, but at this point I'd like to figure out why I can't get the :not pseudo-class working.
Here is what I have:
#media (min-width:300px) and (max-width:450px) {
.header:not(.maindomain):not(.otherdomains) {
font-style: italic;
}
}
As far as I can tell, the syntax is correct, and I don't think it's a specificity issue because !important doesn't make a difference. What am I doing wrong?
.header:not(.maindomain):not(.otherdomains) only targets elements which have the .header class and don't have the .maindomain and/or the .otherdomain class themselves.
Your rules currently say:
<div class="header"> is targeted
<div class="header maindomain"> is not targeted
<div class="header otherdomains"> is not targeted
<div class="header maindomain otherdomains"> is not targeted
But this is not what you want to do here obviously.
You cannot apply rules to the .header class depending on classes of its children with CSS alone.
There's an approved answer to your question here which might guide you in the right direction (using JavaScript or jQuery in that case).
You will need two selectors:
.header {
font-style:italic;
}
.header .otherdomains,
.header .maindomain {
font-style:initial;
}
/* OR
.header * {
font-style:initial;
}
*/
<div class="header"><span class="maindomain">LatestFooty.co.uk</span> is currently available for sale, along with:
<div class="otherdomains">
LatestFootie.com<br>
LatestFootie.co.uk
</div>
</div>
I'm trying to target the "is currently available for sale, along with:", without touching the contents of .maindomain or .otherdomains.
You can't target anonymous elements in CSS.
CSS rules need a "hook" in the HTML to attach to. That hook is an HTML tag. Without the tag, CSS has nothing to target. This concept applies across box models.
From MDN:
An anonymous box is created when there is not an HTML element to use for the box. This situation happens when, for example, you declare display: flex on a parent element, and directly inside there is a run of text not contained in another element. In order to fix the box tree, an anonymous box is created around that run of text. It will then behave as a flex item, however, it cannot be targeted and styled like a regular box because there is no element to target.
(emphasis mine)
Everything is in the demo itself, the JavaScript is for demo purposes.
Demo
const lnx = [...document.links];
lnx.forEach(lnk => lnk.addEventListener('click', viewHTML));
function viewHTML(e) {
const link = e.target;
const headers = document.querySelectorAll('.'+this.dataset.tag);
headers.forEach(hdr => {
if (!hdr.matches('.hide')) {
link.className = 'off';
let str = hdr.outerHTML;
let txt = document.createElement('div');
txt.className = 'txt';
hdr.insertAdjacentElement('afterend', txt);
hdr.nextElementSibling.insertAdjacentText('beforeend', str);
hdr.classList.add('hide');
} else {
link.className = '';
hdr.classList.remove('hide');
hdr.nextElementSibling.remove();
}
});
}
body {
font: 400 2.5vw/1.5 Consolas
}
[class^=header] {
font-family: Arial;
}
/* Header (OP)
Selector fails -- :not() is prefixed incorrectly
.header:... means .header is targeted
.header :... means the descendants of .header is targeted
There is no .header.A, .header.B, nor .header.A.B
so .header without .A and/or .B will have everything in italics
*/
.header:not(.A):not(.B) {
font-style: italic;
}
/* Header 1
Best solution with no extra HTML tags:
Assign font-style: normal...
directly (.C1, .D1)
or by class (.N)
*/
.header1 {
font-style: italic;
}
.C1,
.D1,
.N {
font-style: normal;
}
/* Header 2
Using :not() needs extra HTML tag:
Wrap second textnode in an inline or inline-block tag
As content of a descendant tag, the text can be targeted
*/
.header2 *:not(.E):not(.F) {
font-style: italic;
}
/* Header 3
Smart solution with extra HTML tag:
Wrap second textnode in <i> or <em>
*/
.header3 {
/* no styles needed */
}
/* Header 4
Slickest solution with least HTML:
Wrap text that needs italics in <i> and then style lines with CSS
*/
.header4 {
white-space: pre-line;
}
/* For Demo Purposes */
.dash {
border-style: dashed;
}
.edge {
border-style: ridge;
border-width: 3px;
}
summary:hover {
color: lime;
background: #000;
cursor: pointer;
}
summary + u {
display: inline-block;
text-decoration: none;
white-space: pre-line;
}
code {
color: green;
background: rgba(0, 0, 0, 0.2);
white-space: pre;
}
summary + code {
display: block;
}
a {
display: block;
text-decoration: none;
text-align: center;
}
a:link,
a:visited {
color: cyan;
background: #000;
}
a:hover,
a:active {
color: blue;
background: none;
}
a::before {
content: 'View .'attr(data-tag);
}
a.off::before {
content: 'Hide .'attr(data-tag);
}
a::after {
content: ' HTML';
}
.hide {
display: none;
}
.txt {
color: blue;
background: rgba(0, 0, 0, 0.2);
white-space: pre;
}
<main>
<hr class='edge'>
<details><summary>Header (OP)</summary>
<u>Selector fails -- :not() is prefixed incorrectly
.header:... means .header is targeted 👎
.header<code>␣</code>:... means the descendants of .header is targeted 👍
There is no .header.A, .header.B, nor .header.A.B so
.header <em>without</em> .A and/or .B will have everything in italics</u></details>
<details><summary>CSS</summary>
<code>.header:not(.A):not(.B) {
font-style: italic;
}</code>
<a href='#/' data-tag='header'></a>
</details>
<hr>
<div class='header'>
<span class="A">LatestFooty.co.uk</span> is currently available for sale, along with:
<div class="B">
LatestFootie.com<br> LatestFootie.co.uk
</div>
</div>
<hr class='edge'>
<details><summary>Header 1</summary>
<u>Best solution with no extra HTML tags:
Assign <code>font-style: normal</code>...
directly (.C1, .D1)
or by class (.N)</u></details>
<details><summary>CSS</summary>
<code>.header1 {
font-style: italic;
}
.C1,
.D1,
.N {
font-style: normal;
}</code>
<a href='#/' data-tag='header1'></a>
</details>
<hr>
<div class="header1">
<span class="C1">LatestFooty.co.uk</span> is currently available for sale, along with:
<div class="D1">
LatestFootie.com<br> LatestFootie.co.uk
</div>
</div>
<hr class='dash'>
<div class="header1">
<span class="C2 N">LatestFooty.co.uk</span> is currently available for sale, along with:
<div class="D2 N">
LatestFootie.com<br> LatestFootie.co.uk
</div>
</div>
<hr class='edge'>
<details><summary>Header 2</summary>
<u>Using :not() needs extra HTML tag:
Wrap second textnode in an inline or inline-block tag
As content of a descendant tag, the text can be targeted</u></details>
<details><summary>CSS</summary>
<code>.header2 *:not(.E):not(.F) {
font-style: italic;
}</code>
<a href='#/' data-tag='header2'></a>
</details>
<hr>
<div class='header2'>
<span class="E">LatestFooty.co.uk</span> <span>is currently available for sale, along with:</span>
<div class="F">
LatestFootie.com<br> LatestFootie.co.uk
</div>
</div>
<hr class='edge'>
<details><summary>Header 3</summary>
<u>Smart solution with extra HTML tag:
Wrap second textnode in <code><i></code> or <code><em></code></u></details>
<details><summary>CSS</summary>
<code>.header3 {
/* no styles needed */
}</code>
<a href='#/' data-tag='header3'></a>
</details>
<hr>
<div class='header3'>
<span class="G">LatestFooty.co.uk</span> <i>is currently available for sale, along with:</i>
<div class="H">
LatestFootie.com<br> LatestFootie.co.uk
</div>
</div>
<hr class='edge'>
<details><summary>Header 4</summary>
<u>Slickest solution with least HTML:
Wrap text that needs italics in <code><i></code> and then style lines with CSS</u></details>
<details><summary>CSS</summary>
<code>.header4 {
white-space: pre-line;
}</code>
<a href='#/' data-tag='header4'></a>
</details>
<hr>
<header class='header4'>LatestFooty.co.uk <i>is currently available for sale, along with:</i>
LatestFootie.com
LatestFootie.co.uk
</header>
</main>
Is there a way to avoid the "red highlight" in the last example?
Live Demo
<h3>should be:</h3>
<div class="demo">
<p>foo foo</p>
<p>bar bar</p>
</div>
<hr>
<div class="demo">
<p>foo foo<br>foo foo</p>
<p>bar bar</p>
</div>
<hr>
<div class="demo">
<p>foo foo<br>foo foo</p>
</div>
<h3>should not be:</h3>
<div class="demo">
<p>foo foo</p>
</div>
This is what I currently use:
.demo p:first-child::first-line {
color: red;
}
But, it highlights all examples, including the last one. I also tried these two:
.demo p:first-child::first-line:not(:only-child) {
color: red;
}
/* and... */
.demo p:first-child::first-line:not(:only-of-type) {
color: red;
}
But it seems it just brokes all the highlight in all demos.
Is there way to achieve the desired result? (Remove "red highlight" from the last example).
(JS/jQuery solution is also ok, but, if it could be solved with CSS, it would be much better).
Screenshot with desired result:
I have a solution in jQuery.
CSS:
.demo p:first-child:first-line {
color: red;
}
.demo .not-red:first-line {
color: green !important;
}
JS:
$('.demo p:only-child:not(:has(br))').each(function() {
$(this).addClass('not-red');
});
JS will add not-red class only to paragraphs that doesn't contain br tags and paragraphs that are only child.
CODEPEN
One way to achieve this is to set the color on :first-child and then override with :only-child:
.demo p:first-child::first-line {
color: red;
}
.demo p:only-child, .demo p:only-child::first-line {
color: inherit;
}
Example: https://jsfiddle.net/4s42cnrL/4/
please use
.demo:not(:last-of-type) p:first-child::first-line {
color: red;
}
https://jsbin.com/doqenagapa/4/edit?html,css,output
* {
color: yellow
}
.outermost {
color: blue
}
.middle {
color: red
}
<div class="outermost">
<div class="middle">
<p>Some Text</p>
</div>
</div>
I expect the color of <p>Some Text </p> to be red because <div class="middle"> is its parent. However, the color ends up as yellow. This seems counter-intuitive because the global selector is less specific than the parent container. Why does the p element inherit from global, and how can it be changed so that it inherits from the parent container?
As #j08691 notes in his comment, the universal selector * has no effect on specificity:
* {
color: yellow;
}
.middle {
color: red;
}
<div class="middle">
<p>Some Text</p>
</div>
If you'd like the specificity to operate under normal effects change the * to body:
body {
color: yellow;
}
.middle {
color: red;
}
<div class="middle">
<p>Some Text</p>
</div>
You're conflating inheritance with specificity. Those aren't the same thing; they're totally unrelated concepts in CSS.
It's true that the global selector has a much lower specificity than the .middle class selector, but that's irrelevant because the .middle selector isn't targeting your p element; it's targeting the p element's parent.
Normally that would be sufficient to make p use red text, because, by default, p has its color property set to the special value called inherit, which causes it to inherit its color from it's parent element. But p isn't using the default value (inherit) for its color property, because you have a matching rule telling explicitly to use yellow instead:
* {
color: yellow;
}
Inheritance doesn't even come into play here, because your p element isn't set to inherit from it's parent in the first place.
You can override that behavior using a selector with a higher specificity that targets the element containing your text (not just one of its ancestors) explicitly telling it to inherit from its parent:
* {
color: yellow;
}
.outermost {
color: blue;
}
.middle {
color: red;
}
.middle > p {
color: inherit; // This overrides the rule defined by the global selector above
}
<div class="outermost">
<div class="middle">
<p>Some Text</p>
</div>
</div>
Or alternately, you could just stop using the global selector and instead rely on inheritance to set the text color for most of your elements:
body {
color: yellow;
}
.outermost {
color: blue;
}
.middle {
color: red;
}
<div class="outermost">
<div class="middle">
<p>Some Text</p>
</div>
</div>
Note that using the global selector for this sort of thing is usually discouraged anyway, for numerous reasons.
To help you better understand why your current code isn't working, here's essentially what it's doing:
<div style="color:blue;"> <!-- Matches * and .outermost. Result: Blue -->
<div style="color:red;"> <!-- Matches * and .middle. Result: Red -->
<p style="color:yellow;">Some Text</p> <!-- Matches *. Result: yellow -->
</div>
</div>
This is how you would target the font inside the "middle" class https://jsfiddle.net/DIRTY_SMITH/cfckvvzw/3/
.middle > p {
color: red
}
I have a web app using AngularJS and Bootstrap. In a page I want to have two components with different styles. For example:
<div ng-include="'./component/header.html'"></div>
<div ng-include="'./component/header.html'"></div>
I want these 2 divs have different styles. My question is, 1) How can I encapsulate the style code into header.html? 2) How can I use less to define the style?
Thank you in advance!
For the first question:
Why not give the 2 divs a different class and then adopt the CSS like that:
<div class="first" ng-include="'./component/header.html'"></div>
<div class="second" ng-include="'./component/header.html'"></div>
CSS:
.first table {
/* style definitions */
}
.second table{
/* style definitions */
}
Like in this Snippet
You can add an id or class to each ng-include and style under that selector with css or less. It makes no difference what preprocessor or lack of, you use.
angular.module('app', []);
function HomeCtrl() {
}
* {
box-sizing: border-box;
}
.header {
padding: 20px;
border: 1px solid #000;
}
#header1 {
background: red;
}
#header2 {
background: blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app">
<div ng-include="'header.html'" id="header1" class="header"></div>
<div ng-include="'header.html'" id="header2" class="header"></div>
<script type="text/ng-template" id="header.html">
header
</script>
</div>