Selecting the correct first child [duplicate] - html

This question already has answers here:
CSS selector for first element with class
(23 answers)
Can I combine :nth-child() or :nth-of-type() with an arbitrary selector?
(8 answers)
Closed 5 years ago.
I've been trying to get my css to select the 'correct' first div but can't seem to isolate it. Here's my (stripped back, but correct) html:
<div id="page_container">
<div></div> // this div has content, but no id or class
<div class="holder"></div> // this div has content
<div class="section"></div> // <<<< this is the div I want to select; the first div with the class 'section'
<div class="section"></div> // I don't want to select other 'section's
<div class="section"></div>
<div class="section"></div>
</div>
CSS-wise, so far I have tried...
#page_container > div > .section:first-child {
...rules here
}
#page_container > div:first-child(.section) { // is this even valid?
...rules here
}
#page_container > div > div > .section:first-child {
...rules here
}
...without luck.
I may be experiencing a brain-fart, but how would I select just the first div with a class of 'section'?
Any help, much appreciated!

Try this.
#page_container .section{
background: green;
margin: 10px 0;
}
#page_container .section ~ .section {
background: transparent;
}
<div id="page_container">
<div>this div has content, but no id or class</div>
<div class="holder">this div has content</div>
<div class="section">this is the div I want to select; the first div with the class 'section'</div>
<div class="section">I don't want to select other 'section's</div>
<div class="section"></div>
<div class="section"></div>
</div>

You could use + Combinator.
Adjacent sibling combinator
The + combinator selects adjacent siblings. This means that the second element directly follows the first, and both share the same parent.
Syntax: A + B
Example: h2 + p will match all <p> elements that directly follow an <h2>.
Reference: https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors
Example:
#page_container > .holder + .section {
background: red;
}
<div id="page_container">
<div>this div has content, but no id or class</div>
<div class="holder">this div has content</div>
<div class="section">this is the div I want to select; the first div with the class 'section'</div>
<div class="section">I don't want to select other 'section's</div>
<div class="section"></div>
<div class="section"></div>
</div>

Related

How can I target a child class only if the first child has a specific class?

How can I target the ::before in the class .child-ccc only if the first child has the class .hovered?
Here is the HTML output:
<div class="parent">
<div class="child hovered">
<div class="child-aaa"></div>
<div class="child-aaa"></div>
</div>
<div class="child"></div>
<div class="child-c">
<div class="child-ccc">
::before
</div>
</div>
</div>
I have tried .child:first-child.hovered + .child-c .child-ccc:before
The + selector only selects adjacent siblings but child-c isn't. So, you have to use ~.
.parent .child:first-child.hovered~.child-c .child-ccc::before {
content: '';
display: block;
height: 1rem;
background-color: purple;
}
<div class="parent">
<div class="child hovered">
<div class="child-aaa">aaa</div>
<div class="child-aaa">aaa</div>
</div>
<div class="child">bbb</div>
<div class="child-c">
<div class="child-ccc">
ccc
</div>
</div>
</div>
Yes, You can achieve this by using advance CSS selectors. Try this code .child.hovered ~ .child-c > child-ccc::before
This symbol ~ means the sibling of .child.hovered class and > means direct child element.
Selector + is adjacent sibling combinator it means if we write img + p it selects Paragraphs that come immediately after any image and the selector ~ is General sibling combinator it means if we use img ~ p it selects Paragraphs that are siblings of and subsequent to any image.
so in your problem instead of + you should use ~ to achieve your goal like this:
.child.hovered ~ .child-c .child-ccc::before{
...
}

first of type with global class is not the same as if it is the local class [duplicate]

This question already has answers here:
Can I combine :nth-child() or :nth-of-type() with an arbitrary selector?
(8 answers)
Closed 3 years ago.
When we have this part of code:
<div *ngFor="element of elements1" class="element"></div>
<div *ngFor="element of elements2" class="element"></div>
And class Element:
.element {
color:red;
}
.element:first-of-type {
color:blue;
}
And we have two cases:
The first one is when the class element is global.
In this case just the first element of elements1 will be blue
The second one is when the class "element" is local.
In this case fist elements of both of the arrays will be blue
Why behavior in both cases isn't the same?
Try this:
<div
*ngFor="element of elements1; let first = first;"
class="element"
[ngStyle]="first && {'color': 'blue'}">
</div>
This will set the color only for the first div.
:first-of-type means "first of type" and not "first that matches the previous bit of the selector".
The class is irrelevent. The element type is div.
.element {
color: red;
}
.element:first-of-type {
color: blue;
}
<section>
<div class="element">This is the first div in the section</div>
<div class="element">
This is the second div in the section
<div class="element">This is the first div in the div</div>
<div class="element">This is the second div in the div</div>
</div>
<p class="element">This is the first p in the section</p>
<p class="element">This is the second p in the section</p>
</section>
first-of-type will get the element type using the class, so for example if the class is assigned on a p then the p should be the first of it's type to work so if there is any other p even from another class it will not detect it.
p:first-of-type {color:blue}
.myclass1:first-of-type {color:red}
.myclass2:first-of-type {color:green}
<div>
<div>This text should appear as normal</div>
<p>This text should be blue.</p>
<p class="myclass1">This text should appear red.</p>
<p class="myclass2">This text should appear green.</p>
</div>
check this also:
CSS3 selector :first-of-type with class name?

