How to apply boolean operators not, and [duplicate] - javascript

This question already has answers here:
Can the :not() pseudo-class have multiple arguments?
(5 answers)
Closed 6 years ago.
codepen
div:not(.wayne) div:not(.garth) .content {
background-color:red;
}
<div class="wayne">
<div class="content">Party On Garth</div>
</div>
<div class="garth">
<div class="content">Party On Wayne</div>
</div>
<div class="Saitama">
<div class="content">One punch.</div>
</div>
<div class="Naruto">
<div class="content">Dattebayo</div>
</div>
The background should be red unless the class is either .garth or .wayne. How can I do this for all .content?
Saitama and Naruto should have red backgrounds. Right now nothing has a red background. Any similarly added new characters should also have a red background.
Pseudocode
if(!(wayne || garth)){ apply red background to .content}
I'm willing to use JavaScript if necessary. I prefer css.

Chain the :not together
div:not(.wayne):not(.garth) .content {
background-color:red;
}
<div class="wayne">
<div class="content">Party On Garth</div>
</div>
<div class="garth">
<div class="content">Party On Wayne</div>
</div>
<div class="Saitama">
<div class="content">One punch.</div>
</div>
<div class="Naruto">
<div class="content">Dattebayo</div>
</div>

If you want to have multiple :not selectors, you need to change your CSS to look like this
CSS
div:not(.wayne):not(.garth) .content {
background-color:red;
}
CodePen

You are using a descendant combinator: . Your selector means:
"Select each element with class content which is a descendant of any div element without garth class which is a descendant of any div element without wayne class."
Remove it.
div:not(.wayne):not(.garth) > .content {
background-color:red;
}
"Select each element with class content which is a child of a div element without garth nor wayne classes."

Related

I don't get the difference between adding tippy(".class .class") and tippy(".class, .class) and also what is the function of [0] in the code [duplicate]

Here is an example that I do not understand:
.container_12 .grid_6,
.container_16 .grid_8 {
width: 460px;
}
It seems to me that width: 460px is applied to all above mentioned classes. But why some classes are separated by a comma (,), and some just by a space?
I assume that width: 460px will be applied only to those elements which combine classes in the way mentioned in the CSS file. For example, it will be applied to <div class='container_12 grid_6'> but it will not be applied to the <div class='container_12'>. Is this assumption correct?
.container_12 .grid_6,
.container_16 .grid_8 {
width: 460px;
}
That says "make all .grid_6's within .container_12's and all .grid_8's within .container_16's 460 pixels wide." So both of the following will render the same:
<div class="container_12">
<div class="grid_6">460px Wide</div>
</div>
<div class="container_16">
<div class="grid_8">460px Wide</div>
</div>
As for the commas, it's applying one rule to multiple classes, like this.
.blueCheese, .blueBike {
color:blue;
}
It's functionally equivalent to:
.blueCheese { color:blue }
.blueBike { color:blue }
But cuts down on verbosity.
.container_12 .grid_6 { ... }
This rule matches a DOM node with class container_12 that has a descendant (not necessarily a child) with class grid_6, and applies the CSS rules to the DOM element with class grid_6.
.container_12 > .grid_6 { ... }
Putting > between them says that the grid_6 node must be a direct child of the node with class container_12.
.container_12, .grid_6 { ... }
A comma, as others have stated, is a way to apply rules to many different nodes at one time. In this case, the rules apply to any node with either a class of container_12 or grid_6.
Not exactly what was asked, but maybe this will help.
To apply a style to an element only if it has both classes your selector should use no space between the class names.
For Example:
.class1.class2 { color: #f00; }
.class1 .class2 { color: #0f0; }
.class1, .class2 { font-weight: bold; }
<div class='class1 class2'>Bold Red Text</div>
<div class='class1'>Bold Text (not red)</div>
<div class='class1'><div class='class2'>Bold Green Text</div></div>
Comma groups the classes (applies the same style to them all), an empty space tells that the following selector must be within the first selector.
Therefore
.container_12 .grid_6,
.container_16 .grid_8 {
width: 460px;
}
applies that style to only class .grid_6 which is within .container_12 class and to .grid_8 class which is within .container_16.
The width: 460px; will be applied to the element with the .grid_8 class, contained inside the elements with .container_16 class, and elements with the .grid_6 class, contained inside the elements with .container_12.
The space means heritage, and the comma means 'and'. If you put properties with a selector like
.class-a, .class-b, you will have the properties applied to the elements with anyone of the two classes.
Hope I have helped.
You have four classes and two selectors in your example:
.container_12 .grid_6,
.container_16 .grid_8 {
width: 460px;
}
So .container_12 and .grid_6 are both classes, but the rule width: 460px will only be applied to elements that have the .grid_6 class which are descendants of an element that have the .container_16 class.
For example:
<div class="container_16">
<p class=".grid_6">This has a width of 480px.</p>
<p>This has an unknown width.&lt/p>
</div>
The above means that you are applying styles to two classes, indicated by the comma.
When you see two elements side by side not separated you can assume that it is referring to an area within an area. So in the above, this style only applies to grid_6 classes inside of container_12 classes and grid_8 classes inside of container_16 classes.
in the example:
<div class="grid_6">This is not effected</div>
<div class="container_12">
<div class="grid_6">
This is effected.
</div>
</div>
The first grid_6 will not be effected while the second grid_6 class will because it is contained inside a container_12.
A statement like
#admin .description p { font-weight:bold; }
Would only apply the bold to tags within areas that have class "description" that are inside of an area with id "admin", such as:
<div id="admin">
<div class="description">
<p>This is bold</p>
</div>
</div>
Selectors combinations get different meanings - attached image explains easily
a) Multiple selectors separated by a comma(,) - Same styles are applied to all selected elements.
div,.elmnt-color {
border: 1px solid red;
}
Here border style is applied to DIV elements and CSS class .elmnt-color applied elements.
<!-- comma example -->
<div>
Red border applied
</div>
<p class="elmnt-color">
Red border applied
</p>
b) Multiple selectors separated by space – Those are called descendant selectors.
div .elmnt-color {
background-color: red;
}
Here border style is applied to CSS class .elmnt-color applied elements which are child elements of a DIV element.
<!-- space example -->
<div>
Red border NOT applied
</div>
<p class="elmnt-color">
Red border NOT applied
</p>
<div>
Red border NOT applied
<p class="elmnt-color">
Red border applied
</p>
</div>
c) Multiple selectors specified without space - Here styles are applied to elements which meet all the combinations.
div.elmnt-color {
border: 1px solid red;
}
Here border style is applied only to DIV elements with a CSS class of .elmnt-color.
<!-- no space example -->
<div>
Red border NOT applied
</div>
<p class="elmnt-color">
Red border NOT applied
</p>
<div>
Red border NOT applied
<p class="elmnt-color">
Red border NOT applied
</p>
</div>
<div class="elmnt-color">
Red border applied
</div>
Details are attached at https://www.csssolid.com/css-tips.html
Note: CSS Class is just one of the CSS Selectors. These rules applies to all CSS Selectors (ex: Class, Element, ID etc.,).
.container_12 .grid_6,
.container_16 .grid_8 {
width: 460px;
}
width:460px will be only applied to .grid_6 and .grid_8
Edit: Here is a very good article for you
http://css-tricks.com/multiple-class-id-selectors/

