How can CSS be used to target the element with the highest order?

Initially, I understand that achieving this can be done through JS (and currently have this method in place), however, I would rather utilize native CSS if feasible. Less JS, less DOM access, and so on.

In Short

Is there a CSS selector available to target elements with a specific CSS property? More precisely, to target the element with the highest order property?

Something along the lines of...

el:highest(property:order) {...}

Scenario

I have a list structured like...

<div class="list-container">
    <ul class="list">
        <li style="order: 0;">One</li>
        <li style="order: 1;">two</li>
        <li style="order: 2;">three</li>
        ...
        <li style="order: n;">Last</li>
    </ul>
</div>

This is utilized as an infinite scroller, where the entire list (ul) scrolls inside the container. The specifics are not significantly crucial, but essentially the number of list items remains constant and are recycled as the user scrolls.

Note: Despite the fixed number of list items, there are no limits to the maximum order number. If the initial orders are order=0, 1, 2, 3 (list of four items), then 0->4, 1->5, 2->6, and so forth. Only one order changes at a time - either greater than all others (end of the list) or lesser than all others (start of the list).

I achieve this recycling effect by presenting the list as a flex-box and programmatically altering the order of the list items.

More relevant CSS includes something similar to...

.list-container {
    overflow: scroll;
}

ul.list {
    display: flex;
    flex-direction: column;

    list-style: none;
}

li {
    ... other rules
    margin-bottom: 5px;
}
li:last-child {
    margin-bottom: 0;
}

Each list item contains a bottom-margin to act as a spacer between each item for visual appeal. However, it appears odd when you reach the bottom of the list and encounter unnecessary spacing before the end of the list container. Therefore, the last-child setting removes the bottom margin.

The final appearance of the list resembles...

https://i.sstatic.net/21BgG.png

... please note: the list BG is set to red for visibility of padding, and li:last-child has BG set to aqua to highlight which item represents the last.

Issue resolved, right!?

However, The Problem...

(Assuming the list has n list items.)

In a typical list scenario, the above CSS implementation functions perfectly. The problem arises when I take the top list item (order: 0;) and update its order, causing it to move to the bottom of the list (order: n+1;). Although the list item may appear positioned and shown at the bottom of the list, it is still recognized within the DOM as the first list item.

The DOM structure is as follows...

<div class="list-container">
    <ul class="list">
        <li style="order: n+1;">One</li>
        <li style="order: 1;">two</li>
        <li style="order: 2;">three</li>
        ...
        <li style="order: n;">Last</li>
    </ul>
</div>

Consequently, the outcome appears as...

https://i.sstatic.net/xrgnt.png

Since the list item with order: n; persists as the last-child in the DOM, its bottom margin gets removed, even though another list item is displayed below it.

A similar issue arises when using margin-top and li:first-child. It's an unavoidable challenge!

Therefore, Can I...?

Is there a native CSS technique to target the element possessing the highest order number? Or alternatively, is there another CSS-only workaround to achieve a comparable result?

Many thanks!

Answer №1

To avoid removing the margin on the final li element, an alternative approach would be to conceal it by adjusting the bottom margin of the ul element to -5px and configuring the overflow property to hidden.

Answer №2

Is it possible to add a margin to the ::after pseudo element of your <ul>?

For example:

ul.list::after {
  margin-bottom: 10px;
  content: "";
  display: inline-block;
}

Answer №3

Given that the quantity of list items remains consistent as mentioned in the prompt, you can simply define the CSS for the largest order element by utilizing the attribute selector.

DISCLAIMER:

It's important to bear in mind that this selector is rather sensitive, and the content within the selector must match exactly with the content of the style attribute.

li[style*="order: 4;"] {
  background-color: lightblue;
}

.list-container {
  overflow: auto;
}

ul.list {
  display: flex;
  flex-direction: column;
  list-style: none;
}

li {
  ... other rules margin-bottom: 5px;
}

li:last-child {
  margin-bottom: 0;
}
<div class="list-container">
  <ul class="list">
    <li style="order: 4;">One</li>
    <li style="order: 1;">two</li>
    <li style="order: 2;">three</li>
    <li style="order: 3;">Last</li>
  </ul>
</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

Aligning text vertically within Bootstrap 4 divs, specifically those containing the btn class, without specifying a fixed height, while utilizing CSS Grid instead of flexbox

I'm working on a project with a CSS Grid containing Bootstrap 4 divs with btn tags. The issue I'm facing is that the divs are stretching vertically, and I need to vertically align the text in the middle. While I know I could use flexbox for this ...

Making Ajax requests using Node, express, and vanilla JavaScript

