CSS - when "start" is not 1, an alternative to the "nth-child" selector

Imagine a scenario where you have a list of questions that start at 0 and you want to apply CSS styling based on their value. For instance, consider the following list...

<b>How are you feeling?</b>
<ol start="0">
    <li>Great</li>
    <li>Good</li>
    <li>Fine</li>
    <li>I don't understand</li>
</ol>

Is there a way to achieve this using pseudocode like the one below?

ol li {
    color: rgb(180, 180, 180);
}

ol li:value-of-child(2) {
    color: rgb(0, 0, 0);
}

This would result in the following display:

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

I attempted the following approach but it did not yield the desired outcome.

ol li {
    color: rgb(180, 180, 180);
}

ol li:nth-child(calc(my_value - 1)) {
    color: rgb(0, 0, 0); 
}

The value for my_value is input by the user through a separate text field from a different application, and I am constrained from using JavaScript to address this issue. It may sound unconventional, but my options are limited due to restrictions imposed by the document format I'm working with.

UPDATE: While I acknowledge the constraints I'm operating under, let's avoid assuming this is an XY problem.

My situation involves working with an app that does not allow JavaScript on the front-end but outputs content using WebKit that can be influenced by HTML/CSS. The use of JavaScript is prohibited to maintain readability as plain text and uphold company standards against integrating JS into our workflows.

The presentation of the list mentioned earlier operates independently from how users provide input. Due to character limits, any responses exceeding a certain length need to be truncated. To address this, each list is accompanied by a popup menu offering selections numbered 0-3 which are then displayed multiple times in the narrative. Utilizing the selected value allows for modifications using CSS selectors such as :nth-child.

In my case, the numbering of the list starts at 0, illustrating a decision made by the test creator regarding scoring ranges. Therefore, simply incrementing all values by 1 is not viable as the content cannot be modified, only reproduced accurately.

While looking for solutions, the goal was to optimize the appearance of the generated document without affecting interactive elements. This effort aims to enhance readability, especially within the application or when generating a PDF version of the document.

Despite limitations, leveraging CSS provides a means to improve the overall visual output of the narrative section of the document, making it more accessible and engaging.

Answer №1

Is this something you could potentially utilize?

Update:

The following sample demonstrates a potential logic that you may find useful. To provide more specific guidance, I would need to review the HTML code you are able to generate in order to determine if there are alternative solutions available.

ol > li {
  color: #BBB;
}
.qiz {
  position: relative;
  padding-bottom: 20px;
}                                
.qiz > div[data-value] {
  position: absolute;
  bottom: 0;
}

.qiz > div[data-value="1"] ~ ol > li:nth-child(1)  {
  color: #000;
}
.qiz > div[data-value="2"] ~ ol > li:nth-child(2)  {
  color: #000;
}
.qiz > div[data-value="3"] ~ ol > li:nth-child(3)  {
  color: #000;
}
.qiz > div[data-value="4"] ~ ol > li:nth-child(4)  {
  color: #000;
}

.qiz > div[data-value="5"] ~ ol > li:nth-child(1)  {
  color: #000;
}
.qiz > div[data-value="6"] ~ ol > li:nth-child(2)  {
  color: #000;
}
.qiz > div[data-value="7"] ~ ol > li:nth-child(3)  {
  color: #000;
}
.qiz > div[data-value="8"] ~ ol > li:nth-child(4)  {
  color: #000;
}

.qiz > ol[data-value="1"] > li:nth-child(1) {
  color: #000;
}
<div class="qiz nr1">
  Is This Awesome?
  <div data-value="3">Answer: 3</div>
  <ol start="0">
    <li>Yes</li>
    <li>No</li>
    <li>Maybe</li>
    <li>Eli Whitney</li>
  </ol>
</div>

<hr />

<div class="qiz nr2">
  Okay, What About This?
  <div data-value="2">Answer: 2</div>
  <ol start="5">
    <li>Kinda</li>
    <li>Kinda Not</li>
    <li>Never</li>
    <li>Supernever</li>
  </ol>
</div>

<hr />

<div class="qiz nr3">
  If you need it like this?
  <div data-value="8">Answer: 8</div>
  <ol start="5">
    <li>Dont Know</li>
    <li>Hopefully Not</li>
    <li>Maybe ...</li>
    <li>Doh, YES</li>
  </ol>
</div>

<hr />

<div class="qiz nr4">
  Using normal element flow?
  <ol start="0" data-value="1">
    <li>This is selected</li>
    <li>But this not</li>
    <li>Neither this</li>
    <li>Not the last either</li>
  </ol>
  <div>Answer: 1</div>
</div>

Answer №2

After some careful consideration, I was able to uncover a solution, but it comes with the condition that you must know the total number of items in the list. For instance, if there are 6 items (numbered from 0 to 5), you would use 6n-my_value. This will then be translated by the platform as 6n-2 if my_value is equal to 2.

This may indicate that you will need separate CSS for different ordered lists (OLs).

ol > li {
  color: #BBB;
}
ol > li:nth-last-child(6n-2) { /* or alternatively in your stylesheet: 6n-my_value */
  color: #000;
}
<ol start="0">
  <li>Excellent</li>
  <li>Wonderful</li>
  <li>Superb</li>
  <li>Poor</li>
  <li>Confidential</li>
  <li>I am unsure of the question's meaning</li>
