ChatGPT解决这个技术问题 Extra ChatGPT

CSS3 selector :first-of-type with class name?

Is it possible to use the CSS3 selector :first-of-type to select the first element with a given class name? I haven't been successful with my test so I'm thinking it's not?

The Code (http://jsfiddle.net/YWY4L/):

p:first-of-type {color:blue} p.myclass1:first-of-type {color:red} .myclass2:first-of-type {color:green}

This text should appear as normal

This text should be blue.

This text should appear red.

This text should appear green.

Does this answer your question? CSS selector for first element with class

C
Community

No, it's not possible using just one selector. The :first-of-type pseudo-class selects the first element of its type (div, p, etc). Using a class selector (or a type selector) with that pseudo-class means to select an element if it has the given class (or is of the given type) and is the first of its type among its siblings.

Unfortunately, CSS doesn't provide a :first-of-class selector that only chooses the first occurrence of a class. As a workaround, you can use something like this:

.myclass1 { color: red; }
.myclass1 ~ .myclass1 { color: /* default, or inherited from parent div */; }

Explanations and illustrations for the workaround are given here and here.


No @justNik this does work for multiple elements. The .myclass1 selector would select every element of .myclass1. The selector .myclass1 ~ .myclass1 uses the general sibling combinator to select every element with the class .myclass1 that is a following sibling of an element with a class of .myclass1. This is explained in amazing detail here.
B
Brian Campbell

The draft CSS Selectors Level 4 proposes to add an of <other-selector> grammar within the :nth-child selector. This would allow you to pick out the nth child matching a given other selector:

:nth-child(1 of p.myclass) 

Previous drafts used a new pseudo-class, :nth-match(), so you may see that syntax in some discussions of the feature:

:nth-match(1 of p.myclass)

This has now been implemented in WebKit, and is thus available in Safari, but that appears to be the only browser that supports it. There are tickets filed for implementing it Blink (Chrome), Gecko (Firefox), and a request to implement it in Edge, but no apparent progress on any of these.


Z
Zac

This it not possible to use the CSS3 selector :first-of-type to select the first element with a given class name.

However, if the targeted element has a previous element sibling, you can combine the negation CSS pseudo-class and the adjacent sibling selectors to match an element that doesn't immediately have a previous element with the same class name :

:not(.myclass1) + .myclass1

Full working code example:

p:first-of-type {color:blue} p:not(.myclass1) + .myclass1 { color: red } p:not(.myclass2) + .myclass2 { color: green }

This text should appear as normal

This text should be blue.

This text should appear red.

This text should appear green.


K
Konstantin

I found a solution for your reference. from some group divs select from group of two same class divs the first one

p[class*="myclass"]:not(:last-of-type) {color:red}
p[class*="myclass"]:last-of-type {color:green}

BTW, I don't know why :last-of-type works, but :first-of-type does not work.

My experiments on jsfiddle... https://jsfiddle.net/aspanoz/m1sg4496/


This does not work as required, as can already be seen from the fiddle. The only thing this does, is not select the last div if it does not have the class.
T
TDavison

This is an old thread, but I'm responding because it still appears high in the list of search results. Now that the future has arrived, you can use the :nth-child pseudo-selector.

p:nth-child(1) { color: blue; }
p.myclass1:nth-child(1) { color: red; }
p.myclass2:nth-child(1) { color: green; }

The :nth-child pseudo-selector is powerful - the parentheses accept formulas as well as numbers.

More here: https://developer.mozilla.org/en-US/docs/Web/CSS/:nth-child


Yeah, I don't think that works, at least not in Chrome... jsfiddle.net/YWY4L/91
What do you mean with "now that the future has arrived"? This only works in Safari at the time of writing.
Future still a ways away from arriving: caniuse.com/?search=nth-child
G
Gediminas Bivainis

As a fallback solution, you could wrap your classes in a parent element like this:

<div>
    <div>This text should appear as normal</div>
    <p>This text should be blue.</p>
    <div>
        <!-- first-child / first-of-type starts from here -->
        <p class="myclass1">This text should appear red.</p>
        <p class="myclass2">This text should appear green.</p>
    </div>
</div>

J
Jonathan.

You can do this by selecting every element of the class that is the sibling of the same class and inverting it, which will select pretty much every element on the page, so then you have to select by the class again.

eg:

<style>
    :not(.bar ~ .bar).bar {
        color: red;
    }
<div>
    <div class="foo"></div>
    <div class="bar"></div> <!-- Only this will be selected -->
    <div class="foo"></div>
    <div class="bar"></div>
    <div class="foo"></div>
    <div class="bar"></div>
</div>

Still doesn't work. See codepen.io/dearsina/pen/RwrBGGO
Works in my case. It styles the first element that has the class.
a
adrianTNT

Not sure how to explain this but I ran into something similar today. Not being able to set .user:first-of-type{} while .user:last-of-type{} worked fine. This was fixed after I wrapped them inside a div without any class or styling:

https://codepen.io/adrianTNT/pen/WgEpbE

<style>
.user{
  display:block;
  background-color:#FFCC00;
}

.user:first-of-type{
  background-color:#FF0000;
}
</style>

<p>Not working while this P additional tag exists</p>

<p class="user">A</p>
<p class="user">B</p>
<p class="user">C</p>

<p>Working while inside a div:</p>

<div>
<p class="user">A</p>
<p class="user">B</p>
<p class="user">C</p>
</div>

This will stop working if you place a <p> tag prior to your user A. See here: codepen.io/dearsina/pen/GRoBjZa
I have no idea why, but it worked for me wrapping them in a div
A
Aya Hany

I found something that works If you have a bigger class which contains something like grid, all of elements of your another class You can do like that

div.col-md-4:nth-child(1).myclass{    
border: 1px solid #000;     
}        

R
Robert Columbia

Simply :first works for me, why isn't this mentioned yet?


:first is a pseudo-class related to printing.