The not:first-child selector targets all elements except for the first one in the sequence

This is a simple js gallery. I am using not:first-child to display only one photo when the page is loaded. However, it does not hide the other photos. You can view the gallery at this link: (please note that some photos are +18).

My objective is to hide all photos except for the first one when the page loads. After clicking on the buttons, it should display the next or previous photos normally. I'm confused about what I wrote incorrectly in terms of syntax and logic. The JavaScript is working, but the CSS is not.

var slideIndex = 1;
showDivs(slideIndex);

function plusDivs(n) {
  showDivs(slideIndex += n);
}

function showDivs(n) {
  var i;
  var x = document.getElementsByClassName("mySlides");
  if (n > x.length) {
    slideIndex = 1
  }
  if (n < 1) {
    slideIndex = x.length
  }
  for (i = 0; i < x.length; i++) {
    x[i].style.display = "none";
  }
  x[slideIndex - 1].style.display = "inline";
}
.mySlides not:first-child {
  display: none !important;
}
<div class="btt"><button class="w3-button" onclick="plusDivs(-1)">❮ Prev</button> <button class="w3-button" onclick="plusDivs(1)">Next ❯</button></div>
<div class="w3-content">

  <div><img class="mySlides" src="/wp-content/images/1.jpg" alt="Kaley Cuoco nackt"></div>
  <div><img class="mySlides" src="/wp-content/images/2.jpg" alt="Kaley Cuoco nackt"></div>
  <div><img class="mySlides" src="/wp-content/images/3.jpg" alt="Kaley Cuoco nackt"></div>
  <div><img class="mySlides" src="/wp-content/images/4.jpg" alt=Kaley Cuoco nackt ""></div>
  
  <!-- More image elements here -->

  <div><img class="mySlides" src="/wp-content/images/21.jpg" alt="Kaley Cuoco nackt "></div>
  <div><img class="mySlides" src="/wp-content/images/22.jpg" alt="Kaley Cuoco nackt "></div>

</div>

Answer №1

There are two issues to address here: a syntax error and a logic error. The .mySlides elements are inside a div, which means they do not share the same parent, so using :first-child will target all of them. Additionally, it is important to remove the use of !important as it takes priority over inline styles and may cause conflicts with JavaScript functionality.

To resolve this, you need to ensure that the .mySlides elements are within the same div (sharing the same parent) and correct the CSS syntax for :not(), like so:

var slideIndex = 1;
showDivs(slideIndex);

function plusDivs(n) {
  showDivs(slideIndex += n);
}

function showDivs(n) {
  var i;
  var x = document.getElementsByClassName("mySlides");
  if (n > x.length) {
    slideIndex = 1
  }
  if (n < 1) {
    slideIndex = x.length
  }
  for (i = 0; i < x.length; i++) {
    x[i].style.display = "none";
  }
  x[slideIndex - 1].style.display = "inline";
}
.mySlides:not(:first-child) {
  display: none;
}
<div class="btt"><button class="w3-button" onclick="plusDivs(-1)">❮ Prev</button> <button class="w3-button" onclick="plusDivs(1)">Next ❯</button></div>
<div class="w3-content">

  <div><img class="mySlides" src="https://lorempixel.com/400/400/" alt="Kaley Cuoco nackt"><img class="mySlides" src="https://lorempixel.com/400/420/" alt="Kaley Cuoco nackt">
    <img class="mySlides" src="https://lorempixel.com/420/400/" alt="Kaley Cuoco nackt">
    <img class="mySlides" src="https://lorempixel.com/420/420/" alt=Kaley Cuoco nackt ""></div>

</div>

Answer №2

There appear to be some issues in your code.

Firstly, the attempt to identify whether or not .mySlides is a first child is flawed. Since each .mySlides element is contained within its own div, it will always be the first and only child. A different approach would be targeting .w3-content > div to select all direct descendants of the container.

Furthermore, errors are present in the pseudo-selectors. The correct syntax should be :not(selector), without any space preceding it. In this case, it should be written as :not(:first-child).

To summarize, the proper CSS code should read:

.w3-content > div:not(:first-child) { display: none; }

An alternate method could involve hiding all slides initially, then displaying the first one:

.w3-content > div { display: none; }
.w3-content > div:first-child { display: block; }

Remember to adjust your JavaScript accordingly to target the correct elements by adding parentElement, for example:

x[i].parentElement.style.display = "none";

