Avoid selecting the hidden element using the :first-child pseudo-class

I've encountered an issue while using Sortable.js. The problem is as follows:

I have a list of items that need to be sorted, with the first element wider than the rest.

To achieve this, I've applied the following CSS:

#produto_img {
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -ms-flex-wrap: wrap;
      flex-wrap: wrap;
}

#produto_img > li {
  height: 100px;
  margin: 0 5px;
}

#produto_img > li:not(:first-child) {
  flex: 1;
}

#produto_img > li:first-child {
  width: 100%;
  height: 200px;
  margin-bottom: 10px;
}

Everything works perfectly when dragging any item except for the first one. Even if I switch positions with the first item, it still works smoothly.

The resulting markup looks like this:

https://i.sstatic.net/7GTtn.png

However, when I try dragging the first item, the styling gets messed up because the plugin adds another item in its place and hides it with display: none. As a result, the :first-child selector no longer applies, causing the loss of styling.

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

Check out this gif to see the issue in action.

https://i.sstatic.net/xwRwQ.gif

My initial thought was to somehow avoid styling the first element if it's hidden with display: none, or use something like :first-child:visible. However, these solutions aren't working. Any suggestions for a workaround?

Answer №1

Unfortunately, the use of the :visible pseudoclass is still unique to jQuery. However, one workaround is to manage the hiding by adding or removing a specific class.

Afterwards, it becomes a matter of making adjustments:

li.hidden {
    display: none;
}

li:not(.hidden) {
    width: 100%;
    height: 200px;
    margin-bottom: 10px;
}

li:not(.hidden) ~ li {
    width: auto;
    height: auto;
    margin-bottom: 0;
    flex: 1;
}

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

Styling extracted content using headless browsing algorithm

Is there a way to format the scraped text from multiple elements on the page for use elsewhere? I have JavaScript code that can loop over the elements, add their text to an array, and turn it into a string, achieving the desired formatting. How can I inc ...

Intrusive JQuery Code Incorporating Itself into SharePoint 2010's HTML Markup

There seems to be an issue with my content editor web part wherein the menu, which is clickable and hoverable due to the jQuery installed, is causing some unexpected behavior. Whenever the menu is clicked or hovered over, it changes the content in the adja ...

MongoDB does not always insert the complete array

For inserting 499 entries into a subdocument from an array, I have the following code snippet: var bodyParser = require('body-parser'); var express = require("express"); var path = require("path"); var session = require('express-session&apo ...

Can one utilize the retrieval of past history or document state as a form of navigation within a browser?

Concerned that I may not receive a response from Stack Overflow due to lack of response on my previous question. While not necessary to fully read the previous question, it may provide context for my current inquiry. My company offers an HTML document bui ...

Showing identification on the drop-down list

Within my dropdown menu setup, I am aiming to achieve a specific functionality. When the user clicks on sub menu, I want the title of sub menu to display in the Menu space as illustrated below. What steps can be taken to accomplish this? UPDATE: After fu ...

The use of refs on Function components in SVG is not supported. Any attempts to access the ref will result in an

I am currently working on integrating a navigation bar component using an SVG image in Next.js. While attempting to import and utilize the SVG image, I encountered an error that I couldn't resolve. I tried using Emotion JS to create and use the SVG, ...

Guide on converting a buffer to a PDF file using front-end technologies

I utilized Puppeteer to create a buffer containing pdf data and then transmitted it to the frontend application. Below is the code snippet for the backend API: exports.getPDF = async (req, res) => { const { content } = req.query; const browser = ...

Angular js - The PUT request to the service is missing the URL

Below is the script I am working with: var shipResource = $resource('http://localhost\\:8083/consignments/:cId', null, {ship: {method: 'PUT'}}); $scope.shipConsignment = function() { if($scope.selectedConsignment != null) ...

Stop ajax request when select2 is clicked

Is there a way to change the ajax call behavior in select2 drop down items so that it only retrieves data when I start typing in the search box, and not on click of the element? Your guidance on this issue would be highly appreciated. $("#ddlItems").sel ...

What is the best way to extract all td[3] elements from tr tags using Selenium's Xpath feature in Python?

Looking at my webpage HTML, it appears as follows: <table class="table_type1" id="sailing"> <tbody> <tr> <td class="multi_row"></td> <td class="multi_row"></td> <td class="multi_row"> ...

Tips for inserting a line break in a script

Hello, I need some assistance with creating a typewriter effect using JS, CSS, and HTML. Everything seems to be working fine except when I try to add a new line of text, it doesn't display properly. var str = "<p>I want to put text here then ...

How can I add page transitions to my simple HTML website?

Working on an internal website project for my office using vue.js has been a rewarding experience. I appreciate how easily it allows me to manage multiple pages with dynamic content and seamless transitions. Unfortunately, my IT department is hesitating to ...

Error: Attempted to save data with mongoose but encountered an Uncaught ReferenceError; `amadeus` is not defined at line 16,

When attempting to store data in mongoDB using mongoose, an error was encountered. The code snippet from index.js is as follows: const mongoose = require('mongoose') mongoose.connect('mongodb://127.0.0.1:27017/myapp') .then(() => ...

Issues have been encountered with the functionality of the script (AJAX or JS) in IE8

I'm currently troubleshooting a js/ajax script hosted at A recent complaint notified me that it's not functioning properly on IE8. The onchange event seems to be working, however the subsequent menu isn't being populated. Can anyone assis ...

What is the best way to monitor a document variable that is set after a component has been

Is there a way to access the variable document.googleAnalytics, added by Google Tag Manager, for A/B testing on a Vue.js page? I attempted to initialize the variable in both the mounted and created methods of the component, but it returns as undefined: ex ...

Is there a mechanism that can convert a string into an equation format?

console.log(Math.sqrt(4)); let str = 'Math.sqrt(4)'; console.log(str); Can the 2nd console log display the value 4 instead of the string 'Math.sqrt(4)'? Is there a specific function that can achieve this? ...

Is there a way to have one element automatically expand when another is selected?

I'm currently utilizing the date and time picker from . However, I need some assistance with the current setup. Currently, the date and time pickers are in separate input fields. In order to select a date, I have to click on the Date input field, and ...

I possess an assortment of objects and I must retrieve specific information about them based on two data points

Managing a collection of items can be challenging, especially when filtering based on specific data points. For example, let's say we have a collection of action objects like {name: Detail}, {name: Spec}... and two data points determining which action ...

Leveraging Angular interpolation with the "ngIf" directive

In my table, I have a specific requirement for the first column to change color based on a variable within my class. For instance, within my DisplayProject.ts class, there is a property named indexOfColor DisplayProject.ts export class DisplayProject impl ...

Calculating the sum of values in a JSON array using a specific parameter in Typescript

A flat JSON array contains repetitive identifier, categoryId, and category: data: [ { "identifier": "data", "categoryId": "1", "category": "Baked goods", "product": "Aunt Hattie's", "price": "375" } ...