How to select only the last <p> in a <div>?

I have div1 in this I have more <div>s and <p>s and I only want to select the <p> in this <div> and not in the other divs
I tried the CSS :last-child selector, but it selected also the last <p>s in the other div.
How can I do that?
This is a sample of my code:
<div id="div1">
<div class="div2"><p>text</p></div>
<div class="div3"><p>text</p></div>
<div class="div4"><p>text</p></div>
<p>select only me</p>
</div>
You want to use the immediate children (>) selector along with the :last-child pseudo-class. This selects only p elements that are "first-level" (not nested) children of #div1 and then selects only the last of those elements. See below:
#div1 > p:last-child {
color: red;
}
<div id="div1">
<div class="div2"><p>text</p></div>
<div class="div3"><p>text</p></div>
<div class="div4"><p>text</p></div>
<p>select only me</p>
</div>
You can do:
#div1 p {
}
Which wil select all the paragraph tags that are direct children of the div1
You might be running into a general "any div's last p child" situation instead of specifying with a class.
Make a unique class for the outer, containing div to then use last-child styling on.
Try this
#div1 p:last-child {
}
If you want to select the last div, and then any p inside of that div:
div:last-of-type p {background:green;}
If you want to select the last p in any div:
div p:last-of-type {background:red;}
If you want to select the last p in last div:
div:last-of-type p:last-of-type {background:blue;}
Just give your p a class or id like <p class="blablabla">blablabla</p> or <p id="blablabla">blablabla</p> and in CSS you do:
.blablabla{
#things you want to do in CSS with class
}
or
#blablabla{
#things you want to do in CSS with id
}

How to apply CSS to first child div element only? [duplicate]

This question already has answers here:
How to apply CSS to only immediate children of a certain class
(3 answers)
Closed 3 years ago.
<div class = "node-master">
<div class = "node-1">
<div class ="node-content">
child content 1
</div>
</div>
<div class = "node-2">
<div class ="node-content">
child content 2
</div>
</div>
In example above, I need to target "child content 1" CSS class to apply a different font color. However, the caveat is I cannot target node-1 directly because of how the HTML is being generated (dynalist.io app).
The setting dictating "child content 1" CSS syntax is from node-master.
Basically, what I need to do is have a CSS class that uses node-master and applies its properties to the first child <div>
You can play with :first-child CSS selector, for example if you want to target the first div child of .node-master:
.node-master > div:first-child {
color: red;
}
You can use the :first-child pseudo selector on the parent:
.node-master div:first-child div.node-content {
color: red;
}
.node-content {
color: blue;
}
<div class="node-master">
<div class="node-1">
<div class="node-content">
child content 1
</div>
</div>
<div class="node-2">
<div class="node-content">
child content 2
</div>
</div>
this is the css to target elements without classes
.node-master > div:nth-child(1){background-color:red;}
.node-master > div:nth-child(2){background-color:blue;}
...
.node-master > div:nth-child(n){background-color:red;}
Hope this may help
Thanks.
You need to have each of the node-contents be unique for the css to appear
<div class = "node-master">
<div class = "node-1">
<div class ="node-content1">
child content 1
</div>
</div>
<div class = "node-2">
<div class ="node-content2">
child content 2
</div>
</div>
And then simply set the color property.
.node-content1 {
color: #1e00ef
}
.node-content2 {
color: #f44336
}
Here you go: https://jsfiddle.net/fvw3xhv8/

last child selector in div

I have a parent div and 3 divs inside it. I want to hide the last child div. I tried using last-child CSS selector, but it is not working.
The order of divs:
<div class="wysibb-toolbar">
<div class="wysibb-toolbar-container"></div>
<div class="wysibb-toolbar-container"></div>
<div class="wysibb-toolbar-container">//(This is to be made display:none)
<div class="wysibb-toolbar-btn wbb-code" jQuery110208619481903000815="71"></div>
<div class="wysibb-toolbar-btn wbb-code" jQuery110208619481903000815="71"></div>
</div>
</div>
I have tried this:
div.wysibb-toolbar div:last-child {
display:none;
}
Try this
div.wysibb-toolbar>div:last-child {
display:none;
}
or just
div.wysibb-toolbar-container:last-child{
display:none;
}
or More generic
div.wysibb-toolbar>div.wysibb-toolbar-container:last-child{
display:none;
}
Your problem is that you have mis-typed the word display and have spelt it disply
Try this out
div.wysibb-toolbar div:last-child {
display: none;
}
<div class="wysibb-toolbar">
<div class="wysibb-toolbar-container">Test 1</div>
<div class="wysibb-toolbar-container">Test 2</div>
<div class="wysibb-toolbar-container">Test 3 (should be hidden)</div>
</div>
:last-child selector matches every element that is the last child of its parent.Try this JSFIddle
.wysibb-toolbar-container:last-child {
display:none;
}