Home > Web Front-end > CSS Tutorial > Why Doesn\'t `p.visible:last-of-type` Target the Last Visible Paragraph Element?

Why Doesn\'t `p.visible:last-of-type` Target the Last Visible Paragraph Element?

Linda Hamilton
Release: 2024-11-03 19:20:02
Original
396 people have browsed it

Why Doesn't `p.visible:last-of-type` Target the Last Visible Paragraph Element?

:last-of-type Selector Behavior

The :last-of-type pseudo-class targets the last element of a specified type within a parent container. However, in the context of your CSS selector (p.visible:last-of-type), it's important to note that :last-of-type solely applies to element types and not class instances.

In your HTML markup:

<code class="html"><div>
  <p class="visible">This should be hidden</p>
  <p class="visible">This should be displayed</p>
  <p>This should be hidden</p>
</div></code>
Copy after login

The issue arises because :last-of-type targets the last

element, which in this case does not have the .visible class. Consequently, none of your

elements are visible.

Solution

To target the last

element with the .visible class, you must use JavaScript, as there is no built-in CSS selector for this specific purpose.

The above is the detailed content of Why Doesn\'t `p.visible:last-of-type` Target the Last Visible Paragraph Element?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template