Showing a dynamic number of elements based on the value of a CSS variable can be achieved by manipulating

Within my code, I have a CSS variable called --num that holds a numerical value.

Under the same parent, I have N HTML elements as children. Element n and element n+1 are neighboring siblings. Essentially, all N elements are adjacent to each other.

Is there a way for me to only display element n from this collection of elements if n is less than the value stored in --num?

I've attempted referencing CSS variables in selectors like :nth-child( var(--num)), but it seems that can only be done within styles.

While one option could involve using JavaScript to retrieve --num and loop through the elements, adjusting their display property, I'm curious if there might be a more elegant solution. Are there any clever CSS tricks available that could achieve similar functionality without needing JavaScript?

Answer №1

I found @Vals' answer intriguing, so here is an alternative approach focusing on height/width:

.elem {
  display: inline-block;
  max-height: 60px;
  max-width: 60px;
  position: relative;
  background-color: lightblue;
  --number: 2;
}
.test {
  border:1px solid;
  display:inline-block;
  padding:20px;
}
.test:hover .elem {
  --number: 6;
}

.elem:nth-child(1) {
  height: calc(100px * (var(--number)));
  width: calc(100px * (var(--number)))
}

.elem:nth-child(2) {
  height: calc(100px * (var(--number) - 1));
  width: calc(100px * (var(--number) - 1))
}

.elem:nth-child(3) {
  height: calc(100px * (var(--number) - 2));
  width: calc(100px * (var(--number) - 2))
}

.elem:nth-child(4) {
  height: calc(100px * (var(--number) - 3));
  width: calc(100px * (var(--number) - 3))
}
.elem:nth-child(5) {
 height: calc(100px * (var(--number) - 4));
  width: calc(100px * (var(--number) - 4))
}
.elem:nth-child(6) {
  height: calc(100px * (var(--number) - 5));
  width: calc(100px * (var(--number) - 5))
}
<div class="test">
    <div class="elem">
    </div>
    <div class="elem">
    </div>
    <div class="elem">
    </div>
    <div class="elem">
    </div>
    <div class="elem">
    </div>
    <div class="elem">
    </div>
</div>

Answer №2

Just for fun and not necessarily practical...

An interesting way to adjust opacity using a variable in your request

.elem {
  margin: 10px;
  display: inline-block;
  height: 60px;
  width: 60px;
  position: relative;
  background-color: lightblue;
  --number: 2;
}

.test:hover .elem {
  --number: 5;
}

.elem:nth-child(1) {
  opacity: calc(var(--number));
}

.elem:nth-child(2) {
  opacity: calc(var(--number) - 1);
}

.elem:nth-child(3) {
  opacity: calc(var(--number) - 2);
}

.elem:nth-child(4) {
  opacity: calc(var(--number) - 3);
}
.elem:nth-child(5) {
  opacity: calc(var(--number) - 4);
}
.elem:nth-child(6) {
  opacity: calc(var(--number) - 5);
}
<div class="test">
    <div class="elem">
    </div>
    <div class="elem">
    </div>
    <div class="elem">
    </div>
    <div class="elem">
    </div>
    <div class="elem">
    </div>
    <div class="elem">
    </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

In CSS3, what is the significance of placing a comma between transition and transform?

Just getting started with CSS3 and have a couple of questions: 1) I know that using -XXX-transtion: opacity .4s tells Webkit and Firefox browsers to gradually change the opacity to zero over 400ms. However, what does the comma right after signify? opaci ...

Displaying Hierarchical Data with AngularJS and ng-repeat

Recently, I've been working on finding an elegant solution to represent hierarchical data in my application. Specifically, I have implemented a slide effect for bootstrap badges that showcase sub-categories using AngularJS. With some guidance from th ...

Navigational Scroll Parallax Issues

I have been trying to implement a basic 2 layer parallax effect using ScrollMagic on my index page. The layout consists of a large image with content below it, but I am facing some challenges. Despite going through the documentation and source code multipl ...

What is the best way to assign a data attribute value based on an element's index using jQuery?

I am dealing with multiple sibling elements that are added dynamically or manually using JS. Each element has a data attribute called "data-sec_number" with a number value that increments by one for each subsequent element down the page. Currently, I am ma ...

