Is there a way for me to select elements that don't have the class "class" and are an even

I've been utilizing CSS for various tasks, however, there is a particular situation where it doesn't seem to function correctly.

My goal is to create a list similar to a spreadsheet, with each line alternating between light grey and slightly darker grey colors.

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

<style>
  .spreadsheet {
    display: grid;
    grid-auto-flow: row;
  }
  .spreadsheet > div {
    padding: 0.5rem;
  }
  .spreadsheet > :first-of-type {
    border-radius: 1rem 1rem 0 0;
  }
  .spreadsheet > :only-of-type {
    border-radius: 1rem;
  }
  .spreadsheet > :last-of-type {
    border-radius: 0 0 1rem 1rem;
  }
  .spreadsheet > :nth-of-type(odd) {
    background-color: #fafafa
  }
  .spreadsheet > :nth-of-type(even) {
    background-color: #eaeaea
  }
</style>

<div class="spreadsheet">
  <div>Line 1</div>
  <div>Line 2</div>
  <div>Line 3</div>
  <div>Line 4</div>
  <div>Line 5</div>
  <div>Line 6</div>
  <div>Line 7</div>
  <div>Line 8</div>
  <div>Line 9</div>
</div>

All of the mentioned above works as expected. However, here's my concern in this specific context... I am using JavaScript to modify the classes of certain lines based on a filter. Essentially, I want to change the display property of filtered out lines to none, while maintaining the same alternating pattern of colors as before. Additionally, the first and last lines should still have rounded borders.

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

<style>
  .spreadsheet {
    display: grid;
    grid-auto-flow: row;
  }
  .spreadsheet > div {
    padding: 0.5rem;
  }
  .spreadsheet > :not(.hidden):first-of-type {
    border-radius: 1rem 1rem 0 0;
  }
  .spreadsheet > :not(.hidden):only-of-type {
    border-radius: 1rem;
  }
  .spreadsheet > :not(.hidden):last-of-type {
    border-radius: 0 0 1rem 1rem;
  }
  .spreadsheet > :not(.hidden):nth-of-type(odd) {
    background-color: #fafafa
  }
  .spreadsheet > :not(.hidden):nth-of-type(even) {
    background-color: #eaeaea
  }
  .hidden {
    display: none;
  }
</style>

<div class="spreadsheet">
  <div class="hidden">Line 1</div>
  <div>Line 2</div>
  <div class="hidden">Line 3</div>
  <div class="hidden">Line 4</div>
  <div>Line 5</div>
  <div>Line 6</div>
  <div>Line 7</div>
  <div class="hidden">Line 8</div>
  <div>Line 9</div>
</div>

You'll observe that Line 1 is now set to hidden, so logically, Line 2 should inherit the background-color of #fafafa and possess a border-radius of 1rem 1rem 0 0. Despite this, it does not reflect these styles, especially when visually adjacent to similarly colored lines.

Any suggestions on how to achieve the desired outcome?

Answer №1

This solution bypasses the issue of CSS determining whether a particular element is odd or even, eliminating the necessity for additional JavaScript.

By setting up a linear-gradient background on the parent element, the need to determine if an element is the last visible one is also eliminated.

The border radius is applied to the parent element as well:

* {
  margin: 0;
}