and:

x[slideIndex - 1].parentElement.style.display = "inline";

Answer №3

As mentioned,

Shouldn't it be :not? And there shouldn't be any space between .mySlides and :not. A space indicates children.

The reason it's not working is because all your images are the first child.

You should use the selector

div.w3-content div:not(:first-child) img.mySlides
instead.

Concept

  • Selecting all divs within .w3-content except for the first child. I am using div directly but you should consider adding a class and utilizing it as div.className
  • Hide the mySlides images within them.

Example:

div.w3-content div:not(:first-child) img.mySlides {
  display: none;
}
<div class="btt"><button class="w3-button" onclick="plusDivs(-1)">❮ Prev</button> <button class="w3-button" onclick="plusDivs(1)">Next ❯</button></div>
<div class="w3-content">

  <div><img class="mySlides" src="/wp-content/images/1.jpg" alt="Kaley Cuoco naked"></div>
  <div><img class="mySlides" src="/wp-content/images/2.jpg" alt="Kaley Cuoco naked"></div>
  <div><img class="mySlides" src="/wp-content/images/3.jpg" alt="Kaley Cuoco naked"></div>
  <div><img class="mySlides" src="/wp-content/images/4.jpg" alt=Kaley Cuoco naked ""></div>
  <div><img class="mySlides" src="/wp-content/images/5.jpg" alt="Kaley Cuoco naked"></div>
  <div><img class="mySlides" src="/wp-content/images/6.jpg" alt="Kaley Cuoco naked"></div>
  <div><img class="mySlides" src="/wp-content/images/7.jpg" alt="Kaley Cuoco naked"></div>
  <div><img class="mySlides" src="/wp-content/images/8.jpg" alt=Kaley Cuoco naked ""></div>
  <div><img class="mySlides" src="/wp-content/images/9.jpg" alt="Kaley Cuoco naked"></div>
  <div><img class="mySlides" src="/wp-content/images/10.jpg" alt="Kaley Cuoco naked"></div>
  <div><img class="mySlides" src="/wp-content/images/11.jpg" alt="Kaley Cuoco naked"></div>
  <div><img class="mySlides" src="/wp-content/images/12.jpg" alt=Kaley Cuoco naked ""></div>
  <div><img class="mySlides" src="/wp-content/images/13.jpg" alt="Kaley Cuoco naked"></div>
  <div><img class="mySlides" src="/wp-content/images/14.jpg" alt="Kaley Cuoco naked"></div>
  <div><img class="mySlides" src="/wp-content/images/15.jpg" alt="Kaley Cuoco naked"></div>
  <div><img class="mySlides" src="/wp-content/images/16.jpg" alt=Kaley Cuoco naked ""></div>
  <div><img class="mySlides" src="/wp-content/images/17.jpg" alt="Kaley Cuoco naked"></div>
  <div><img class="mySlides" src="/wp-content/images/18.jpg" alt="Kaley Cuoco naked"></div>
  <div><img class="mySlides" src="/wp-content/images/19.jpg" alt="Kaley Cuoco naked"></div>
  <div><img class="mySlides" src="/wp-content/images/20.jpg" alt="Kaley Cuoco naked "></div>
  <div><img class="mySlides" src="/wp-content/images/21.jpg" alt="Kaley Cuoco naked "></div>
  <div><img class="mySlides" src="/wp-content/images/22.jpg" alt="Kaley Cuoco naked "></div>

</div>

Answer №4

Check out this interesting article on CSS Tricks

Here is an example of how it could be implemented:

.mySlides:not(:first-child){
    display:none !important;
}

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

What is the best way to initiate a loading event once the window has completely finished loading?

