Leveraging the power of the nth-of-type CSS

I am currently in the process of building a website that includes a lengthy page with full-page slides, each featuring two clickable images that open modal windows when clicked.

The issue I am facing with my modals is that I have set them at a specific distance from the top, which works fine for the first slide but becomes problematic for subsequent slides.

One approach I am considering to address this problem is using nth-child. Since each slide contains 2 images, I thought about adding 100% to 3 and 4, 200% to 5 and 6, and so on...

However, I have been unsuccessful in getting nth-child to work as expected. Here is a snippet of my CSS:

.modalDialog {
    position: fixed;
    font-family: Arial, Helvetica, sans-serif;
color:blue;
    left: 22%;
width:80%;
height:90%;
top:103%;
    z-index: 9;
    opacity:0;
    -webkit-transition: opacity 400ms ease-in;
    -moz-transition: opacity 400ms ease-in;
    transition: opacity 400ms ease-in;
    pointer-events: none;
}

.modalDialog:nth-of-type(1){
    color:yellow;
    top: 203%;
}
<div id="slide1" class="slide">
  <div class="title"> 
        blah blah
  </div>
    <div class="tooltip">     
        <a href="#openModal">
            <img src="ONE.png">  
            <p class="tooltiptext">yup</p>
        </a>
    </div>
    <div class="tooltip">
        <a href="#openModal2">
            <img src="two.png">  
             <p class="tooltiptext">aye..</p>
        </a>
    </div> 
        <div id="openModal" class="modalDialog">
            <div>
                <a href="#close" title="Close" class="close">X</a>
                <h2>descriptions</h2>
                <img src="ONE.png">
            </div>
        </div> 
        <div id="openModal2" class="modalDialog">
            <div>   
                <a href="#close" title="Close" class="close">X</a>
                    <h2>desc</h2>
                    <img src="two.png">
            </div>
        </div>      
  </div>  

As the slide number increases, the openModal also increments (e.g., slide3 has openModal5 and 6).

Unfortunately, I am struggling to make nth-of-type or other methods like nth-child work properly. I can't even get the second modal on the first slide to change its properties. I'm simply looking to elicit some sort of response with the code snippet provided above.

What could I be doing incorrectly here?

Answer №1

One way to select elements is by using the attribute selector [id$="Modal1"]

[attr$=value]

This selector targets elements with an attribute named attr where the value ends with "value".

.modalDialog {
  position: fixed;
  font-family: Arial, Helvetica, sans-serif;
  color: blue;
  left: 22%;
  width: 80%;
  height: 90%;
  top: 103%;
  z-index: 9;
  opacity: 0;
  -webkit-transition: opacity 400ms ease-in;
  -moz-transition: opacity 400ms ease-in;
  transition: opacity 400ms ease-in;
  pointer-events: none;
}
/*Starts with string */
[id^="openModal"] {
  position: relative;
  top: 200%;
  z-index: 10;
  opacity:1
}
/*Ends with string */
[id$="Modal1"] {
 background : red
}
[id$="Modal2"] {
 background : yellow
}
<div id="slide1" class="slide">
  <div class="title">
    blah blah
  </div>
  <div class="tooltip">
    <a href="#openModal">
      <img src="ONE.png">
      <p class="tooltiptext">yup</p>
    </a>
  </div>
  <div class="tooltip">
    <a href="#openModal2">
      <img src="two.png">
      <p class="tooltiptext">aye..</p>
    </a>
  </div>
  <div id="openModal1" class="modalDialog">
    <div>
      <a href="#close" title="Close" class="close">X</a>
      <h2>descriptions</h2>
      <img src="ONE.png">
    </div>
  </div>
  <div id="openModal2" class="modalDialog">
    <div>
      <a href="#close" title="Close" class="close">X</a>
      <h2>desc</h2>
      <img src="two.png">
    </div>
  </div>
</div>

Answer №2

nth-of-type(n) targets the specific element type (within the same hierarchy), rather than the class: Therefore, it will identify the nth occurrence of a span element, not the nth element with the .highlighted class.

Answer №3

Instead of using a new style for each nth-of-type selection, I propose a different approach as shown in the code snippet below:

.modalDialog:nth-of-type(1){
    color:yellow;
    top: 203%;
}

A better way would be to set a margin for the modal dialogue to determine its distance from the top like this:

.modalDialog{
    color:yellow;
    margin: 15% auto;
}

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

An error was caught: Cursor blinking not functioning properly in textbox when using JavaScript

Currently, I am in the process of creating an HTML form that involves selecting items from a list box. Once an item is selected, an input text box is generated automatically for entering the quantity of the selected item. I have implemented a feature where ...

Loading jQuery leads to the entire body of the webpage becoming white

As I transition from a welcoming page to a new content page using jQuery's fadeOut/fadeIn and load functions, I encounter an issue where the background, which is black on both pages, suddenly changes to white upon loading the second page. This isn&ap ...

