Css selector that targets the first or last element of its type while also having another specified class applied

When creating a simple pager, I encountered the need to apply specific styles to the current page if it happens to be either the first or last page. However, I am unsure how to incorporate the :first-of-type/:last-of-type selector along with the usual class selector.

   <div id="myPages">
      <div class="something-else"></div>
      <div class="page-number current-page">1</div>
      <div class="page-number">2</div>
      <div class="page-number">3</div>
      <div class="another-one"></div>
  </div>

I attempted using

#myPages .page-number:first-of-type.current-page
, but it did not yield the desired results due to the presence of the something-else and another-one div elements.

In order to achieve the desired outcome, check out my updated jsFiddle example, where I aim to make the '1' text appear in blue for the current page.

Thank you for your assistance.

[edit] I have made further updates to the fiddle to clarify my intention :)

Answer №1

UPDATE: There has been a modification to the original post, prompting me to adjust my response:

#explain {
  margin-bottom: 20px;
}

#myPages .something-else+.current-page {
  color: blue;
}

#myPages div:nth-last-child(2) {
  color: red;
}
<div id="explain">
  Please note:<br> - there will only be one 'page-number' with 'current-page' in my app<br> - I don't know beforehand how many pages there will be (therefore nth-child won't work)
</div>
<div id="myPages">
  <div class="something-else"></div>
  <div class="page-number current-page">1 - Should appear in blue as it is the first page-number <strong>and</strong> possesses the current-page class</div>
  <div class="page-number">2</div>
  <div class="page-number current-page">3 - No action required</div>
  <div class="page-number current-page">4 - Must display in red since it's the ultimate page-number <strong>and</strong> contains the current-page class</div>
  <div class="another-one"></div>
</div>

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

Having trouble with Bootstrap form-inline stacking issue, any tips on how to stop them from stacking?

I've created a simple test navbar with a search input box and search button using the Bootstrap class form-inline. However, the search button is appearing below the search input, even though there is enough space available. You can view the codepen he ...

navigation panel including menu items and company logo positioned to the left side

My webpage contains the following code: <nav class="navbar" style="background-color: #264653; position:absolute; top:0px; left:50%; transform:translateX(-50%); width:100%; "> <div class="container-fluid"> < ...

IE not rendering 'right' property in jqueryui CSS

I am attempting to shift a neighboring element after resizing a text area, triggered by the stop event on jqueryUI's resizable feature: $("textarea").resizable({ stop: function (event, ui) { var x = ui.originalElement.closest("li").find(" ...

Looking for assistance with aligning a form in the center?

After spending the last hour trying to center a form for a name without success, I have decided to seek help here. Although I have searched extensively, I am still struggling at this point. Here is my HTML: <div id="main" class="main"> <tabl ...

The command to fix Eslint is not able to resolve all of the errors related to vue/html-indent

In my .eslintrc.js file, I have set up some linting rules as shown below: module.exports = { root: true, env: { browser: true, es6: true, node: true, }, extends: ['plugin:vue/recommended', 'eslint:recommended'], pa ...

Is it possible to send a PHP variable to a popup using a button and JavaScript?

I am facing an issue with a dynamically created table in PHP that displays requests. Each row in the table has a button to open a popup. I need to pass the ID of each request to the popup to retrieve all the data associated with it. Can someone guide me o ...

Issues with implementing Bootstrap 4 navbar on a Rails 5 application

I'm currently in the process of transitioning my static HTML/CSS/JS website to a Rails application. Using Bootstrap 4, I had all the functionality and CSS classes working perfectly on the static site. However, after the migration to the Ruby app, the ...

Incorporate a variable into a CSS class

Currently, I am working on developing a component that creates an animation loop with a duration that is passed as a prop to the component. The issue I am facing involves using CSS to create the animation: animate:{ -webkit-transition: 10.0s !important ...

Compact looped slideshow

Currently in the process of designing a website and looking to incorporate a unique photo gallery feature. The concept is as follows: The photo gallery will be displayed in a rectangular/box shape. There are a total of 10 photos, with only 7 initially vis ...

Span within a span using Bootstrap

I am facing some challenges in figuring out how to nest a span within another span using Bootstrap. My objective is to have a centrally aligned block, with the following structure inside: - One row containing: - A block on the left (span6) ...

before sending the url with fetch(url), it is adjusted

My front end code is currently fetching a URL from a local node.js server using the following snippet: fetch('http://localhost:3000/search/house') .then(.... Upon checking what is being sent to the server (via the network tab in Firefox dev ...

Checking for correct format of date in DD/MM/YYYY using javascript

My JavaScript code is not validating the date properly in my XHTML form. It keeps flagging every date as invalid. Can someone help me figure out what I'm missing? Any assistance would be greatly appreciated. Thank you! function validateForm(form) { ...

Issue with Tooltip Position when Scrolling Sidebar is causing display problems

My goal is to create a Sidebar with Tooltip attached to its <li> elements similar to this example: Screenshot - Good Tooltip However, I am facing an issue where the position of the Tooltip breaks when scrolling to the bottom of the sidebar, causing ...

Guide to designing a progress bar using numerical data stored in a database

I am currently working on a project to create a dynamic progress bar based on numeric values stored in a database. The database has two fields, 'NEXT_STEP' and 'MAX_STEPS'. When the value of NEXT_STEP reaches MAX_STEPS + 1, it indicates ...

Selenium encountering difficulty retrieving the select outcomes

I just started using Selenium and I'm having trouble extracting the values from a select tag on a specific website (link provided in the code). Every time I try to retrieve the select tag, it comes back as empty. Here's my code: from selenium i ...

The style was declined from being applied due to its MIME type, which was identified as 'text/html'

Currently, I am working on VueJS with Webpack and SCSS. I am facing a problem when attempting to import the SCSS file into the component using the following code: <style type="scss"> @import "/sass/pages/_initial.scss";</style> Upon doing so, ...

The back-to-top feature is malfunctioning in the new Bootstrap 4 update

I've been working on adding a back-to-top button to my website using JavaScript in Bootstrap 4 with the Font Awesome icon set. It was functioning perfectly until I made some changes to the site, and now it's not working anymore. I'm pretty n ...

Error: Unable to submit form with jQuery due to timeout issue - e[h] function not recognized

This question has surfaced on SO previously without any solution, maybe due to a different scenario Below is the form in question <form id="testSubmit" method="post" action="submit.php"> <!--The value of the following field will be collecte ...

Why won't the div move when I click it?

Could you please explain why my JavaScript code isn't functioning as expected? The intended behavior is for the 'mark' div to move to the current mouse coordinates upon clicking within the map. ...

What is the best way to pass a value from an addEventListener to another object for use?

In my project, I am trying to create three sliders that can adjust the color of a div when changed. Each slider functions properly, and I am able to track the value changes in the console. However, I am facing a challenge when attempting to assign that val ...