Do you want so as to add customized styling to the primary and final pieces of your WordPress navigation menu?

You need to merely upload a customized CSS magnificence to the primary and final menu pieces, but when the menu is rearranged, then the ones pieces will now not be first and final.

On this article, we’ll display you learn how to upload a .first and .final magnificence that may taste the primary and final menu pieces even supposing the menu pieces are reordered.

How to Add the First & Last Class to WordPress Navigation Menu Items

Why Taste the First and Final Navigation Pieces In a different way?

In a previous customized design venture, we wanted so as to add some customized styling to the navigation menu pieces of a WordPress website. This design particularly required other styling for the primary menu merchandise and the final menu merchandise.

Now shall we simply edit the menu and upload a customized CSS magnificence to the primary and final menu merchandise. However as a result of we have been handing over the venture to a consumer, our resolution needed to paintings even supposing they rearranged the order of the menus.

So we determined to do use filters as a substitute.

On this educational, we’ll display you two tactics to taste the primary and final pieces of your navigation menu. You’ll make a selection your most popular approach from the checklist under:

Manner 1: Including First and Final Elegance The use of a Filter out

The primary strategy to taste your first and final navigation menu pieces another way is so as to add a clear out for your theme.

You’ll wish to upload code for your theme’s functions.php report. Should you haven’t finished this sooner than, then see our information on how to copy and paste code in WordPress.

All it’s a must to do is open your theme’s purposes.php report then paste the next code snippet:

serve as wpb_first_and_last_menu_class($pieces) {
    $pieces[1]->categories[] = 'first';
    $pieces[count($items)]->categories[] = 'final';
    go back $pieces;
}
add_filter('wp_nav_menu_objects', 'wpb_first_and_last_menu_class');

This creates .first and .final CSS categories to your first and final navigation menu pieces respectively. You’ll use the ones categories to taste the menu pieces.

To discover ways to do that intimately, check with our information on how to style WordPress navigation menus.

For this educational, we’ll upload the next fundamental CSS formatting to our theme’s taste.css stylesheet to easily daring the primary and final menu pieces:

.first a {font-weight: daring;}

.final a {font-weight: daring;}

Right here you’ll be able to see screenshots sooner than and once we added the code to our demo web site.

Preview of First and Last Menu Items Styled Differently

Manner 2: Styling First and Final Pieces The use of CSS Selectors

A 2nd strategy to taste the primary and final menu pieces another way is to make use of CSS selectors. This system is more effective, nevertheless it won’t paintings with some older browsers, corresponding to Web Explorer.

To apply this system you’ll have so as to add code for your theme’s style sheet or the ‘Further CSS’ segment of the WordPress Theme Customizer.

Should you haven’t finished this sooner than, then see our information on how to easily add custom CSS to your WordPress site.

You will have to get started by way of modifying your theme’s taste.css report, or by way of navigating to Look » Customise and clicking on ‘Further CSS’.

After that, you want to stick the next code snippet after which save or put up your adjustments.

ul#yourmenuid > li:first-child { }
ul#yourmenuid > li:last-child { }

Observe that you are going to wish to substitute ‘yourmenuid’ with the true ID of the navigation menu. The selectors ‘first-child’ and ‘last-child’ make a selection a component if it’s the first and final little one of its guardian, which is the navigation menu.

For instance, we used this code to daring the primary and final navigation menu pieces on our demo web site:

ul#primary-menu-list > li:first-child a {
    font-weight: daring;
}
ul#primary-menu-list > li:last-child a {
    font-weight: daring;
}
Using CSS Selectors to Style First and Last Menu Items Differently

We are hoping this educational helped you discover ways to upload the .first and .final magnificence to WordPress navigation menus.

You may additionally wish to be told how to fix 50 common WordPress errors, or take a look at our list of the best drag and drop page builders.

Should you appreciated this newsletter, then please subscribe to our YouTube Channel for WordPress video tutorials. You’ll additionally to find us on Twitter and Facebook.

The publish How to Add the First and Last CSS Class to WordPress Menu Items first gave the impression on WPBeginner.

WordPress Maintenance

[ continue ]