I have been experimenting with vanilla JS to make some ajax calls. On the server side, I am utilizing node within the express framework. It appears that the data I am attempting to send is not getting through to the back-end. Does anyone have any suggesti ...

Developing a PHP switch statement to alternate between different stylesheets based on specific

I've encountered a challenge while trying to create a button on my website that switches the stylesheet back and forth. The button is functioning properly, but whenever I click on it from a page other than the homepage, it keeps switching back to the ...

Div that scrolls independently without affecting the position of other elements

Visit this link for reference <div class="col-10 content" id="content"> <ul> <li>News</li> <li>News</li> <li>News</li> <li>News</li> < ...

The challenge of PHP's HTTP_REFERER

I am experiencing an issue with http_referer. In my setup, I have two files: index.php and index.html. The index.php file contains a form that, upon submission, includes information like the http_referer. On the other hand, the index.html file runs an aja ...

Formatting dynamically generated HTML using C#

My ASP.NET web forms site has a large menu that is dynamically generated using C# as a string. The HTML code returned looks something like this: <ul><li><a href='default.aspx?param=1&anotherparam=2'>LINK</a></li> ...

Ways to display and conceal menu depending on initial view and scrolling

On my website, I have implemented two menus - one to be displayed when the page loads and another to show up when a scroll occurs. You can view my page here. The goal is to have the white part visible when the position is at the top, and switch to the blu ...

Why is the validate function not being executed in the Backbone Model?

I am a beginner in learning Backbone and I am attempting to implement simple validation in my Person Model. However, I am facing an issue where the validate method does not run when I set a new age. Can someone guide me on where I might be making a mistake ...

Utilizing underscore.js to aggregate data points: A comprehensive guide

If I have an array containing 6 numeric data points and I want to transform it into a new array with only 3 data points, where each point is the sum of two of the original points. For example: [1,1,1,1,1,1] would become [2,2,2] What would be the most eff ...

When conducting a click + drag mouse action, Internet Explorer may experience freezing. What steps can be taken to identify the root cause of this issue

Currently, I am in the process of developing a JavaScript application designed for generating simple diagrams. However, I have encountered some performance issues specifically in Internet Explorer version 8. This application allows users to draw lines on ...

Request not being sent to the server by the code

I'm encountering an issue with sending a request from my app via the phonegap build solution and a push notifications plugin. Below is the code snippet: function tokenHandler(result) { var channelid; if (subParam == 2) { ...

What is the best way to code a JavaScript button that triggers a transition both when clicked and scrolled?

I am working with a .js document that enables me to switch between different pages within the same html document. Currently, I have a trigger button that activates the transition only on click, but I would like it to also activate on scroll or on scroll al ...

setting a custom data attribute for a model

Within my HTML code, I included: <div class="sli1" data-values="10, 20, 30, 40,50, 60, 70" I am trying to find a way to connect the data-values to a model that I can use again with Angular. I have looked into ng-bind but I am struggling to unders ...

Iterate through a loop to assign values to an object, then transform it back into an object

My goal is to create a Component with a child that could be any component, so I need to assign the props to this Component. I attempted to do this in a loop because I am unsure about the number of props that should be assigned. //Array of objects that I w ...

The HTML2Canvas library has been known to occasionally generate empty images without any warning, but this issue seems to occur only when

Currently, I am in the process of developing a meme generator, where users have the option to either enter an image link or upload a local file from storage. Below is the HTML code for both options: <div class="Main_Grid_Item Link_Upload_Div"& ...

Personalizing the success message of a function in node.js

I received this node.js code for an IBM Cloud function that allows a user of my Watson Assistant to send me email reminders. It's working flawlessly. var nodemailer = require('nodemailer'); let smtpConfig = { host: 'hosthere', ...

When the model is replaced, the Vue.js directive v-html may fail to update

Upon running the code below (a Vue.js component), my expectation is that both the v-html directive and the console.log() will display the same value after the AJAX call returns. To my surprise, while v-html remains stuck at "loading...(1)", the value of o ...

A guide to setting up Firebase(FCM) Push Notifications on nuxtjs / vuejs

Despite extensive hours scouring the internet for a user-friendly method to incorporate Firebase FCM Push Notification into my nuxt project, I came up empty-handed. ...

Javascript tree structures that enable the drag-and-drop of multiple items

At the moment, our application utilizes the ExtJS tree view. We now have a need for users to be able to select multiple nodes (which the tree view already supports through a pluggable selection model) and then drag these selections to another section of th ...

Tips for creating line breaks in Google Chart tooltips

I'm having trouble breaking a line in the code snippet below. Here is the complete code: <html> <head> <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script> <script ...