CSS Select the First Child from Elements with Particular Attribute

CSS select the first child from elements with particular attribute

The :first-child pseudo-class only looks at the first child node, so if the first child isn't an element[bla="3"], then nothing is selected.

There isn't a similar filter pseudo-class for attributes. An easy way around this is to select every one then exclude what comes after the first (this trick is explained here and here):

element[bla="3"] {
}

element[bla="3"] ~ element[bla="3"] {
/* Reverse the above rule */
}

This, of course, only works for applying styles; if you want to pick out that element for purposes other than styling (since your markup appears to be arbitrary XML rather than HTML), you'll have to use something else like document.querySelector():

var firstChildWithAttr = document.querySelector('element[bla="3"]');

Or an XPath expression:

//element[@bla='3'][1]

CSS select element with particular child element attribute

I tried your complex selector in CSS, plain JS and using the jQuery lib... Guess who wins!

// Checking if JS handles that selector....

// Let's have the selector in a variable, just to make sure the same is tried in both cases...
let ourSelector = ".wp-block-image figure.alignright:has(img:not([src*='triangle']))"

// JS querySelector
try{
document.querySelector(ourSelector).style.border = "3px solid blue";
}
catch(error){
console.log(error.message);
}

// jQuery! (--WORKS--)
$(ourSelector).css("border", "3px solid blue");
/* This rule applies */
.wp-block-image figure.alignright img:not([src*="triangle"]) {
border: 3px solid red;
}


/* This one not */
.wp-block-image figure.alignright:has(img:not([src*="triangle"])) {
opacity: 0.2;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="wp-block-image">
<figure class="alignright">
<img width="387" height="500" src="https://via.placeholder.com/300x300.jpg?text=triangle" />
</figure>
<figure class="alignright">
<img width="387" height="500" src="https://via.placeholder.com/300x300?text=square" />
</figure>
</div>

Select first element with data attribute

Unfortunately CSS does not have a filter selector, something like p[data-result="INVALID"]:first or p[data-result="INVALID"]:first-with-attribute. You can mimic the behaviour that you want by first setting all the corresponding items to a red color, and then make all items that are the next sibling of an identical item reverse back to black.

I would also like to point you to two issues that I have with your code: using uppercase class names, IDs, attributes, and what-have-you is confusing. Either use all lower-case, or all upper-case. Some people (especially back-end developers) like to use camel-case, but I don't like it - that is personal though. But for uniformity and manageability it is recommended to stick with one convention, and don't start mixing things up. Secondly, the b tag might not be the one you want. It used to be a very convenient tag, but is since been surpassed by the strong tag in many respects. See the following links for details:

  • MDN
  • W3
  • HTML5 Doctor

p[data-result="INVALID"] {  color: red}
p[data-result="INVALID"] ~ p[data-result="INVALID"] { color: black;}
<p data-result="VALID"><b>Note:</b> a:active MUST come after a:hover in the CSS definition in order to be effective.</p><p data-result="INVALID"><b>Note:</b> a:hover MUST come after a:link and a:visited in the CSS definition in order to be effective.</p><p data-result="INVALID"><b>Note:</b> a:hover MUST come after a:link and a:visited in the CSS definition in order to be effective.</p>

Target first-child with css data attribute

Here you go man. Just the wrong css selector.

https://codepen.io/jackgisel/pen/dyPxrOG

[data-item="8"] p:first-of-type {
...
}

CSS selector for first element with class

This is one of the most well-known examples of authors misunderstanding how :first-child works. Introduced in CSS2, the :first-child pseudo-class represents the very first child of its parent. That's it. There's a very common misconception that it picks up whichever child element is the first to match the conditions specified by the rest of the compound selector. Due to the way selectors work (see here for an explanation), that is simply not true.

Selectors level 3 introduces a :first-of-type pseudo-class, which represents the first element among siblings of its element type. This answer explains, with illustrations, the difference between :first-child and :first-of-type. However, as with :first-child, it does not look at any other conditions or attributes. In HTML, the element type is represented by the tag name. In the question, that type is p.

Unfortunately, there is no similar :first-of-class pseudo-class for matching the first child element of a given class. At the time this answer was first posted, the newly published FPWD of Selectors level 4 introduced an :nth-match() pseudo-class, designed around existing selector mechanics as I mentioned in the first paragraph by adding a selector-list argument, through which you can supply the rest of the compound selector to get the desired filtering behavior. In recent years this functionality was subsumed into :nth-child() itself, with the selector list appearing as an optional second argument, to simplify things as well as averting the false impression that :nth-match() matched across the entire document (see the final note below).

While we await cross-browser support (seriously, it's been nearly 10 years, and there has only been a single implementation for the last 5 of those years), one workaround that Lea Verou and I developed independently (she did it first!) is to first apply your desired styles to all your elements with that class:

/* 
* Select all .red children of .home, including the first one,
* and give them a border.
*/
.home > .red {
border: 1px solid red;
}

... then "undo" the styles for elements with the class that come after the first one, using the general sibling combinator ~ in an overriding rule:

/* 
* Select all but the first .red child of .home,
* and remove the border from the previous rule.
*/
.home > .red ~ .red {
border: none;
}

Now only the first element with class="red" will have a border.

Here's an illustration of how the rules are applied:

.home > .red {
border: 1px solid red;
}

.home > .red ~ .red {
border: none;
}
<div class="home">
<span>blah</span> <!-- [1] -->
<p class="red">first</p> <!-- [2] -->
<p class="red">second</p> <!-- [3] -->
<p class="red">third</p> <!-- [3] -->
<p class="red">fourth</p> <!-- [3] -->
</div>

CSS select first child if it's a certain tag

figure:first-child will select all the figures that are first child of a parent.

Check this example at W3C.

Using CSS selectors to get first child from a nested element

Well, the anchor element you're referring to is the only child of the h3.r parent.

So :first-child, :last-child and :only-child would all apply.

Sample Image

A simple h3.r > a (child selector) or h3.r a (descendant selector) should suffice, assuming it's unique in the document.

Your selector – h3.r a:first-child – should, technically speaking, work as well.

Based on the image above, an attribute selector may also work:

h3.r a[data-href="https://mail.google.com/"]

More information: https://www.w3.org/TR/css3-selectors/#selectors

How to style for first-child of a certain type of element?

Use first-of-type instead of first-child

.page a:first-of-type{
color:red;
}

The :first-of-type CSS pseudo-class represents the first sibling of its type in the list of children of its parent element.

Taken from MDN Documentation. You can find more details & examples here.

Explanation : :first-child not working as expected

Selecting child elements with css attribute value selector

This works: https://jsfiddle.net/09zkqatx/

Basically, I've done all the same way you described.

div div {
width: 50px;
height: 50px;
border: 1px solid black;
}

div[class*="recipes"] .header{
margin-left: 40px;
}


Related Topics



Leave a reply



Submit