Close the Bootstrap modal upon clicking the "Ok" button in SweetAlert2

I need help with closing my Modal Bootstrap after clicking the Ok button in SweetAlert. I've attempted various methods, but none of them have worked so far. I'm not sure if I'm implementing it correctly, but I've utilized JQuery for thi ...

Exploring the Fusion of Different Styles in Material-UI Using React

I have two different styles that I use in my code. One style is specific to certain components, while the other style is global and used across various components. For example, consider the following file tree: index.tsx -App.tsx -globalConstants.ts In ...

I am facing an issue in Node.js where I am unable to successfully store form data in MongoDB using Mongoose and then retrieve it to display on

I have been working on setting up an application where users can input their personal data, which will later be displayed on the web page. I am utilizing passport for local authentication. The web app generates an HTML form that allows users to enter their ...

Modifying the Hover Color of the Navbar in Bootstrap 4: Tips and Tricks

Looking to customize the hover color of my navbar but struggling to find the right element in the inspect tool. Tried solutions from Stack Overflow with no luck, any suggestions would be appreciated! <!doctype html> <html lang="en> <hea ...

Obtain the contenteditable span's value

I'm facing an issue with a content editable span - I want to extract its value when the post button is clicked, but it's not working as expected. Please note that I am unable to convert the span to a textbox or div, I specifically need the value ...

What could be the reason behind ejs not allowing if else statements when the variable is undefined?

I am attempting to replicate a rather simple example from this question here <div <% if (page_name === 'overview') { %> class="menu__menu-overviewPage menu" <% } %> class="menu"> However, when I try it on a ...

Tips for displaying a hidden div even without JavaScript enabled

I have a scenario where I am using display:none to hide a div, and this div is supposed to be shown only when the user clicks on the + icon. However, I also want to ensure that if JavaScript is disabled, the div is shown by default. How can I achieve this? ...

How to retrieve the dimensions of an image for a specified CSS class with Selenium or PIL in Python

I am interested in using Python to determine the size of specific images. Is it possible to automate this process so that I can identify only images with certain classes? I am unfamiliar with PIL. Could I define the CSS class/name/id after specifying the U ...

How can I utilize jQuery to create a remove button that deletes individual div elements one at a time?

<div class= "button"> <button id="More" type="submit">Click here to add more Parameters</button> <button id="Remove" type="submit">Click here to Remove Parameters</button> </div> <script> var count = 2; ...

Is there a way to automatically adjust the size of an input field after dynamic spans have been

I am looking to replicate the tagging functionality found on StackOverflow when posting a new question. In this case, spans are dynamically added using an input field located next to them. Here is an example: <div> <div id="multiple-span"&g ...

What is the process for eliminating the overflow in this situation?

How can I eliminate the overflow issue with this toggle button? The functionality of this toggle button is simple - you click on it to turn it on and off. I've been struggling to find a solution. Every attempt so far has been unsuccessful. All I wa ...

"(PHP)WordPress produces a dynamic menu that adapts to different screen sizes

I recently created a menu on my WordPress site [ and I'm facing an issue with making it display vertically in a responsive way. The code in header.php is as follows: <!-- header --> <header> <div class="overNavPanel"> ...

Incorporating custom CSS and HTML into the link is essential for enhancing the appearance of KnpMenu

I'm working with the KnpMenuBundle and I need to customize a link that has the route 'uri'=>'#'. How can I achieve this? The desired link should be styled like this: <a href="#" class="js-sub-menu-toggle"> &l ...

Instructions for pulling data from a datatable row and showing it in a modal dialog for editing

I am facing an issue with displaying a dialog for editing datatable rows using Jquery. Despite trying various codes and researching online, I have not been successful in achieving the desired functionality. Any helpful guidance or suggestions on how to ach ...

Tips for adjusting the default container width in MaterializeCSS

The default width of container in MaterializeCSS is initially set to 70%: While the container class is not an integral part of the grid system, it plays a crucial role in organizing content by allowing you to centrally align your page's content. Th ...

Is it possible to incorporate a variable into an object?

If you are wondering whether it is possible to utilize a variable in the following scenario, the reason for my inquiry is connected to the potential of utilizing an input element to dynamically modify the variable: var str = "pineapples" var cost = { pine ...

Tutorial on making a pop-up appear on click using jQuery

Below are the attempts I have made: Using jQuery: $("#type_name").click(function(){ $("#add_form").fadeIn(1000); $("#involved_name").val(""); positionPopup('#add_form'); $("#involved_name").focus(); }); Usin ...

What is the best approach to choose the thead element from a dynamically created table?

My table in JavaScript/JQUERY is dynamically constructed. Once the table is created, I am attempting to target the thead element. Here is a snippet of my code: $(document).ready(function(){ buildTbl(); }); function buildTbl() { var tbl = '< ...