.spreadsheet {
  display: grid;
  grid-auto-flow: row;
  border-radius: 1rem;
  background-image: linear-gradient(#fafafa 0 2rem, #eaeaea 2rem 4rem);
  background-size: 100% 4rem;
}

.spreadsheet>div {
  padding: 0.5rem;
  line-height: 1rem;
}
<div class="spreadsheet">
  <div>Line 1</div>
  <div>Line 2</div>
  <div>Line 3</div>
  <div>Line 4</div>
  <div>Line 5</div>
  <div>Line 6</div>
  <div>Line 7</div>
  <div>Line 8</div>
  <div>Line 9</div>
</div>
With some children display: none;
<div class="spreadsheet">
  <div style="display: none;">Line 1</div>
  <div>Line 2</div>
  <div>Line 3</div>
  <div style="display: none;">Line 4</div>
  <div>Line 5</div>
  <div style="display: none;">Line 6</div>
  <div>Line 7</div>
  <div>Line 8</div>
  <div>Line 9</div>
</div>

It should be noted (as highlighted by @somethinghere) that this method is only effective for single line entries - similar to what you might see in a basic spreadsheet on a sufficiently wide viewport.

Answer №2

Maybe there's a better way to approach this in JavaScript. What if we tried something like the following:

document.getElementById(id).style.visibility = "hidden";

You could hide or display lines, then later apply changes with:

let flag = True;

for (const line in lines) { // Lines is an array with your lines
  if (line.style.display != none){ // If the line is being displayed
    if(flag){ // Flag acts as a line color control
      line.style...// Make it so it displays a white line
    } else {
      line.style...// Make it so it displays a gray line
    }
    flag = !flag; // We switch the color of the line
  }
}

Would it be simpler to assign a different id for each line instead of changing the classes? It might help simplify the whole process.

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

The jquery.blockUI plugin seems to be malfunctioning

Trying to disable a simple div: <div id="MyDiv"> </div> Here is the code I used: $('#MyDiv').blockUI({ message: '<h1>This has been blocked!</h1>', css: { border: '3px solid #a00' } }); Issue ...

The fixed position seems to be malfunctioning and behaving more like absolute

I have a simple question. I am currently designing a mobile version of a page, and we are trying to keep the "snag it" yellow button fixed at the bottom. However, the 'position: fixed' property is not working as expected; it's behaving like ...

How can I achieve a full-width div without using the container-fluid class while still maintaining the width and structure of

I am looking for a full div without the container-fluid class while keeping the container width fixed. The structure of the container should not change as I need another div inside the container. The structure should remain as it is. .main { padding: ...

Guide on connecting a URL with a generated ID to the <a href> element

I am currently working on building my own portfolio website using Django. The main idea behind this project is to create posts showcasing the websites that I have developed. These posts will be featured on a page titled "Projects," where viewers can click ...

When clicking on the side-bar, it does not respond as expected

My website has a menu layout that features a logo on the left and an icon for the menu on the right side. When the icon is clicked, the menu slides in from the right side of the window, and when clicked again, it slides out. However, I am facing two issues ...

I have developed a custom jQuery carousel that includes functionality to start and stop the carousel based on specific conditions

I have set up a jQuery carousel that moves to the left when a checkbox is checked, but I need it to stop moving when the checkbox is unchecked. Can someone help me achieve this? $("#checkBox").click(function(){ if($(this).prop("checked") == true){ ...

None of the stylesheets are functioning properly in WordPress except for the menu

After installing a new theme and making some menu edits, my site suddenly broke. Strangely, only the menu css seems to be working fine. Instead of images, I am now seeing a string of array codes and the css is not functioning properly. It seems like the ...

Validating Date Input in HTML and JavaScript Text Fields

When designing my form, I wanted to ensure that users could only input digits and hyphens in a text field where they enter a date. However, I encountered some difficulty implementing this feature. Currently, the field only accepts digits and removes any le ...

Using the viewport meta tag in HTML and CSS within Laravel's Blade template while also

Greetings everyone! I have a question regarding Blade Laravel: Can we add a condition to the meta name="viewport" content="width=device-width, initial-scale=0.4"? I want the scale to start at 0.4 for smartphones and at 0.6 for tablets. ...

Artistic Canvas: Selected Image

I am trying to determine if the user has clicked on an image drawn in a canvas element. Despite clicking on the image, nothing seems to be happening. The alert function is not being triggered and the last condition in the code never evaluates to true. An ...

Display the homepage if a user accesses a page intended for an ajax request

Currently, I am utilizing jQuery to create a straightforward website. The main page is called 'index.html' and it has the capability to load different content (such as 'info1.html' or 'info2.html') through jQuery ajax requests ...

Nivo Slider's Interactive Image Mapping

I am encountering difficulties in integrating an image map into my nivo slider. The code I am currently using is shown below, and you can access the site by clicking on this link. Do you have any suggestions on how to resolve this issue? <div class=" ...

Generate a new div element dynamicaly after every 10 elements are added

I am facing the following scenario: I need to dynamically add an UNKNOWN number of checkboxes to a layout component, arranged in columns with 10 checkboxes per layout item. Although I have successfully added all the checkboxes to a single div element, I ...

What is an alternative method for creating a horizontal line without the need for the <hr> tag?

Is there a way to create a slim horizontal line without using the <hr> tag ? This is what I attempted: .horizontal-line{ border-top: 1px solid #9E9E9E; border-bottom: 1px solid #9E9E9E; } While it does function, I am hoping for a thinner line. ...

Difficulty maintaining consistent line widths while setting up a CSS border grid on a table

What is the most effective method for creating a table with a 1px grid (internal and external)? Could it be possible that Chrome and Edge have issues with borders in certain scenarios? On this Codeply example, the standard approach is taken - setting bord ...

Ways to align images of the same size in a gallery using the margin: auto property

I have an image gallery within a div element named "container" with a specified width:70%. I need the images to be justified with auto margins. This is the HTML code I am using: (There are 4 images in total) <div class="container"> < ...

Parallax Materialize fails to initialize

Despite my efforts to initialize the code, I am still unable to display the images. I've only been coding for two days, so I'm clueless about what else could be causing this issue. Here is the current state of my code. Thank you in advance! < ...

Make sure to fully load the HTML page before running the PHP code

Here is the code snippet I am currently working on: <div class="text-center"> <img src="img/ford_lodding.gif" alt="Loading GIF" style="width: auto; height: 500px;"> </div> <?php require 'php/loading.php';?> The cont ...

Repositioning a variable number of divs as the cursor hovers over the target area

Can anyone show me how to generate div elements in a loop using jQuery and change their position with the "mouseover" function? I've tried some code, but the positioning isn't changing correctly. Any suggestions on what needs fixing? var r, g, ...

ways to halt a noise once an animation is complete

I don't have much experience with coding in general, but somehow I've made it this far. Now I'm stuck on the final piece of the puzzle. This is related to a Twitch alert that I'm setting up through 'Stream Elements'. The iss ...