The HTML component fails to acknowledge width settings

I seem to be having trouble identifying an issue or perhaps I am misunderstanding something (I am using daisyui + nx + tailwind css + angular). To simplify my problem, let's consider the following scenarios: In the first scenario, I have: <div cl ...

Firefox's Sneaky Scrollbars

On my website, I have a dropdown menu with 4 links leading to different sections - About Us, Platform, Contact Us, and Information. However, whenever one of these links is clicked, a vertical scrollbar appears. I have attempted to use the following code t ...

Adjust the positioning of individual navigation items to the right using Bootstrap 5

My website's navbar is built using Bootstrap 5 and I'm looking to make some adjustments. In the screenshot provided, I am trying to move the last three navigation items to the right side.https://i.sstatic.net/kbVKD.png I attempted using ms-auto, ...

placing a see-through overlay onto a text-filled div

I have a text within a div and I am looking to overlay a transparent image on this div. When the image is clicked, the content of the background div should be displayed with the div changing its width to show the entire text with a scroll bar. Does anyone ...

Align content to the bottom using Bootstrap 4

Looking for help with aligning content to the middle and bottom on a full-page bootstrap layout? The first page has a background image in the first div, and a darker layer on top in the second div. <div class="h-100 w-100"> <div class="h-100 w-10 ...

Tips on halting the current animation and modifying styles upon hovering

Currently, I have a basic label featuring a contact number with a blinking animation. However, the animation is only a simple color transition at this time (see code below). I have managed to successfully pause the animation and revert the text back to it ...

When the page is resized, the Font-Awesome icons shift position

/* icon sect*/ .social { margin-top: px; padding: 0; position: absolute; top: 60%; left: 50%; transform: translate(-50%,-50%); } .social li { list-style: none; float: left; margin: 0px; width: 60px; height: 10px; line-height: 60px; ...

Prevent styling on a fixed header in Jquery Mobile

Hello everyone, I am currently attempting to deactivate the theme/style on a fixed header within the following code snippet: <div id="home-header" data-role="header" data-id="mobile-header" role="banner" data-position="fixed" style="background-color: ...

Can you explain the distinction between using single and double quotes in my JavaScript and CSS code?

Is there a significant distinction between using single or double quotes in JS and CSS? Does it depend on personal preference, or are there certain instances where one is preferred over the other? In W3Schools, they employ single quotes for url('&apo ...

What is the method that Google uses to automatically load the footer at the bottom of the web page?

Google manages to keep its footer at the bottom of the browser regardless of resolution when fully maximized. How can I achieve a similar effect? ...

Resolving the issue of "Cannot set properties of null (setting 'innerHTML') at HTMLInputElement.loader" involves identifying the root cause and implementing

Need assistance with this code. Although I believe everything is correct, it still isn't working as expected. The error mentioned in the title comes up whenever I try to use it. I've made multiple modifications to the code but it's still not ...

Tips for adjusting column spacing in HighCharts

Looking for some advice on how to make a chart container scrollable and properly space the labels and bars within a parent container with fixed width and height. The goal is to ensure all labels are visible and bars are well spaced. Any suggestions or tips ...

What is the best way to incorporate a button for toggling CSS animations?

I need help adding a button to toggle the animation on this JSFiddle. I tried adding the code below but can't seem to start and stop the animation. body .a [class^="b"] { width: 200px; height: 142.8571428571px; margin: 0 auto; ...

Persist with displaying and concealing divs as we navigate through different pages

Incorporating a function into my main page has been successful so far, but I am seeking guidance on how to effectively utilize the function: localStorage.setItem("language", selectedLanguage); currentlanguage= localStorage.getItem("language"); My object ...

What is the best way to assign a CSS class to the jqGrid height property when using setGridHeight?

Our objective is to implement a media query css class. Currently, the code is utilizing $(window).height() to determine the height. $('#list').jqGrid('setGridHeight', $(window).height() - 380); Instead of relying on $(window).height( ...

Is there a way to extract and store an image from a webpage using selenium, beautifulsoup, and Python 3?

Currently, my main goal is to extract and save a single image from a website post logging in. After examining the image, I discovered that it has a full xpath of /html/body/form/main/div/section/div[1]/div/div[2]/div/img. My plan is to utilize beautiful so ...