</ol>

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

Scraping: Previously scraped links can no longer be retrieved for dumping HTML files into a designated folder

My current project involves using Python, Selenium, Sublime, and Firefox to scrape links from a specific website. I want to save the scraped pages as HTML files into a designated folder. Unfortunately, I've encountered challenges in getting the body o ...

Can one determine if a webpage is being displayed within an Infragistics igDialog?

Occasionally, my web page is displayed without a container and other times it's embedded within an igDialog of another container page, based on the user's navigation throughout our web application. Is there a way, using pure javascript or jQuery ...

Align the items at the center of a Vue Router Link

I am looking to design a navigation bar that includes router-link items, with each item displaying an icon on the left side and text on the right side. An example of this design can be seen in the Firebase navigation bar. https://i.sstatic.net/eg328.png ...

What is the correct way to send parameters in the action tag?

Can I set the res variable as the form action in HTML? <script> var name =$("#name").val(); var file =$("#file").val(); var res= localhost:8080 + "/test/reg?&name="+name+"&file=" +file ; </script> <form acti ...

Transmitting an image from an HTML file to a Node.js server

Hello, I am currently working on capturing frames from a video and converting them into images. However, I am encountering an issue when passing this image to server.js as I am unable to access it and the console shows its type as undefined. Below is the s ...

Ways to transfer ID to a different HTML page

I have some Javascript code in a file named nextPage.js. When this code is executed by clicking on something, it should transfer the value of category_id to another page called reportlist.html. Can you please provide assistance with this? var base_url = " ...

Ways to show or conceal content using only CSS

Looking for help with switching content using pure CSS. I'm not very skilled with CSS and would prefer to avoid using JavaScript if possible. Any assistance would be greatly appreciated. Below is a snippet of the code: If A is clicked, toggle-on-con ...

Controlling z-buffering for transparent textures in Three.js

Creating a simple map with flat, paper-like trees protruding from it in WebGL using THREE.js has been quite a challenge. The issue lies in understanding how the library handles z-buffering. Despite numerous attempts to tweak parameters like renderer.sortOb ...

Insert a div element into the JavaScript file

I have a JavaScript code snippet that needs to be inserted after the "Artwork" section. Here is the code: <div class="image-upload"> <label for="files"> <img src="ohtupload.jpg"> </label> </di ...

Is it possible to utilize Javascript instead of PHP Curl to present JSON API data?

After successfully displaying JSON API data from openweathermap.org on my HTML webpage using PHP Curl, I am now seeking help to achieve the same output using Javascript. My PHP script retrieves JSON data from the source, and here is a snippet of that PHP c ...

How can you use CSS animations to animate two images in a way that hides one while showing the other?

click here for the image link visit this webpage for the link I need I am looking to add an animated section to my website. The inspiration comes from the webpage linked above, where images slide down one after another in a seamless manner. I attempted t ...

Styling input[type='date'] for non-Chrome browsers with CSS

Looking for the css equivalent of: ::-webkit-datetime-edit ::-webkit-datetime-edit-fields-wrapper ::-webkit-datetime-edit-text ::-webkit-datetime-edit-month-field ::-webkit-datetime-edit-day-field ::-webkit-datetime-edit-year-field ::-webkit-inner-spin-bu ...

Adjust the parent container to match the height of the child container when it is being hovered

I have been searching for a solution to this issue, but none of the answers I found seem to work in my specific case. Currently, I have a "Mega Menu" with links on the left side that expand when hovered over, revealing hidden links with images on the righ ...

Unpredictable Behavior of CSS Transition with JS createElement() Function

I'm using JavaScript to create a div element and I'm adding the CSS property transition: .5s linear top; When the user clicks on the element (onmousedown event), it is supposed to smoothly move to the top of the screen and then be deleted using ...

Sending a request to a PHP file using Ajax in order to display information fetched from

I am attempting to display the database row that corresponds with the student's forename and surname selected from the dropdown list. I have managed to store the first name and surname in a variable called clickeditem. However, I suspect there may be ...

Why does LESS keep prompting me with missing elements?

I am currently working on my first project using Less and have been following a tutorial closely. However, when I try to compile with lessc, I encounter the following error: ParseError: Unrecognised input. Possibly missing something in C:\Path\t ...

HTML5 Boilerplate and optimizing the critical rendering path by deferring scripts and styles

Previously, I constructed my website layouts using the HTML5 Boilerplate method: incorporating styles and Modernizr in the head section, jQuery (either from Google CDN or as a hosted file) along with scripts just before the closing body tag. This is an exa ...

Conceal certain tables within a container

On my SharePoint page, I have the following HTML structure: <div id="ctl00_PlaceHolderLeftNavBar_ctl02_WebTreeView"> <table cellspacing="0" cellpadding="0" style="border-width:0;"> <table cellspacing="0" cellpadding="0" style="border-width: ...

Using jQuery, locate identical text and assign a class to the corresponding container

How can I check if any of the h2 elements match the text in h1, ignoring case sensitivity? If a match is found, I want to add a class to the container Product-div. <div id="PageHeader"> <h1>Same text</h1> </div> <div class= ...

Uploading pictures to a database using PHP and SQL

Anyone have a reliable php and sql code for image upload to a database? The ones I've found are glitchy and not supported by all browsers. Any suggestions? ...