Select the specific element only if it is followed by no other siblings

I am puzzled by the following HTML markup and CSS style:

[class^="container"] .target:last-of-type {
  color:red;
}
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
  <div class="container-1">
    <div class="item">1</div>
    <div class="item">2</div>
    <div class="target">3</div>
    <div class="item">4</div>
    <div class="item">5</div>
  </div>
  <br>
  <div class="container-2">
    <div class="item">1</div>
    <div class="item">2</div>
    <div class="item">3</div>
    <div class="item">4</div>
    <div class="target">5</div>
  </div>
</body>
</html>

I am trying to target the .target element, only if it is the last one within its container.

The use of the :last-of-type class seems to work in this scenario, but I find it confusing. Shouldn't it apply to both .target elements? After all, they are the only and last elements within their respective parent containers. Am I misunderstanding the purpose of the :last-of-type selector?

Answer №1

:last-of-type doesn't target the class itself, but rather focuses on the type, meaning the tag of the element.

This is why it only affects the last element within the second container div, applying the style when the .target also happens to be the :last-of-type, which in this case is the last div inside its container.

I've made some adjustments to your code sample, try it out with either the .item class or directly targeting the div tag before the last-of-type selector.

[class^="container"] .target:last-of-type {
  color:blue;
}
[class^="container"] .item:last-of-type {
  text-decoration: underline;
}

[class^="container"] div:last-of-type {
  text-indent: 20px;
}
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
  <div class="container-1">
    <div class="item">1</div>
    <div class="item">2</div>
    <div class="target">3</div>
    <div class="item">4</div>
    <div class="item">5</div>
  </div>
  <br>
  <div class="container-2">
    <div class="item">1</div>
    <div class="item">2</div>
    <div class="item">3</div>
    <div class="item">4</div>
    <div class="target">5</div>
  </div>
</body>
</html>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The styling of the first column in this row will be different from the rest

Despite having the same styling and no typing errors, I am still facing an issue with the first column not being styled correctly. It appears as nothing but text inside a column square. HTML: <div class="container-fluid"> <!--Nav Bar--> ...

Having trouble getting the CSS transition effect to work properly

With the click of a button, I alternate the opacity of an element between 0 and 1. A transition effect is supposed to take place for the opacity property. However, the transition effect does not seem to work as expected. Check out this link to see the p ...

Troubleshooting Problem with CSS3 Transition Rotation

Can someone help me with my CSS3 transitions issue? I'm working on a new website and trying to rotate some images. It's working fine in a new HTML document, but when I try to add it to my actual website, nothing happens. Any advice or tips would ...

Issue: Unable to interact with a checkbox using Selenium and Python due to element visibility problem

Currently, I am in the process of automating a specific aspect of my ongoing project. This particular task involves accessing a webpage and fetching a randomly generated key. In order to acquire this key, the script must navigate to the website, log in, an ...

Enhance the functionality of your website by implementing a zoom feature

I am looking to dynamically change the CSS of a div using jQuery when it is clicked. The challenge I am facing is that there are multiple divs with the same class and I am unable to modify their classes as they are created dynamically using PHP. <div ...

Exploring the world of CSS: accessing style attributes using JavaScript

So I created a basic HTML file with a div and linked it to a CSS file for styling: HTML : <html> <head> <title>Simple Movement</title> <meta charset="UTF-8"> <link rel="stylesheet" type=&qu ...

Show the Select component in a grid format with a list view, displaying three items per row

Within my react Dropdown component, there is currently a list view displayed when an item is selected. However, I am looking to modify it so that the items are arranged in a grid format with 3 items per row. Sample Code: import { Dropdown } from 'pri ...

Creating a mouse-resistant div with a magnet-like effect

Looking for a way to make a circle move like a magnet effect when the mouse is near, causing elements underneath it to be exposed. If you want to see an example, check out this fiddle link: http://jsfiddle.net/Cfsamet/5xFVc/1/ Here's some initial c ...

Switching the Focus in Laravel Layouts

My understanding of CSS media queries includes: @media screen and (orientation: portrait) { div.container { ... } } @media screen and (orientation: landscape) { div.container { ... } } Despite this knowledge, I am looking ...

Adjust the size of the Vimeo video to fit the container dimensions and eliminate any black bars

Is there a way to eliminate the black bars that are present in the vimeo video which has been embedded? Below is the link to the code snippet. JSFiddle Link <div class="embed-container"> <iframe src="https://player.vimeo.com/video/86019637?title= ...

Identify whether the page is being accessed through the Samsung stock browser or as an independent web application

Hey there! I am on a mission to determine whether my webpage is being accessed through Samsung's default browser or as a standalone web app saved on the homescreen. Unfortunately, the javascript codes I have come across only seem to work for Safari an ...

Steps to import shared CSS using Styled-components

In my project using react, I've implemented styled-components for styling. One requirement is to have a shared loading background. Here is the code snippet of how I defined it: const loadingAnimation = keyframes` 0% { background-position: 95% 95%; } ...

I am a beginner in using Bootstrap and I am currently working on creating a navigation bar. However, despite inputting the correct code, the navigation bar does not seem to show up on

I'm currently learning how to use bootstrap and I've encountered an issue while trying to create a navigation bar. The problem is that despite using the correct code, the nav bar is not appearing on my webpage. I suspect it might be due to compat ...

Adjust the placement of the fixed header waypoint or change its offset

Currently working on developing a Wordpress site with the Avada theme, my goal is to have a sticky header that starts immediately at the top of the page. This is the website in progress: The site I'm trying to emulate is synergymaids.com. If you vi ...

Prevent Bootstrap 5 input fields from automatically adjusting to the height of a CSS grid cell

Below is some Bootstrap 5 markup that I need help with. The first example is incorrect. I don't want the Bootstrap input to be the same height as the grid cell. The second example is right, but I want to achieve the same result without using a wrapp ...

Is there a way to modify the font color in Chrome when the input autofill feature is active?

I tried adding the css code below: input:-webkit-autofill{ color:white; } Unfortunately, it didn't have any effect. If anyone can assist me with this issue, I would greatly appreciate it. Thank you. ...

The margin set in Bootstrap isn't making any difference

I'm currently utilizing bootstrap 4 in my react project, and while the bootstrap components seem to be functional, I am facing an issue where setting margins does not work as expected. Here is a snippet of code from one of my pages: return ( < ...

Tips for Moving an h1 Tag to the Beginning of a Div

As a beginner in HTML, I have encountered an issue with my code. My div element contains an image and an h1 tag, but the h1 is currently positioned below the image. How can I center the h1 on top of the image? <div class="headwrap"> <div clas ...

Python script encounters issue with clicking button within Tor Browser using Firefox

I've been attempting to access a specific site (dumpert.nl) using Tor Browser as a proxy with Firefox. My goal is to visit the website with a different IP address every time I access it. While I know this is achievable, I haven't yet discovered h ...

Exploring the Matrix Filter Functionality in JavaScript

After successfully setting up CSS rotation for all browsers, I am now looking to achieve the same effect using JavaScript. jQuery is also being utilized in this process with the following code snippet: .css({ '-ms-filter':"progid:DXImageTransfor ...