Locate a list item within an unordered list and proceed with a comparison

Looking for a way to target an li within a specific ul that has a data-rank higher than the others, and apply different styling to it in comparison to its sibling li.

Is there a smart approach to achieve this using regular CSS or styled components?

<ul>
  <li data-rank='1'>Person1
    <ul>More li here</ul>
  </li>
    
  <li data-rank='1'>
    Person2
    <ul>More li here</ul>
  </li>

  <!-- The aim is to modify the style of this li as its siblings have different ranks -->
  <li data-rank='2'>
    Person3
    <ul>More li here</ul>
  </li>

  <li data-rank='1'>
    Person4
    <ul>More li here</ul>
  </li>
</ul>

Answer №1

If you want to target a specific html tag using modern CSS, you can do so by referencing its attribute. Here's an example:

li[data-rank="2"] { margin: 50px; }
<ul>
  <li data-rank='1'>Person1
    <ul>More li here</ul>
  </li>
    
  <li data-rank='1'>
    Person2
    <ul>More li here</ul>
  </li>

  <!-- I want to style this li because its siblings have a different number -->
  <li data-rank='2'>
    Person3
    <ul>More li here</ul>
  </li>

  <li data-rank='1'>
    Person4
    <ul>More li here</ul>
  </li>
</ul>

Always remember to verify the compatibility with different browsers before implementing such styles.

Best of luck with your project, "tocayo".

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

Activate all chosen CSS circles

I'm currently working on creating a progress bar using CSS circles. The idea is that when you click on each circle in sequence (1, 2, 3), all three circles and the line connecting them will fill up with red color. If you then go back (3, 2, 1), the co ...

The transparent DIV/waiting message may not be displayed in IE10

Here is a DIV container that serves as a transparent background with a loading message. This is the HTML code for the DIV container: <div id="generatingexcel" style="display:none; position: fixed; width: 100%;height: 100%; z-index: 999; top: 0; left: ...

What is the best method for combining multiple text box values into a single text box?

I need assistance with concatenating multiple text box values into one dynamic text box. I am currently retrieving values using IDs, which are dynamically added as the text boxes are dynamically created. However, when using a for loop to dynamically add te ...

Saving changes made in HTML is not supported when a checkbox is selected and the page is navigated in a React application using Bootstrap

In my array, there are multiple "question" elements with a similar structure like this: <><div className="row"><div className="col-12 col-md-9 col-lg-10 col-xl-10 col-xxl-10"><span>Question 1</span></div ...

CSS transforms fluidly transition between absolute and relative positioning

Can CSS transitions be used to combine "position: relative" and "position: absolute" in a smooth manner? I have developed a compact widget called the Deck, which has both a collapsed and expanded state. Currently, it is working well by using absolute posi ...

Issue with JQuery click() not triggering on a specific div button

I am attempting to replicate a user's click on a website where I have no control over the code. The specific element I am trying to interact with is a div that functions as a button. <div role="button" class="c-T-S a-b a-b-B a-b-Ma oU v2" aria-dis ...

Designing a webpage feature that enables users to choose an image from the selection

Can someone help me with coding a feature on my page where users can simply click an image to select their favorite design? I want the selection to be recorded and sent to me without requiring any text input from the user. I plan to use jQuery to display t ...

Attempting to interact with the <Button> element, within selenium web driver utilizing python

I have attempted to interact with a button in the given HTML using Python (Selenium WebDriver). <div class="cssm-TopNav_printIcon_2VzB_"> <button type="button" aria-label="Print" title="Print" class="cssm-Button_button_2a549 cssm-Button_secondar ...

I am looking to remove all white spaces from my website's HTML code

I am looking to remove all white spaces from my website in html. The more I align my text to the right, the more whitespace it creates on the right side of the page. As seen in the images, there are a lot of whitespaces and I suspect that elements are caus ...

Utilize CSS styling on dynamically loaded HTML content

I am working on a project similar to the StackOverflow editor. I am fetching markdown text, converting it to HTML, and then displaying it in a preview area. However, when I try to style the injected HTML elements using CSS, the styles are being ignored. Up ...

The functionality of scrolling ceases to work once the bootstrap modal is closed

I am utilizing Bootstrap v3.1.1 to showcase a modal popup on one of my web pages. The following code shows how I have implemented it. <!--Start show add student popup here --> <div class="modal fade" id="add-student" tabindex="-1" role="dialog" a ...

Sass can be used to create custom color classes for Bootstrap 5.3 that offer

I am currently using a Bootstrap 5.3 theme and I would like to give users the option to change the main color theme from the default blue to something like purple or orange. My idea is to create additional CSS files named "orange-as-primary.css", "purple- ...

Encountering issues with asp.net code following saving a PNG file to disk - troubleshooting with iTextSharp and PDFStam

I've managed to successfully draw on an HTML5 canvas and save the canvas to a PNG file on the server. I then embed the PNG into a PDF using iTextsharp. Everything is working well, but there's one issue. My code currently displays the PNG on the ...

Displaying dates on the Amcharts category axis for instances with empty data

I am currently creating a fruit consumption chart for each day, and so far everything is working correctly in the provided example. var chart = AmCharts.makeChart("chartdiv", { "type": "serial", "hideCredits": true, "fixedColumnWidth": '10px& ...

Utilizing Javascript for altering HTML elements

It seems this issue is quite puzzling and I believe another perspective could be beneficial in identifying the problem. Despite my efforts, I am unable to understand why the "Energy Calculator" does not return a value when submitted, whereas the "Battery C ...

Creating uniform-sized cards with CSS and Bootstrap

Is there a way to ensure consistent card size across all lines? I've noticed that the cards in the second line are taller. Can we base the height of all cards on this? https://i.sstatic.net/KgZwu.png Below is the HTML code: <div class="row"&g ...

How can HTML be assigned to a PHP variable without losing its formatting?

My goal is to store HTML in a variable in PHP to help with modularizing my code. $html_block1 = "<div class='item'> <img src='chicago.jpg' alt='Chicago'> <div class='carousel-caption&ap ...

Converting RGBA to Hex Color Code with Javascript: A Step-by-Step Guide

I attempted to change the rgba color representation to hexadecimal, but I encountered difficulty in converting the opacity value while successfully converting the remaining colors. Here is the snippet of my code: var colorcode = "rgba(0, 0, 0, 0.74)"; ...

Position an image at the center within a footer widget

I am attempting to position an image at the center of a footer widget. I have gone through various solutions recommended here, but I am unsure if I am implementing the code correctly. Here is my CSS: #footer img.center { display: block; margin-le ...

Constructing an interactive table from a JSON dataset using Node.js and Express

Just starting out with Node.JS here! I'm currently working on creating a page with multiple tables and information using NodeJS. However, I've hit a roadblock when trying to display the result of an SQL query in an HTML table. Right now, I'm ...