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

Adjusting the grid for various mobile screen sizes

I am currently working on designing a user interface (UI) for mobile screens that includes a header, grid, and buttons. I want the UI to look consistent across all mobile devices. Here are screenshots from various mobile screens: Samsung S5 Pixel 2 XL I ...

Tips for making content vanish or remain concealed behind a see-through header during page scrolling

I made a JavaScript fiddle http://jsfiddle.net/claireC/8SUmn/ showcasing a fixed transparent header. As I scroll, the text scrolls up behind it. How can I make the text disappear or be hidden behind the transparent div when scrolling? Edit: I should also ...

When I try to use Node.js and Express, I encounter an issue where I receive the

I recently developed a basic application. Everything was running smoothly until I decided to organize the code using an MVC template. However, now when you visit localhost:3000/add-service, you are greeted with a "Cannot Get /add-service" error message. W ...

Adjust the slide count accordingly when navigating to a particular item within the Bootstrap 3 Carousel

I made adjustments to the standard Bootstrap 3 Carousel so that it can navigate to a specific slide when the URL matches #. Although this feature is working, I am facing an issue with my pager-text not updating correctly when jumping to a specific slide. T ...

Troubleshooting a unique CSS bug in jQuery mouseover functionality

Check out this pen: https://codepen.io/anon/pen/eKzEVX?editors=1111 I recently created a Form Select in Laravel: {!! Form::select('status_id', $statuses, $post->status_id, ['class' => 'form-control post-sub-items-label &apo ...

Navigating through a Single Page Application (SPA) can be achieved without

Can the CSS (Level 4) @document rule handle URLs that have the # symbol or include query parameters? For those who are not familiar with this topic Currently, only Firefox supports this feature with the @-moz-document prefix. If other browsers start supp ...

Tips for clearing a text box on an AngularJS page when clicking a link within the page

On my HTML page, I have a search box and various category links. When a user enters text in the search box, I want to display: p in Products | filter {searchedText} If a user clicks on a category link, I want to display: p in Products | filter {categor ...

Creating a cascading layout with React Material-UI GridList

Can the React Material-UI library's GridList component be used in a layout similar to Pinterest's cascading style? I have utilized Material-UI Card components as children for the GridList: const cards = props.items.map((item) => <Card ...

Choosing HTML components using Selenium in the Whatsapp application

Apologies in advance for any language errors. I am currently working on a script using selenium and python to identify all messages with photos in a group. The structure of all message boxes is the same. Repository: # feel free to check out my code if you ...

React Bootstrap always displays tooltips

I have integrated react-bootstrap into my project. I am currently attempting to keep a tooltip always displayed, but I am facing some challenges in achieving this. Below are the approaches I have tried so far: <Card style={{width: '10rem'}} ...

Leveraging the $dirty property in AngularJS to determine if any changes have been made to a form

Recently, I've been attempting to determine if my form is being edited by monitoring certain fields. I've come across $dirty as a potential solution for this task, but unfortunately, I'm struggling to identify what exactly I'm overlooki ...

Utilizing ng-click within a tooltip

I've implemented Angular Bootstrap UI and successfully added a tooltip to my project. Snippet of HTML: <div ng-app="helloApp"> <div ng-controller="helloCtrl as hello"> <a tooltip-trigger="click" tooltip-placement="bottom" uib-t ...

iOS 10's autofocus feature experiencing difficulties in focusing on input

While using an application on my desktop or Android device, I have noticed that the input focus works perfectly fine. However, when I try to run the same application on iOS 10 Safari, the input focus does not seem to be working. It is worth noting that I ...

Transform an array into an object with assigned key names

How can we transform the following array: [2019,2020,2021] into the format: { 0: {year:2019}, 1: {year:2020}, 2: {year:2021} } ...

Synchronous execution in Node.js: Best practices for coordinating tasks

While Node.js is known for its asynchronous nature, I am seeking to perform tasks in a sequential manner as outlined below: 1. Make an API request > 2. Convert the body from XML to JSON.stringify format > 3. Pass the string to a template. request.g ...

Obtain the complete file path using JavaScript

Imagine you have a web application utilizing NodeJS and ReactJS, both working on your local machine. In this setup, NodeJS is capable of accepting files selected from the ReactJS GUI via a simple file upload feature, like so: <input type="file" id="fil ...

The useEffect alert is triggered before the component is re-rendered

Can someone help me understand why the HP in the code below is displayed as "1" when the alert is thrown, and only set to "0" after I confirm the alert? Shouldn't the component be rerendered before the alert is thrown so that it has already been displ ...

Tips for eliminating the left and bottom border in a React chart using Chart.js 2

Is there a way to eliminate the left and bottom borders as shown in the image? ...

Expand the container as the child element grows using flexbox

Check out this layout: <div class="wrapper"> <div class="middleman"> <div class="inner-child"> /* Child with the ability to expand height */ </div> </div> </div> Imagine the wrapper has an initial hei ...

Load jQuery DataTable when the event changes

I have implemented a script that loads the DataTable when the page is ready: function initializeDataTable(serviceUrl) { var $dataTable = $('#example1').DataTable({ "ajax": serviceUrl, "iDisplayLength": 25, "order": [[2, "asc"]], ...