0

On a page, I have two paragraphs, both of which are identified via .entry p selector. However, the first one also includes a timestamp, which is marked with .time selector – the code looks like this:

<div class="entry">

    <p><span class="time">17:25:22</span> Sed ut perspiciatis, unde omnis iste natus.</p>

    <p>Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit?</p>

</div>

Now, is it possible apply different rules to those two paragraphs without introducing new selectors? Like, because of the condition that the first one contains this span class="time", and the second one does not?

A S
  • 1,195
  • 2
  • 12
  • 26

1 Answers1

1

You could use :first-child, in the entry div if the first p is always going to contain a time, then you could just target the first p in the div.

For example:

.entry p:first-child {
 css here
}

or using Jquery:

$('.entry p .time').parent().addClass('p-class');
Sensoray
  • 2,360
  • 2
  • 18
  • 27
  • *"because of the condition that the first one contains this span class="time", and the second one does not?"* - OP was quite specific about the case they're looking for, and I don't think selecting `

    ` by index satisfies that. The hypothetical selector they'd be looking for would target any `

    ` that has a `.time` child. It's not possible.

    – Tyler Roper Jun 19 '17 at 15:11
  • @Santi OP is trying to target the paragraph that contains time. If it's always going to be the first paragraph in the entry div, then :first-child would be a good option. – Sensoray Jun 19 '17 at 15:13
  • Unfortunately, there will be more those timestamps down the page. It's just they are mixed with no-timestamps paragraphs. – A S Jun 19 '17 at 15:13
  • 1
    Then I'd use jquery to target all `$('.entry p .time').parent().addClass('special-p');` or whatever the class would be. – Sensoray Jun 19 '17 at 15:16
  • 1
    Yeah, thanks, @PaigeMeinke. – A S Jun 19 '17 at 15:17
  • @As jQuery also has a [`:has()` selector](https://api.jquery.com/has-selector/): `$("p:has(.time)").addClass("myClass");` – Tyler Roper Jun 19 '17 at 17:16