Choose the final elements by using CSS selectors

Can you target the last X elements in a group using only CSS without knowing the total number of elements?

For example, is it possible to target the last 3 elements in a ul using CSS?

<ul>
  <li>Element 1</li>
  <li>Element 2</li>
  <li>Element 3</li>
  ...
  <li>Element X-2</li>
  <li>Element X-1</li>
  <li>Element X</li>
</ul>

Answer №2

This CSS will add styles to the last 3 elements in a list. Check out the demo

li:nth-last-child(-n+3)
{
    ....
}

Browser Support:

Feature        Chrome   Firefox    (Gecko)    IE    Opera   Safari
Basic support   4.0     3.5        (1.9.1)    9.0   9.5   3.2

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

The CSS underline stays stationary when clicked

I'm attempting to implement a sliding horizontal line that moves on click of hyperlinks. Check out the code at: http://codepen.io/anon/pen/JdEPPb The HTML structure is as follows: * { box-sizing: border-box; } body { f ...

PHP Multiple Uploads is a feature that allows users to

I'm currently attempting to utilize the dropzone JS plugin in combination with PHP for the purpose of uploading multiple files and then displaying each file's URL. Here is my progress so far: $upload_dir = 'files'; for($i=0; $i&l ...

The spacing between lines has no impact on the functionality of my app

In my rails app, I have a form structured like the following: <p class='field'> <div class = "form-group"> <div class = "col-sm-3 mobile-label" > <%= form.label :loyalty%> <%=image_tag "qun1.png ...

When hovering over a single list item, only the top border will be displayed without affecting the

Check out the live link provided below: I am attempting to add a border and later an arrow in the middle on hover in the menu. However, the current code places a border over the entire menu, with individual list items on top of that. #menu ul li { margin ...

I'm experiencing an issue where my table refuses to sort in either ascending or descending order when using Bootstrap 4

<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>Demo</title> <!-- Bootstrap --> <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6 ...

Can CSS styling be applied to lazy-loaded images using Gatsby and Sharp before they finish loading?

I am utilizing Sharp to implement lazy loading for images on my Gatsby website. Currently, the images appear blurry and only come into focus once they enter the screen. Is there a method to add CSS styles or filters to these blurry images while they are s ...

Disappearance of external page upon refreshing - jquery mobile

I'm in the process of developing a jQuery mobile application that loads external pages into a specified div when a link is clicked. These externally loaded pages contain links to additional pages. However, I've noticed that when I click on these ...

Obscured Background Beneath the Bootstrap Tip

Desired Outcome: I am aiming to achieve a blurred background behind the tooltip element. https://i.sstatic.net/AE1RQ.png Current Status The current state shows that the area behind the tooltip is not blurred as intended. https://i.sstatic.net/bE57E.pn ...

Knitting: exporting to HTML file while retaining formatting

Recently discovered the amazing knitr library in R, which looks great when viewed in the viewer. But unfortunately, the style gets lost when saved to an html file. Code library(knitr) library(kableExtra) some.table <- data.frame ( x = rep(1 ...

Ways to conceal text as a div moves over it during hover effects?

How can I hide the title when hovering over the first child in my preview div items? I want to change the opacity of the title to zero on hover. Is there a simple way to achieve this effect? Currently, the layout looks like this - <div className="col ...

Learn how to trigger a Bootstrap 4 modal simply by clicking a button on the initial page, which will then direct you to a second page where the modal is already activated

In my current project, I am working with Bootstrap 4 and I have a specific requirement to implement a modal. The modal should function in the following way: Upon clicking a button on the first .html page, I want to be redirected to a second .html page wh ...

Reset all divs to their default state except for the one that is currently active using jQuery

Here's the plan for my script: Once a user clicks on a "learn more" button, it will reveal the relevant information in the corresponding box. I'm aiming to implement a feature where if you click on another "learn more" button while one box is a ...

Is it recommended to employ a switch statement in order to link CSS IDs with JavaScript variables?

function getVariableFromID(ID) { switch (ID) { case "GTA1": return GTA1; case "GTA2": return GTA2; case "GTA3": return GTA3; ... case "GTJ0": return GTJ0; } } In my code, I have implement ...

Achieving responsive masonry layout with Bootstrap 4

I'm attempting to implement the bootstrap 4 masonry effect on my website, but I'm facing issues with card responsiveness. The page is very basic without any special effects. While the page works well when resizing the browser window, it doesn&ap ...

tilt and give motion to image within canvas

I am looking to incorporate skewing and animation effects on an image displayed on a canvas. While I have successfully drawn the image on the canvas using the code snippet below, I am unsure about how to apply skewing and animation to the image post-drawin ...

Determining the disparity in days between two calendar dates provided by the user

I need assistance with creating a form that calculates the number of days a person has been staying at a specific location. For example, if someone enters Start Date: 22/02/2021 and End Date: 18/05/2021, I want the page to display "86" as the difference. ...

The scripts do not allow the image to be resized

Struggling to resize an image while implementing a hover effect with css and javascript. The code I have so far is: HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equ ...

Alignment issues with menus

Could someone help me with aligning my register and login buttons to the right? I will provide any necessary information upon request. Please note that this is just a small part of a larger menu with additional CSS styling. PHP/ HTML: <a href="<?ph ...

Issue encountered when attempting to conceal navigation buttons in navbar post user login due to Stack Overflow error

Is there a way to hide the login button in the masterpage header once the user is logged in? Here are the relevant codes: SiteMaster.Master.aspx <%@ Master Language="C#" AutoEventWireup="true" CodeBehind="SiteMaster.Master.cs&q ...

What steps can I take to ensure that the content within my bootstrap container always fits neatly within the container boundaries?

I'm attempting to structure one container with 3 rows. The first row contains the header, the last row the footer. In the middle row, I want two columns. The left column should display multiple scrollable cards. I've tried various methods to make ...