I am currently working with a framework (vue.js) that inserts images when there is a page change, without actually refreshing the entire page. When the page is loaded directly, I can implement a loading screen using the following code: loading = true; $(w ...

Is there a way to implement infinite scrolling in my MUI DataGrid component?

Hey there! I have a custom component that needs to have infinite scrolling added to it. I tried looking into MUI DataGrid for solutions, but didn't have much luck implementing anything successfully. Currently, I'm fetching data using GraphQL and ...

Issue with AngularJS factory $http.get request receiving HTML files as response

Could someone please explain why I keep receiving an HTML file as a return from my Angular factory? This is the route on my backend: function ensureAuthenticated(req, res, next) { if (!req.headers.authorization) { return res.status(401).send({ mess ...

Tips for Displaying and Concealing Tables Using Radio Buttons

Does anyone know how to refactor the jQuery code to toggle between two selection options (Yes and No)? This is the jQuery code I have tried: $(document).ready(function() { $("#send_to_one").hide(); $("input:radio[name='decision']").chan ...

What causes the size of text to change when I zoom in my browser?

As I work on my website, I am facing a challenge with maintaining consistent text size as the page scales. For example: p { width: 10%; height: 10%; background-color: rgb(0,0,0); top: 50%; left: 50%; position: fixed; color: rgb(255,255,25 ...

What's the Hold-Up with IntersectionObserver in Brackets?

As a novice in the world of web development, I have been utilizing Brackets as my main tool. Recently, I've encountered a few hurdles specifically related to javascript. One issue I'm facing is trying to implement lazy loading on a div containing ...

Integration of elFinder with TinyMCE 4

Has anyone attempted to integrate elFinder into the latest version (4b1) of TinyMCE? The previous implementation seems to be not working. If you have any snippets or tips, please share. Thank you very much. ...

Design a dynamic advertisement page using HTML and Bootstrap to ensure responsiveness

I am currently working on a marketing project where I want to give clients the ability to customize their landing page with image ads in a layout that looks something like this: -------------------------------------------------------------------------- | ...

Exploring the functionality of a personalized hook using the useSelector method

I have developed a custom hook and I am in the process of testing it independently. However, I am encountering an issue where I need to wrap the hook inside a Provider to proceed further. The error message I am getting is displayed below: Error: could not ...

What is the best way to add a border to the <map coordinates>?

Is there a way to add a border around the active link section of an image defined by coordinates? I have been able to show an outline when the user clicks using outline-color and href, but now I need a default border around the specified coordinates. I am ...

media query for css on windows 7 mobile devices

Is there a way to apply CSS media queries specifically for Windows phone 7? I have attempted the following code without success: @media only screen and (max-width: 480px) and (min-width: 5px) { // css here } Any advice or guidance would be greatly appr ...

Having trouble exporting an object from a different JavaScript file in Node.js

I have been attempting to make the getCurrentSongData function retrieve the songdata object passed in from the scraper. However, I am encountering the following output: ******************TESTING**************** c:\Users\(PATH TO PROJECT FOLDER)& ...

Tips for keeping a dynamic height div in place without affecting surrounding elements

HTML: <div id="autocomplete" hidden></div> <input type = "button" id = "search" value = "Search"> The autocomplete div contains dynamically generated input tags created by jQuery. These input tags cause the button to be shifted down the ...

Encountering ReferenceError while running production build with Webpack

After upgrading to webpack 3.10.0 and Babel 6.26, I managed to fix my dev build but encountered issues with the prod build that I can't seem to resolve. This is the error message I am seeing: ERROR in ./src/index.js Module build failed: ReferenceErr ...

What is the reason behind the Typescript compiler not converting .ts files to .js files automatically?

Displayed below are the folders on the left showcasing my Typescript file in /src (blue) compiled into Javascript in /dist (purple) using tsc. https://i.stack.imgur.com/7XNkU.png In the source file on the left, there is a reference to a .ts module file t ...

d3.json is unable to parse a value of 'Infinity

My goal is to retrieve data from an SQLite database and convert it into JSON format for use with d3.js in order to create a graph. I have successfully obtained this data in JSON format using the following URL: http://localhost:8085/SQLQuery/?date1=2019-03 ...

Inconsistencies with AJAX performance

Hey there, I'm a newbie and absolutely loving this site! Currently diving into the world of JavaScript and have been through numerous tutorials. However, I seem to be struggling with getting Ajax to work. Recently stumbled upon this code snippet on ...

Setting up environments utilizing restify

Having experience with gulp, I am well-versed in setting up a distinct configuration for each environment within a single configuration file. This allows for running gulp dev to initiate the server using the development environment configuration, gulp stag ...

Child_process module spawn method in Node.js

When I attempt to play an audio stream using the mpg123 command, everything works perfectly fine. I have also implemented a method to terminate the process successfully. However, I am struggling to retrieve output from the executed command. Despite follow ...

What is the process for syncing ng-model with external data sources?

Here is a question that I have pondered: Let's consider the HTML code snippet below: <div id="container" ng-controller="Controller"> <my-tag ng-model="values"></my-tag> </div> Now, take a look at the controller defined a ...