CSS Selectors - difference between and when to use ">", "+" or " " [duplicate] - css

This question already has answers here:
CSS '>' selector; what is it? [duplicate]
(7 answers)
What does the "+" (plus sign) CSS selector mean?
(9 answers)
Closed 5 years ago.
When using CSS, I can query elements in the following ways:
div > .class
div .class
div + .class
However I can't quite tell the exact difference between each of these DOM queries. Do they all point to child elements? I know that ">" and " " (space) do.
But under what circumstances would I use each?

In CSS these are called Combinators and means three different things:
div > .class: is called Child selector and will select all elements that are direct children of a div and have the class .class.
div .class: is called Descendant selectors and will select all elements inside a div and having the class .class.
div + .class: is called Adjacent sibling selector and will match any element that immediately follows a div and have the class .class.
Example:
In the following example:
<div>
<p class="test">
<a href="#" class="test">
Testing link</a>
<img class="test"/>
</p>
<span class="test">A span</span>
</div>
<h4 class="test">A title</h4>
div > .test will match only <p> and <span> elements.
div .test will match <p>, <a>, <img> and <span> elements.
div + .test will match only <h4> element because it follows the <div> immediately.
Demo:
div .test {
background: yellow;
}
div>.test {
background: red;
}
div+.test {
background: green;
}
<div>
<p class="test">
Pragraph
<a href="#" class="test">
link</a>
<img class="test" width="50px" height="50px" />
</p>
<span class="test">Span</span>
</div>
<h4 class="test">Title</h4>

Related

Applying style on children does not work in Tailwind

Let's say I have this markup:
<div class="">
<p>First text</p>
<p>
<span>Hi</span>
<span class="bold">Bye</span>
</p>
</div>
If I use text-red-400 on the parent element, I see that all of the children become red.
However, when I use [&>.bold]:text-red-400, the span element that has bold class won't be changed.
What is wrong here?
Because > CSS selector is direct children only selector
The child combinator (>) is placed between two CSS selectors. It matches only those elements matched by the second selector that are the direct children of elements matched by the first.
Utility [&>.bold]:text-red-400 basically is equivalent of
.utility > .bold {
color: red;
}
Every direct child with .bold class will be red
<div class="[&>.bold]:text-red-400">
<p class="bold">I'm red</p>
<p>
<span>Hi</span>
<span class="bold">I'm not</span>
</p>
</div>
If you wish to target every .bold element just remove > selector and replace it with underscore _
<div class="[&_.bold]:text-red-400">
<p class="bold">I'm red</p>
<p>
<span>Hi</span>
<span class="bold">Me too</span>
</p>
</div>
DEMO
So if you want to change the text color for only the span element with bold class then you need to specify the position of the child element as well. You can try the following code:
[&>*:nth-child(2)]:text-red-500.
This is mentioned in the tailwind documentation a well for more help: tailwind doc

Select outer parent frame from child in css [duplicate]

This question already has answers here:
Is there a CSS parent selector?
(33 answers)
Closed last month.
Good day all,
I have an a tag with class "WORKSHEET_block" and which is contained in 3 other div.
The css to style is (which does not work):
.WORKSHEET_block < .fc-daygrid-event-harness < .fc-daygrid-day-events < .fc-daygrid-day-frame {
background-color: green !important;
}
<div class="fc-daygrid-day-frame">
<div class="fc-daygrid-day-events">
<div class="fc-daygrid-event-harness">
<a class="WORKSHEET_block">My Value</a>
</div>
</div>
</div>
I know if it was the other way around from parent to child we would use ">" from the parent to the child.
Is there anywhere I can select the parent from the child?
.fc-daygrid-day-frame:has(.WORKSHEET_block) {
background-color: green;
}
<div class="fc-daygrid-day-frame">
<div class="fc-daygrid-day-events">
<div class="fc-daygrid-event-harness">
<a class="WORKSHEET_block">This is the child using the class</a>
</div>
</div>
</div>
<div class="fc-daygrid-day-frame">
<div class="fc-daygrid-day-events">
<div class="fc-daygrid-event-harness">
<a>this child does not have any class</a>
</div>
</div>
</div>
Using :has relative selector. I have inserted an extra HTML code it doesn't have a WORKSHEET_block class, so the style is not applying to it

What is the benefit of using the '>' selector in CSS? [duplicate]

