Why is the :nth-last-child(-n+3) selector not compatible with the article element?

I've been trying to figure out why the :nth-last-child(-n+3) {} selector is not working for the article element in my document markup. It should work according to comments I've seen, like in this example: http://jsfiddle.net/8PXXm/

However, when I incorporate the markup into my layout, it doesn't seem to have the desired effect of selecting the last 3 items in a row.

.post-entry:nth-last-child(n+3) { display: none; } 

The above code seems to be selecting all row items rather than just the last 3.

Here is the HTML snippet:

<article class="grid_4 post-entry">
    <a href="#" title="">
        <figure class="post-thumb">
                <img src="../images/placehold.png" alt="Placehold">

                <figcaption>
                    <img src="../images/cross.png" alt="Cross x">
                </figcaption><!-- End figcaption.post-thumb -->
        </figure><!-- End figure.post-thumb -->
    </a>
</article><!-- End article.grid_4 post-entry -->

Here's the JSFiddle link for reference: http://jsfiddle.net/4XP5W/11/

Answer №1

Your code is correct, but you need to include the - in your selector.

If it's still not working, you should remove the last two elements div.clear and footer.grid_12 from the parent div.container12

This is necessary because:

The :nth-last-child CSS pseudo-class targets an element with siblings that come after it in the document tree.

Therefore, your selector

.post-entry:nth-last-child(n+3) { background: red; }
is including

The elements div.clear and footer.grid_12 as well.

Update:

If you want to keep those elements in your HTML structure, simply wrap your .post-entry elements within a block element like div or section, excluding the last two non-sibling elements of .post-entry. Make sure that the block element only contains siblings of .post-entry

JSFiddle Link

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

Modify the css with JQUERY when there are no rows inside the tbody section

Is it possible to change the css using jquery if there are no rows in the table body? I have attempted this but my current approach is not working. I tried adding an alert within the if statement, but the alert did not appear. My goal is to hide the table ...

ASP:Menu: Want to style bulleted lists using CSS

I am currently utilizing ASP:Menu and I would like to customize the menu display as outlined below. Can you please advise on how to implement the CSS styling and what modifications are required? Products Instock Out-of-Stock Orders Purchase Orders Sa ...

Identify specific elements using CSS to easily target them with JavaScript later on

Currently, I am utilizing CSS selectors to target specific elements and now I want to be able to identify those elements using javascript. My approach involves setting the color of these elements in css and then retrieving them based on their color. Howeve ...

Firefox Misplacement: Erroneously Shifting Element Locations

I have recently completed designing and developing a new website, which you can visit here. However, I am currently facing an issue with the positioning of the "About Us" image when accessing the site on either a tablet or a mobile device. Strangely enou ...

Tips for changing a fullscreen HTML5 video appearance

I discovered a way to change the appearance of a regular video element using this CSS code: transform: rotate(9deg) !important; But, when I try to make the video fullscreen, the user-agent CSS rules seem to automatically take control: https://i.sstatic. ...

Distinguishing Font Sizes in Safari Compared to UIWebView

When the page is loaded in Safari, everything looks perfect. However, when viewed in a UIWebView, all the fonts appear enlarged. I've done my research and tried various solutions like: - Setting WebView.scalesPageToFit = true - Applying default con ...

Issues with Media Queries on Desktop Devices

I've encountered an odd issue with my media queries. While they function properly on my mobile devices, resizing my desktop browser doesn't seem to trigger the changes. This prevents me from inspecting my mobile styles using web inspector/firebug ...

Picture failing to adjust to the designated width

I am struggling with an issue on my website where the images in a section are not scaling properly in two columns. Even though I have set the margins and widths in my media query for a viewport of 640px or less, the images do not adjust in size when draggi ...

Navigating through tabs in a Meteor application: How to maintain the active tab when using the back button

I am working on a multi-page meteor application where each page includes a navigation template. To switch between pages, I am using iron-router. The user's current page is indicated by setting the appropriate navigation link's class to 'ac ...

Apply a dual-color gradient background vertically

I am trying to figure out how to achieve a two-tone background color in an HTML document. I know this question has been asked before, but I specifically want the colors to be split vertically rather than horizontally. ...

Creating a dual-direction infinite scroll effect with CSS through mouse dragging

I'm currently working on implementing an infinite scroll component for a project. After consulting this tutorial, I've encountered an issue. It seems that I can only achieve infinite scroll in one direction. Whenever I add elements to the leftmo ...

"Flaticon icons display properly when stored on a local server, but encounter issues when accessed online

Working on a website, I have incorporated numerous icons into the design. Despite organizing the files effectively, I am struggling to identify any potential issues. As a beginner in this field, I continue to learn and grow. ...

Is it possible to automatically move text to the next line once it reaches a specific character limit using HTML/CSS/PHP?

Is there a way to handle text when it reaches the ceiling or exceeds a certain number of characters so that it goes to the next line instead of showing a scrollbar? I have a div element where I display text retrieved from a MySQL database, and sometimes th ...

Ways to verify an iCheck located on the following page

Here is the code snippet I am currently working with: $("#add-new-size-grp").click( function (event) { event.preventDefault(); $.ajax({ type: "get", url:"ajax-get-sizes.php", success: function(result){ $("#sizeg ...

Ways to remove the extra space above and below text within a td element

My goal is to make the background change when a user hovers over the td element. The current behavior of my code can be seen in the image below: I am working to eliminate the white space so that the gradient fills the entire td/cell. The following code s ...

Issue with Bootstrap's has-feedback icon not displaying correctly when working in conjunction with a dropdownlist

I'm encountering an issue regarding the alignment of bootstrap feedback. The form-control-feedback when used with a dropdown list does not align correctly. I would like it to appear in the right corner after the dropdown list, similar to how it displa ...

What methods can be used to prevent divs from overlapping when the window size is reduced?

Creating a login page with two main content divs in a section layout. The first div contains the logo, input prompts, and login/password reset buttons. The second div serves as a footer with social media links. On a full-size window, the logo is positioned ...

Tally Every Number Encountered Within the Blog Archive

I've been facing some challenges putting together this jquery functionality. My original idea was to create a toggle effect for the archive's years and months. For example, when I click on a YEAR, it should toggle down to display all MONTHS, and ...

Overflow issues with CSS FlexBoxLayout

I am currently working on creating a website using Bootstrap and CSS's flexbox. My goal is to create a list that fits the height of the screen without overflowing it. After some trial and error, I managed to come up with a solution that looks like th ...

stylized tables with assorted colored categories

Here is the layout of my table: Grade Sign StudentGrade 6 + 1 6 0 6 - 0 5 + 0 5 0 5 - 0 4 + 0 4 0 4 - 1 3 + 0 3 ...