The ability to select multiple last child elements

Is it possible to style the last instances of .first.bb before each .first.a element without altering the HTML structure? Keep in mind that DIV.first elements are dynamically added and may change. Also, the last .first.bb within #wrapper should already be styled accordingly.

http://jsfiddle.net/MqPLx/

html

<div id="wrapper">
    <div class="first">first</div>
    <div class="first bb">first second</div>
    <div class="first bb">first second</div>
    <div class="first bb">first second</div> <!-- <- this should be selected -->
    <div class="first a">first</div>
    <div class="first a">first</div>
    <div class="first a">first</div>
    <div class="first bb">first second</div>
    <div class="first bb">first second</div>
    <div class="first bb">first second</div>
    <div class="first bb">first second</div> <!-- <- this should be selected -->
    <div class="first a">first</div>
    <div class="first a">first</div>
    <div class="first a">first</div>
    <div class="first bb">first second</div> <!-- <- this should be selected -->
</div>

css

 .first.bb:last-child {
    color:red;
}

 .first.bb + .first.a {
        color:red;
}

Answer №1

To select the previous sibling element, you can only achieve this with JavaScript currently. You can refer to an example at http://jsfiddle.net/iamnotsam/t4BUW/

// Using jQuery
$('.bb + .a').prev().addClass('last-first-bb'); 

Alternatively, you can use plain JavaScript

// With pure JavaScript
var elements = document.querySelectorAll('.bb + .a'),
    className = 'last-first-bb';

Array.prototype.forEach.call(elements, function(el, i){
  var el = el.previousElementSibling;
  if (el.classList)
    el.classList.add(className);
  else
    el.className += ' ' + className;
});

This code snippet helps in selecting the specific element you need. Selecting the last child of #wrapper using CSS is simple and doesn't require any tricks.

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

Struggling with CSS Flexbox when it comes to organizing rows and combining Table and Table Cells

I've been struggling to fix a CSS flex problem for hours. My goal is to make sure that the three boxes in each row have the same height. While I'm familiar with using flex, I suspect that floats may be the root of the issue. Even after clearing t ...

Is it possible to have images automatically resize to fit the content div without becoming distorted?

I have created a webpage that I really enjoy, but now I need to ensure it supports smaller resolutions like 1020X768. However, for those with larger monitors, I want to take advantage of the extra space by setting a minimum width that can grow with the scr ...

angular js validation is not working properly

Currently, I am working on a basic HTML page that requires validation using AngularJS. I imported the HTML file with AngularJS in the following manner: <title>Student Registration Form</title> <script src="https://ajax.googleapis.co ...

Angular: limit components height to fit screen dimensions

Is there a way to adjust the max-height and max-width of all Angular components to fit the screen size without setting them to 100% from the html down to each component? I want to prevent nested components from exceeding the screen size. ...

Generating HTML files using Express and body-parser

I need assistance with incorporating user login data into an existing HTML document using Express. The input from the username field should be displayed at the top of the page. How can I access specific elements in the HTML document through Node or Express ...

Reset input value when adding or removing inputs dynamically

Currently, I have an input element that has the capability to clear its value when a button is clicked. Additionally, this input can dynamically add or remove input elements. However, I am facing an issue where after adding an input element, the clear butt ...

Is there a way to automatically extend my content to fill the space on the page below the Material UI AppBar?

I am currently using React and Material UI React to develop my application. My goal is to implement the AppBar component with content underneath, without causing the entire page to scroll. I want the content to occupy the maximum remaining height and the f ...

Ways to incorporate services into functions within Angularjs

I'm struggling to grasp the concept of dependency injection in AngularJS. Currently, I am attempting to inject $interpolate and $eval into my controller. However, after consulting the documentation, I realized that my lack of foundational knowledge i ...

Ways to delete an image from a batch file uploading feature

I encountered an issue with my multiple image upload function that includes preview and remove options. When I select 4 images and preview them correctly, removing 2 from the preview still results in uploading all 4 images to the database. $(document).rea ...

Using PHP to utilize logical OR or AND operators when adding a class to the global navigation's list items

I am currently working on implementing a global navigation bar using PHP on all pages. The PHP code is also being used to add a class and display the current page. The main challenge I am encountering involves selecting the parent navigation item. When on ...

Include the <script> tag in the HTML code for an iframe without running it

Currently, I am working on an HTML code that involves memory for an Iframe. Interestingly, whenever I use the append function, it not only executes the code but also carries out its intended task. html = $(parser.parseFromString($("#EHtml").val(), "text/h ...

Having trouble with Vue router functionality

I've attempted troubleshooting my router countless times, but it still won't work properly. I'm unable to navigate or switch to different pages in my Vue project. Despite following all the steps outlined in this link , nothing seems to chang ...

Tips for increasing the number of hide/show div toggles with unique classes

I am currently working on a project that involves toggling the visibility of different div classes. The toggle status is saved in local storage to ensure that the setting remains the same even after refreshing the page. Recently, I attempted to create ano ...

Update the formatting of the dates in the dateRangePicker

Utilizing a dateRangePicker, I am receiving dates in the format "mm-dd-yyyy". Below is my HTML code: <input type="text" name="TextDate" value="10/24/1984" /> Here is the script being used: <script> $(function() { $(&apos ...

Creation of tables in XSLT using dynamic techniques

My objective is to convert an XML file into an HTML file, with the contents displayed in a table format. However, I am struggling to figure out how to ensure that only four elements are shown per row. members.xml: <?xml version = "1.0"?> ...

As the page is scrolled upwards, the custom cursor's location shifts accordingly

In developing a custom hover cursor for my video div, I am encountering an issue where the cursor does not move with my mouse and remains in place when I scroll. Could someone please review my code and identify what I am doing wrong? Additionally, I have ...

The placement of an HTML canvas within a webpage

I've been experimenting with a canvas script that resembles dottydots. Everything is functioning correctly, but I'm looking to adjust its position on the canvas. I attempted using position:absolute, which did move the canvas visually, but the wor ...

The current height of the div is not meeting the desired specifications outlined in

I am encountering some issues with nested 'div' elements. Within a parent div, I have two child 'div's. <div class="case-study-video-container"> <div class="video-holder"> <?php putRevSlider( '' ); ?> ...

Animating the mobile menu transition using Bootstrap CSS

Can anyone help me with animating the mobile menu when I click the menu icon? This is what I have for the menu: .overlay-menu { position: fixed; display: none; z-index : 1040; width: 100vw; height: 100vh; background: rgba(0, 0,0, 0 ...

Utilizing AngularJS to show content based on regular expressions using ng-show

With two images available, I need to display one image at a time based on an input regex pattern. Here is the code snippet: <input type="password" ng-model="password" placeholder="Enter Password"/> <img src="../close.png" ng-show="password != [ ...