This question already has answers here:
What does the ">" (greater-than sign) CSS selector mean?
(8 answers)
Closed 8 years ago.
These two Selectors:
.class>li>a{}
and
.class li a{}
are doing exactly same job for me so can some one please tell me what is the benefit of using > ?
Thanks
It makes the selector more specific.
The first selector only targets an anchor tag that is a child tag of a lst item that is a child tag of a CLASS.
<div class="fo">
<li>
<a>
the second select will target all anchor tags that are a descendant of a list item which is a descendant of a specific class.
<div class="fo">
.....
<li>
.....
<a>
where .... can be any other dom element
With the selector > you focus on the immediate chidlrens
<div class="class">
<ul>
<li>text</li>
</ul>
</div>
With this part of code, the selector .class>li>a{} won't work cause the child of .class is "ul". But .class li a{} will work cause it check all in the selector tree.
<ul class="class">
<li>text</li>
</ul>
Will work with your .class>li>a{} cause they are all immediate children.
Another exemple, if you have this html code
<div id="section">
<span>some text</span>
<div class="subSection">
<span>some text</span>
</div>
</div>
The selector #section>span will apply to the first span only.
The selector #section span will apply to all spans in the id section.

:last-child works, :first-child doesn't [duplicate]

This question already has answers here:
CSS selector for first element with class
(23 answers)
Closed 8 years ago.
I have an aside with two <div class="sku"> elements. I'm trying to use CSS to manipulate the :first-child but it doesn't work. However, when trying to access the :last-child it does.
JSFiddle
HTML
<aside>
<h1>Product Name</h1>
<div class="sku">
<h3>
100 – Small
</h3>
<div class="dimension">
<ul>
<li>
<span class="title">
Product Dimensions
</span>
<span class="specs">
23.75w
x
17.75h
x
28d
</span>
</li>
</ul>
</div>
</div>
<div class="sku">
<h3>
200 – Large
</h3>
<div class="dimension">
<ul>
<li>
<span class="title">
Product Dimensions
</span>
<span class="specs">
29.75w
x
17.75h
x
28d
</span>
</li>
</ul>
</div>
</div>
</aside>
CSS
.sku:first-child {
display:none !important; /*doesn't hide the first child*/
}
.sku:last-child {
display:none !important; /*does hide the first child*/
}
Why won't :first-child select the first div?
You cannot use :first-child psuedo class since .sku is not the first child. A better option is to use either :first-of-type (for first child) or :nth-of-type (which can accept a number or an equation) pseudo classes:
.sku:nth-of-type(1) {
display: none;
}
Updated Demo
The :first-child means the first child. Which is in this case the H1. So this does not work. You can use:
h1 + .sku { }
But only if this is the order you place your HTML.

What's the difference between tag.class and tag .class? [duplicate]

This question already has answers here:
What does a space mean in a CSS selector? i.e. What is the difference between .classA.classB and .classA .classB? [duplicate]
(3 answers)
Closed 8 years ago.
What's the difference between
tag.class and tag .class
or
tag#id and tag #id?
tag.class matches all elements with name tag and with the class class, e.g.:
div.foo { color: red; } matches <div class="foo">
tag .class matches all .class elements that are descendants of tag elements (note the space between tag and .class):
(Remember that "descendants" includes all children, their children, and so on - whereas, in comparison, the > (child selector) only selects immediate children and not any grandchildren.)
div .foo { color: red; } matches the <span> in <div><p><span class="foo">
tag#id and tag #id are similar to the above, except matching on the id attribute instead of the class attribute[1]
div#foo { color: red; } matches <div id="foo">
div #foo { color: red; } matches the <span> in <div><p><span id="foo">
Remember that because id="" attributes are meant to be unique that additional selectors may be superfluous, e.g. div#bar could be simplified to just #bar (unless you're reusing the same CSS rules on different pages where #bar may have different element tag names).
[1]: This is true in HTML, in other SGML and XML languages using CSS the .class and #id selectors can be mapped to other attribute names.
tag.class would mean a tag with class='class'. Something like this:
<!-- This tag would be selected -->
<tag class="class">
...
</tag>
tag .class would mean an element whose class='class', and is a descendant of a <tag>. Something like this:
<tag>
...
<!-- This div would be selected -->
<div class="class">
</div>
...
</tag>
In general, something like selector1 selector2 would mean an element that matches selector2 and is a descendant of an element that matches selector1. Consider this example:
CSS:
/*
div.face matches a div with class="face"
div.eye#left matches a div with class="eye" and id="left"
Hence, this would select an element with class="eye" and id="left" which is
inside a div with class="face"
*/
div.face div.eye#left {
}
HTML:
<div class="face"> <!-- div.face -->
<div class="upper">
<div class="eye" id="left"></div> <!-- div.eye#left is selected -->
<div class="eye" id="right"></div>
</div>
</div>
Space () is a descendant selector, see documentation: http://www.w3.org/TR/CSS2/selector.html#descendant-selectors
Long story short tag.class applies to a tag element with class class, whereas tag .class applies to any elements with class class that are within tag element.

Resources