how to hide/show element according to url parameter

i have 4 url parameters like so
http://127.0.0.1:4000/post?&id=5f04698e6114e4069099d8bf#like
http://127.0.0.1:4000/post?&id=5f04698e6114e4069099d8bf#comment
http://127.0.0.1:4000/post?&id=5f04698e6114e4069099d8bf#share
http://127.0.0.1:4000/post?&id=5f04698e6114e4069099d8bf#save
and 4 divs like
html
<div id='like' class='hide'></div>
<div id='comment' class='hide'></div>
<div id='share' class='hide'></div>
<div id='save' class='hide'></div>
and
css
.hide{
display:none;
}
how do i unhide elements when a url param is searched,
for example if i search for
http://127.0.0.1:4000/post?&id=5f04698e6114e4069099d8bf#like
the div with id='like should be now visible
what i have tried
i have tried to unhide the elements on button click, and I am successful using classList.toggle('hide')
but how do I achieve the same thing with changes in url. I would be best if the classList is set acc to the url for example when there is #like in url ,the element with id like should not contain the class hide anymore. other answers are also accepted ,thanks.
No JavaScript needed. Use the CSS :target selector like:
#like.hide:target {display:block;}
.hide {
display: none;
}
#like.hide:target,
#comment.hide:target,
#share.hide:target,
#save.hide:target {
display: block;
}
<div id='like' class='hide'>like</div>
<div id='comment' class='hide'>comment</div>
<div id='share' class='hide'>share</div>
<div id='save' class='hide'>save</div>
like
comment
share
save
A JavaScript approach would be as follow:
like.style.display = "none";
You could implement if statement to trigger that line if this is what you prefer.

How to use JQuery to detect Mouseover and add animation on dynamic item

