How to target the last child in Flexbox using CSS order property

Is there a way to correctly detect the last child after rearranging divs using flex order? Even though I have set the order in the CSS, it is still recognizing the last child based on the DOM tree. Since the items are dynamic, we can't rely on nth-child. How can I target the actual last child when they have been reordered with flex order? There may be multiple items that need to appear at the top. I'm looking for a solution using either CSS or JavaScript.

    <div class="mainWrap">
     <div class="item">first</div>
     <div class="item">second</div>
     <div class="item orderFirst">third</div>
     <div class="item">fourth</div>
     <div class="item orderFirst">fifth</div>
     <div class="item">Six</div>
     <div class="item orderFirst">Seven</div>
    </div>

    <style>
    .mainWrap {display:flex; flex-direction: column;}
    .mainWrap .item {order:2; border-bottom:1px solid #ccc}
    .mainWrap .item:last-of-type {border-bottom:none;}
    .mainWrap .item.orderFirst {order:1;}
    </style>

Answer №1

To achieve the desired effect of reversing borders, you can implement the following CSS code:

.element {
  order: 2; // double check this value
  border-top: 1px solid #ccc; // apply a top border to all elements with the class .item
}

.item.orderFirst {
  order: 1;
  border-top: none; // remove the top border for the first visually displayed element
}

Assuming your scenario is similar to the provided example, utilizing this solution should fulfill your requirements.

Answer №2

If you're adamant about approaching it in this manner, one way to achieve the desired result is by utilizing JavaScript to locate the last element that does not carry the orderFirst class and apply styling to it. Nevertheless, this method may be considered as a misuse of the order property.

const container = document.querySelector(".container");

const elements = [...container.children];
const filteredElements = elements.filter((ele) => !ele.classList.contains("orderFirst"));

const lastElement = filteredElements.pop();
lastElement.style.setProperty("border-bottom", "none");
.container {
  display: flex;
  flex-direction: column;
}

.item {
  order: 2;
  border-bottom:1px solid #ccc;
}

.item.orderFirst {
  order: 1;
}
<div class="container">
  <div class="item">first</div>
  <div class="item">second</div>
  <div class="item orderFirst">third</div>
  <div class="item">fourth</div>
  <div class="item orderFirst">fifth</div>
  <div class="item">Six</div>
  <div class="item orderFirst">Seven</div>
</div>

Answer №3

Do you intend to remove the border from the last visible item within the mainWrap div container, rather than deleting the last element entirely?

.mainWrap {display:flex; flex-direction: column;}
.mainWrap > div { order:2; border-bottom: 1px solid #ccc }
.mainWrap > div:nth-last-child(2) { border-bottom:none; }
.mainWrap > div:last-child { order:1; background: red;}
<div class="mainWrap">
     <div class="item">first</div>
     <div class="item">second</div>
     <div class="item">third</div>
     <div class="item">fourth</div>
     <div class="item orderFirst">fifth</div>
 </div>

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

JSON is functioning properly, however, I am encountering difficulties trying to make JSONP work

Hello, I have been working on trying to make some JSON work properly. Here is the code snippet that I have written: $.getJSON("/calendar/event/test", function(data) { $.each(data, function(i,item){ $('#test').append('<li>' ...

Observing data retrieved via ajax in Internet Explorer 9

Is there a way to view data returned by an ajax script in Internet Explorer 9? For instance, Firefox has the Firebug 'All' tab with a 'Response' Sub Tab that displays data returned by an Ajax Script. How can this be done in Internet Ex ...

Create a function that is determined by a specific hyperlink

I am looking to create a universal function that can work with all types of links on my pages. For example: If I click on a .pdf link, it should open a PDF viewer. If I click on a .mp3 link, it should open an audio player, and so on. However, I want to ...

Have I got it all wrong with the way Controllers communicate with Services and how I conduct unit testing?

Currently, I am in the process of developing an AngularJS application and I want to ensure it follows best practices. When making external API calls using either $resource or $http, I always do this within a separate Service or Factory. However, I am unsur ...

Guide to creating a dynamically populated dropdown menu using jQuery

I am in search of a method to select a dropdown value dynamically after it has been loaded with JSON values. My goal is to perform this action once the loading process is finished. Below is an illustration of what I am aiming for: $.ajax({ url:"/ech ...

Sequence of background colors not altering as intended

After clicking a button, I included the following jQuery code in a code snippet on my WordPress site: jQuery("#cf_course_planner tr").css("background-color", "#f66"); jQuery("#cf_course_planner tr:eq(0)").css(& ...

XState: linking together multiple promises seamlessly without needing intermediate states

After reading the Invoking Multiple Services section, it seems that despite being able to invoke multiple promises, they appear to be triggered without waiting for the previous one to complete in my own tests. // ... invoke: [ { id: 'service1' ...

Jquery Autocomplete with Dynamic Text Updates

I am having an issue with the autocomplete feature on my website. Whenever I enter a search and select an option, the text in the box changes to the selected value instead of keeping what I originally entered. How can I prevent this from happening? <sc ...

Using Selenium Webdriver to simulate pressing ‘ctrl + t’ for opening a new tab in Javascript

I have been utilizing Selenium Webdriver to conduct testing on a web application that is currently undergoing development. Despite having developed multiple tests already, I am facing challenges in attempting to open new tabs within the window controlled b ...

After successfully uploading a file, refresh the file list using AngularJS, DropzoneJS, and JSON

In my "file manager page," I am using DropzoneJS for uploading files and AngularJS ng-repeat to display all the saved files. The file list is retrieved through an ng-controller that uses an http.get request to fetch a JSON file with the file list. When Dr ...

When modifying v-model directly in a Vue instance, the Vuetify component on the screen may not update its displayed value

Within my Vue component example (utilizing Vue 2 and Vuetify 1), I have implemented an input field for user data entry. However, I am looking to programmatically alter the input field's data to a specific value. For instance, in this scenario, I aim ...

Images are not being shown on the desktop carousel display

I am encountering an issue with my carousel. When I attempt to decrease the height of the carousel, the entire image is not being displayed properly (on desktop), although it works fine on mobile devices. My goal is for the carousel not to take up the ent ...

Ensure that the CSS grid has a minimum span of 3 columns, and if a class object is the only item in a grid row, it

I am currently utilizing CSS grid to create the layout for my web application. I need to specify that a particular class should have a minimum grid-column span of 3 frames and extend across the entire grid row if it is the only object in that specific row. ...

What are the best ways to enhance performance when making multiple require() calls?

I am in the process of developing a bot for my company's Mattermost server (similar to a Discord bot). Each command for the bot is housed in its own file, making it easier to manage and maintain. When a user wants to execute a command, they must send ...

How to programmatically close a colorbox using an ASP.NET form's Submit button within an iframe

I've looked at other similar questions, but I just can't seem to make this work. I want to close the colorbox iframe when a .NET Button form within the iframe is clicked. <script type="text/javascript"> $(document).ready(function() { ...

Having trouble storing the message ID within the user Object using Mongoose

My goal is to add a message to the messages property (an array) of a user object using Mongoose. However, I encounter an error when trying to save the user (user.save()). To troubleshoot, I added three console.log statements in the code below. Can anyone h ...

How do I get my navbar to change color using a JavaScript scroll function?

I've been struggling with changing the color of my navbar when it's scrolled. I have some JavaScript code at the bottom that should handle this, but for some reason, it's not working as expected. I've double-checked that my bootstrap CD ...

codeigniter view is facing issues with ajax functionality

Working with CodeIgniter, I am attempting to send form data to a controller using AJAX. However, I encountered an issue where the content of $this->input->get_post("sku_qty") in the controller is NULL. Additionally, there seems to be an error in the ...

Post request in ExpressJS fails to redirect user after submission

My current project involves developing a web application using NodeJS and ExpressJS. One issue I am encountering is with the login functionality on my website. After filling out the login form, the user's credentials are sent via a post request to the ...

Connecting a CSS file with an HTML document

Having an issue with my CSS. For instance, here is a snippet from my CSS file .readmore { font-style: italic; text-decoration: none; color: #509743; padding-left: 15px; background: url(../images/more.png) no-repeat 0 50%; } .readmore: ...