I have a series of div's that get loaded dynamically. There ID is set when the page loads, and they all have the same class of "pp-post". When the user hovers over an item with class="pp-post" the 'p' items within that become visible.
I want to add a different animation for each of these 'p' tags when they become visible.
I have minimal experience with JQuery so I am wondering how I can detect which "pp-post" item is hovered and apply the animations to the 'p' tags.
As for the animations not sure yet what to use but it could be JQuery animations or maybe use animations.css and add a class to the p tags when visible.
HTML:
<div id="{post_id}" class="pp-post">
<div id="{post_id}" class="pp-post-item">
<p id="{post_id}" class="pp-post-title"></p>
<p id="{post_id}" class="pp-arrow-down"></p>
</div>
</div>
CSS:
.pp-post-title {
visibility: hidden;
}
.pp-arrow-down {
visibility: hidden;
}
.pp-post-item:hover > p {
visibility: visible;
}
how I can detect which "pp-post" item is hovered and apply the animations to the 'p' tags
You can use $(this) to get the hovered pp-post.
The code will look like
$('.pp-post').hover(function(){
$(this).animate({
//animation code
});
});
To make p tag visible,
$('pp-post').hover(function(){
$(this).find('p').animate({
//animation code
});
});
Instead of animation function, you can also use certain specific functions like fadeIn
Sample snippet
$(document).ready(function() {
$('.pp-post').hover(function() {
$(this).find('p').fadeIn(2000);
});
});
.pp-post p {
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="pp-post">Hover
<p>Sample1</p>
</div>
<div class="pp-post">Hover
<p>Sample1</p>
</div>
<div class="pp-post">Hover
<p>Sample1</p>
</div>
<div class="pp-post">Hover
<p>Sample1</p>
</div>
<div class="pp-post">Hover
<p>Sample1</p>
</div>

Target specific set of CSS classes

I have two instances of elements in my document. One is with just a class of slide and the other with a class of slide and slide--current. Below is an example of this markup.
<div class="slide slide--current">
<h2 class="title title--centered title--modifier">Slide 1</h2>
<div class="text-block">
<p>text</p>
</div>
</div>
<div class="slide">
<h2 class="title title--centered title--modifier">Slide 2</h2>
<div class="text-block">
<p>text</p>
</div>
</div>
If I want to target text-block when it has slide--current as the parent I would use .slide--current > .text-block as my selector. But what if I want to target text-block when it doesn't have slide--current? I'm asking because both elements need to have the class slide applied to them at all times. If I use .slide .text-block styles will be applied to slide--current when they shouldn't be. Is this a case foe the :not() selector?
You can use a pseudo-selector:
.slide:not(.slide--current) {
// styling here
}
In your case, as long as .text-block is always a direct child of .slide:
.slide > .text-block {
// Apply to all text blocks regardless of state of slide
}
.slide:not(.slide--current) > .text-block {
// Only applied if parent slide is not current
}
You could probably achieve the same result by reading the answer by #Rounin and avoid pseudo-selectors by reversing your thought process and setting some base styles for all text-blocks first then overriding them when the slide is current.
But what if I want to target text-block when it doesn't have
slide--current?
This is a lesson in how to use the cascade in CSS in the right order.
.text-block {
BASIC .text-block STYLES
}
.slide .text-block {
ADDITIONAL .slide .text-block STYLES
}
.slide--current .text-block {
ADDITIONAL .slide--current .text-block STYLES
}
Basically, you start with the core styles and you add, cumulatively, as the context grows increasingly specific.

Event listener for DIV and change content [duplicate]

This question already has answers here:
What do querySelectorAll and getElementsBy* methods return?
(12 answers)
Closed 6 years ago.
i got blocks like these:
<div class="listener" id="123">
<h1>Title</h1>Some text...
<div class="container"></div>
</div>
<br>
<div class="listener" id="456">
<h1>Title</h1>Some text...
<div class="container"></div>
</div>
CSS:
.listener{width:500px;background:red;border:1px solid black}
JavaScript:
document.getElementByClassName("listener").addEventListener("click", function()
{
// get the id of the DIV
// put content inside the class=container inside the DIV with innerHTML
});
What should happen?
After clicking in one of those boxes:
call the function
get the ID of the clicked DIV
put some content inside the DIV (DIV with class=container)
Please NO solution with <a> or jQuery.
Any idea? Thanks!
I have a Fiddle here: https://jsfiddle.net/wwp9jk8t/
document.getElementByClassName("listener") will return nodelist not a DOM element and you can not bind click event to over array-like NodeList. Use [].forEach.call/for-loop to iterate all the elements in nodeList and apply adeventListener to every element.
Also note typo getElementByClassName, it should be getElementsByClassName(plural)
Try this:
[].forEach.call(document.getElementsByClassName("listener"), function(elem) {
elem.addEventListener("click", function() {
alert(this.id);//to get the id attribute of the clicked element..
this.getElementsByClassName("container")[0].innerHTML = "Hello World";
});
})
.listener {
width: 500px;
background: red;
border: 1px solid black
}
<div class="listener" id="123">
<h1>Title</h1>Some text...
<div class="container"></div>
</div>
<br>
<div class="listener" id="456">
<h1>Title</h1>Some text...
<div class="container"></div>
</div>

Categories