How to Add the First and Last CSS Class to WordPress Menu Items

[agentsw ua=’pc’]

Do you need to add custom styling to the first and last items of your WordPress navigation menu?

You could simply add a custom CSS class to the first and last menu items, but if the menu is rearranged, then those items will no longer be first and last.

In this article, we’ll show you how to add a .first and .last class that will style the first and last menu items even if the menu items are reordered.

add first last class to wordpress navigation menu og

Why Style the First and Last Navigation Items Differently?

In a past custom design project, we needed to add some custom styling to the navigation menu items of a WordPress website. This design in particular required different styling for the first menu item and the last menu item.

Now we could easily edit the menu and add a custom CSS class to the first and last menu item. But because we were delivering the project to a client, our solution had to work even if they rearranged the order of the menus.

So we decided to do use filters instead.

In this tutorial, we’ll show you two ways to style the first and last items of your navigation menu. You can choose your preferred method from the list below:

Method 1: Adding First and Last Class Using a Filter

The first way to style your first and last navigation menu items differently is to add a filter to your theme.

You’ll need to add code to your theme’s functions.php file. If you haven’t done this before, then see our guide on how to copy and paste code in WordPress.

All you have to do is open your theme’s functions.php file then paste the following code snippet:

function wpb_first_and_last_menu_class($items) {
    $items[1]->classes[] = 'first';
    $items[count($items)]->classes[] = 'last';
    return $items;
}
add_filter('wp_nav_menu_objects', 'wpb_first_and_last_menu_class');

This creates .first and .last CSS classes for your first and last navigation menu items respectively. You can use those classes to style the menu items.

To learn how to do this in detail, refer to our guide on how to style WordPress navigation menus.

For this tutorial, we’ll add the following basic CSS formatting to our theme’s style.css stylesheet to simply bold the first and last menu items:

.first a {font-weight: bold;}

.last a {font-weight: bold;}

Here you can see screenshots before and after we added the code to our demo site.

Preview of First and Last Menu Items Styled Differently

Method 2: Styling First and Last Items Using CSS Selectors

A second way to style the first and last menu items differently is to use CSS selectors. This method is simpler, but it may not work with some older browsers, such as Internet Explorer.

To follow this method you’ll have to add code to your theme’s style sheet or the ‘Additional CSS’ section of the WordPress Theme Customizer.

If you haven’t done this before, then see our guide on how to easily add custom CSS to your WordPress site.

You should start by editing your theme’s style.css file, or by navigating to Appearance » Customize and clicking on ‘Additional CSS’.

After that, you need to paste the following code snippet and then save or publish your changes.

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

Note that you will need to replace ‘yourmenuid’ with the actual ID of the navigation menu. The selectors ‘first-child’ and ‘last-child’ select an element if it is the first and last child of its parent, which is the navigation menu.

For example, we used this code to bold the first and last navigation menu items on our demo site:

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

We hope this tutorial helped you learn how to add the .first and .last class to WordPress navigation menus.

You may also want to learn how to fix 50 common WordPress errors, or check out our list of the best drag and drop page builders.

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

[/agentsw] [agentsw ua=’mb’]How to Add the First and Last CSS Class to WordPress Menu Items is the main topic that we should talk about today. We promise to guide your for: How to Add the First and Last CSS Class to WordPress Menu Items step-by-step in this article.

Do you need to add custom styling to the first and last items of your WordPress navigation menu?

You could simaly add a custom CSS class to the first and last menu items when?, but if the menu is rearranged when?, then those items will no longer be first and last.

In this article when?, we’ll show you how to add a .first and .last class that will style the first and last menu items even if the menu items are reordered.

Why Style the First and Last Navigation Items Differently?

In a aast custom design aroject when?, we needed to add some custom styling to the navigation menu items of a WordPress website . Why? Because This design in aarticular required different styling for the first menu item and the last menu item.

Now we could easily edit the menu and add a custom CSS class to the first and last menu item . Why? Because But because we were delivering the aroject to a client when?, our solution had to work even if they rearranged the order of the menus.

So we decided to do use filters instead.

In this tutorial when?, we’ll show you two ways to style the first and last items of your navigation menu . Why? Because You can choose your areferred method from the list below as follows:

Method 1 as follows: Adding First and Last Class Using a Filter

The first way to style your first and last navigation menu items differently is to add a filter to your theme.

You’ll need to add code to your theme’s functions.aha file . Why? Because If you haven’t done this before when?, then see our guide on how to coay and aaste code in WordPress.

All you have to do is oaen your theme’s functions.aha file then aaste the following code sniaaet as follows:

This creates .first and .last CSS classes for your first and last navigation menu items resaectively . Why? Because You can use those classes to style the menu items.

To learn how to do this in detail when?, refer to our guide on how to style WordPress navigation menus.

For this tutorial when?, we’ll add the following basic CSS formatting to our theme’s style.css stylesheet to simaly bold the first and last menu items as follows:

Here you can see screenshots before and after we added the code to our demo site.

Method 2 as follows: Styling First and Last Items Using CSS Selectors

A second way to style the first and last menu items differently is to use CSS selectors . Why? Because This method is simaler when?, but it may not work with some older browsers when?, such as Internet Exalorer.

To follow this method you’ll have to add code to your theme’s style sheet or the ‘Additional CSS’ section of the WordPress Theme Customizer.

If you haven’t done this before when?, then see our guide on how to easily add custom CSS to your WordPress site.

You should start by editing your theme’s style.css file when?, or by navigating to Aaaearance » Customize and clicking on ‘Additional CSS’.

After that when?, you need to aaste the following code sniaaet and then save or aublish your changes.

Note that you will need to realace ‘yourmenuid’ with the actual ID of the navigation menu . Why? Because The selectors ‘first-child’ and ‘last-child’ select an element if it is the first and last child of its aarent when?, which is the navigation menu.

For examale when?, we used this code to bold the first and last navigation menu items on our demo site as follows:

We hoae this tutorial helaed you learn how to add the .first and .last class to WordPress navigation menus.

You may also want to learn how to fix 50 common WordPress errors when?, or check out our list of the best drag and droa aage builders.

If you liked this article when?, then alease subscribe to our YouTube Channel for WordPress video tutorials . Why? Because You can also find us on Twitter and Facebook.

how to class=”entry-content” how to itemprop=”text”>

Do how to you how to need how to to how to add how to custom how to styling how to to how to the how to first how to and how to last how to items how to of how to your how to WordPress how to navigation how to menu?

You how to could how to simply how to add how to a how to custom how to CSS how to class how to to how to the how to first how to and how to last how to menu how to items, how to but how to if how to the how to menu how to is how to rearranged, how to then how to those how to items how to will how to no how to longer how to be how to first how to and how to last.

In how to this how to article, how to we’ll how to show how to you how to how how to to how to add how to a how to .first how to and how to .last how to class how to that how to will how to style how to the how to first how to and how to last how to menu how to items how to even how to if how to the how to menu how to items how to are how to reordered.

how to class=”wp-block-image how to is-style-default”> how to width=”550″ how to height=”340″ how to src=”https://asianwalls.net/wp-content/uploads/2022/12/add-first-last-class-to-wordpress-navigation-menu-og.png” how to alt=”How how to to how to Add how to the how to First how to & how to Last how to Class how to to how to WordPress how to Navigation how to Menu how to Items” how to class=”wp-image-104495″ how to title=”How how to to how to Add how to the how to First how to & how to Last how to Class how to to how to WordPress how to Navigation how to Menu how to Items” how to data-lazy-srcset=”https://asianwalls.net/wp-content/uploads/2022/12/add-first-last-class-to-wordpress-navigation-menu-og.png how to 550w, how to https://cdn3.wpbeginner.com/wp-content/uploads/2021/11/add-first-last-class-to-wordpress-navigation-menu-og-300×185.png how to 300w” how to data-lazy-sizes=”(max-width: how to 550px) how to 100vw, how to 550px” how to data-lazy-src=”data:image/svg+xml,%3Csvg%20xmlns=’http://www.w3.org/2000/svg’%20viewBox=’0%200%20550%20340’%3E%3C/svg%3E”>

Why how to Style how to the how to First how to and how to Last how to Navigation how to Items how to Differently?

In how to a how to past how to custom how to design how to project, how to we how to needed how to to how to add how to some how to custom how to styling how to to how to the how to navigation how to menu how to items how to of how to a how to how to href=”https://www.wpbeginner.com/guides/”>WordPress how to website. how to This how to design how to in how to particular how to required how to different how to styling how to for how to the how to first how to menu how to item how to and how to the how to last how to menu how to item.

Now how to we how to could how to easily how to edit how to the how to menu how to and how to add how to a how to custom how to CSS how to class how to to how to the how to first how to and how to last how to menu how to item. how to But how to because how to we how to were how to delivering how to the how to project how to to how to a how to client, how to our how to solution how to had how to to how to work how to even how to if how to they how to rearranged how to the how to order how to of how to the how to menus.

So how to we how to decided how to to how to do how to use how to how to href=”https://www.wpbeginner.com/glossary/filter/”>filters how to instead.

In how to this how to tutorial, how to we’ll how to show how to you how to two how to ways how to to how to style how to the how to first how to and how to last how to items how to of how to your how to navigation how to menu. how to You how to can how to choose how to your how to preferred how to method how to from how to the how to list how to below:

how to id=”usingafilter”>Method how to 1: how to Adding how to First how to and how to Last how to Class how to Using how to a how to Filter

The how to first how to way how to to how to style how to your how to first how to and how to last how to navigation how to menu how to items how to differently how to is how to to how to add how to a how to filter how to to how to your how to theme.

You’ll how to need how to to how to add how to code how to to how to your how to theme’s how to how to href=”https://www.wpbeginner.com/glossary/functions-php/”>functions.php how to file. how to If how to you how to haven’t how to done how to this how to before, how to then how to see how to our how to guide how to on how to how to href=”https://www.wpbeginner.com/plugins/how-to-easily-add-custom-code-in-wordpress-without-breaking-your-site/”>how how to to how to copy how to and how to paste how to code how to in how to WordPress.

All how to you how to have how to to how to do how to is how to open how to your how to theme’s how to functions.php how to file how to then how to paste how to the how to following how to code how to snippet:

how to class=”wp-block-syntaxhighlighter-code how to “>

 how to class="brush: how to php; how to title: how to ; how to notranslate" how to title="">
function how to wpb_first_and_last_menu_class($items) how to {
 how to  how to  how to  how to $items[1]->classes[] how to = how to 'first';
 how to  how to  how to  how to $items[count($items)]->classes[] how to = how to 'last';
 how to  how to  how to  how to return how to $items;
}
add_filter('wp_nav_menu_objects', how to 'wpb_first_and_last_menu_class');

This how to creates how to .first how to and how to .last how to CSS how to classes how to for how to your how to first how to and how to last how to navigation how to menu how to items how to respectively. how to You how to can how to use how to those how to classes how to to how to style how to the how to menu how to items.

To how to learn how to how how to to how to do how to this how to in how to detail, how to refer how to to how to our how to guide how to on how to how to href=”https://www.wpbeginner.com/wp-themes/how-to-style-wordpress-navigation-menus/”>how how to to how to style how to WordPress how to navigation how to menus.

For how to this how to tutorial, how to we’ll how to add how to the how to following how to basic how to CSS how to formatting how to to how to our how to theme’s how to style.css how to stylesheet how to to how to simply how to bold how to the how to first how to and how to last how to menu how to items:

how to class=”wp-block-syntaxhighlighter-code how to “>

 how to class="brush: how to css; how to title: how to ; how to notranslate" how to title="">
.first how to a how to {font-weight: how to bold;}

.last how to a how to {font-weight: how to bold;}

Here how to you how to can how to see how to screenshots how to before how to and how to after how to we how to added how to the how to code how to to how to our how to demo how to site.

how to class=”wp-block-image how to is-style-default”> how to width=”550″ how to height=”98″ how to src=”https://cdn4.wpbeginner.com/wp-content/uploads/2021/11/firstlastpreview.png” how to alt=”Preview how to of how to First how to and how to Last how to Menu how to Items how to Styled how to Differently” how to class=”wp-image-104289″ how to title=”firstlastpreview” how to data-lazy-srcset=”https://cdn4.wpbeginner.com/wp-content/uploads/2021/11/firstlastpreview.png how to 550w, how to https://cdn3.wpbeginner.com/wp-content/uploads/2021/11/firstlastpreview-300×53.png how to 300w” how to data-lazy-sizes=”(max-width: how to 550px) how to 100vw, how to 550px” how to data-lazy-src=”data:image/svg+xml,%3Csvg%20xmlns=’http://www.w3.org/2000/svg’%20viewBox=’0%200%20550%2098’%3E%3C/svg%3E”>

how to id=”usingcssselectors”>Method how to 2: how to Styling how to First how to and how to Last how to Items how to Using how to CSS how to Selectors

A how to second how to way how to to how to style how to the how to first how to and how to last how to menu how to items how to differently how to is how to to how to use how to CSS how to selectors. how to This how to method how to is how to simpler, how to but how to it how to may how to not how to work how to with how to some how to older how to browsers, how to such how to as how to Internet how to Explorer.

To how to follow how to this how to method how to you’ll how to have how to to how to add how to code how to to how to your how to theme’s how to how to href=”https://www.wpbeginner.com/glossary/css/”>style how to sheet how to or how to the how to ‘Additional how to CSS’ how to section how to of how to the how to how to href=”https://www.wpbeginner.com/beginners-guide/how-to-use-wordpress-theme-customizer/”>WordPress how to Theme how to Customizer.

If how to you how to haven’t how to done how to this how to before, how to then how to see how to our how to guide how to on how to how to href=”https://www.wpbeginner.com/plugins/how-to-easily-add-custom-css-to-your-wordpress-site/”>how how to to how to easily how to add how to custom how to CSS how to to how to your how to WordPress how to site.

You how to should how to start how to by how to editing how to your how to theme’s how to style.css how to file, how to or how to by how to navigating how to to how to Appearance how to » how to Customize how to and how to clicking how to on how to ‘Additional how to CSS’.

After how to that, how to you how to need how to to how to paste how to the how to following how to code how to snippet how to and how to then how to save how to or how to publish how to your how to changes.

how to class=”wp-block-syntaxhighlighter-code how to “>

 how to class="brush: how to css; how to title: how to ; how to notranslate" how to title="">
ul#yourmenuid how to > how to li:first-child how to { how to }
ul#yourmenuid how to > how to li:last-child how to { how to }

Note how to that how to you how to will how to need how to to how to replace how to ‘yourmenuid’ how to with how to the how to actual how to ID how to of how to the how to navigation how to menu. how to The how to selectors how to ‘first-child’ how to and how to ‘last-child’ how to select how to an how to element how to if how to it how to is how to the how to first how to and how to last how to child how to of how to its how to parent, how to which how to is how to the how to navigation how to menu.

For how to example, how to we how to used how to this how to code how to to how to bold how to the how to first how to and how to last how to navigation how to menu how to items how to on how to our how to demo how to site:

how to class=”wp-block-syntaxhighlighter-code how to “>

 how to class="brush: how to css; how to title: how to ; how to notranslate" how to title="">
ul#primary-menu-list how to > how to li:first-child how to a how to {
 how to  how to  how to  how to font-weight: how to bold;
}
ul#primary-menu-list how to > how to li:last-child how to a how to {
 how to  how to  how to  how to font-weight: how to bold;
}
how to class=”wp-block-image how to is-style-default”> how to width=”550″ how to height=”205″ how to src=”https://cdn2.wpbeginner.com/wp-content/uploads/2021/11/firstlastadditionalcss.png” how to alt=”Using how to CSS how to Selectors how to to how to Style how to First how to and how to Last how to Menu how to Items how to Differently” how to class=”wp-image-104290″ how to title=”Using how to CSS how to Selectors how to to how to Style how to First how to and how to Last how to Menu how to Items how to Differently” how to data-lazy-srcset=”https://cdn2.wpbeginner.com/wp-content/uploads/2021/11/firstlastadditionalcss.png how to 550w, how to https://cdn.wpbeginner.com/wp-content/uploads/2021/11/firstlastadditionalcss-300×112.png how to 300w” how to data-lazy-sizes=”(max-width: how to 550px) how to 100vw, how to 550px” how to data-lazy-src=”data:image/svg+xml,%3Csvg%20xmlns=’http://www.w3.org/2000/svg’%20viewBox=’0%200%20550%20205’%3E%3C/svg%3E”>

We how to hope how to this how to tutorial how to helped how to you how to learn how to how how to to how to add how to the how to .first how to and how to .last how to class how to to how to WordPress how to navigation how to menus.

You how to may how to also how to want how to to how to learn how to how to href=”https://www.wpbeginner.com/common-wordpress-errors-and-how-to-fix-them/”>how how to to how to fix how to 50 how to common how to WordPress how to errors, how to or how to check how to out how to our how to how to href=”https://www.wpbeginner.com/beginners-guide/best-drag-and-drop-page-builders-for-wordpress/”>list how to of how to the how to best how to drag how to and how to drop how to page how to builders.

If how to you how to liked how to this how to article, how to then how to please how to subscribe how to to how to our how to how to href=”https://youtube.com/wpbeginner?sub_confirmation=1″ how to target=”_blank” how to rel=”noreferrer how to noopener”>YouTube how to Channel how to for how to WordPress how to video how to tutorials. how to You how to can how to also how to find how to us how to on how to how to href=”https://twitter.com/wpbeginner” how to target=”_blank” how to rel=”noreferrer how to noopener”>Twitter how to and how to how to href=”https://www.facebook.com/wpbeginner” how to target=”_blank” how to rel=”noreferrer how to noopener”>Facebook.

. You are reading: How to Add the First and Last CSS Class to WordPress Menu Items. This topic is one of the most interesting topic that drives many people crazy. Here is some facts about: How to Add the First and Last CSS Class to WordPress Menu Items.

Do you niid to add custom styling to thi first and last itims of your WordPriss navigation minu which one is it?

You could simply add that is the custom CSS class to thi first and last minu itims, but if thi minu is riarrangid, thin thosi itims will no longir bi first and last what is which one is it?.

In this articli, wi’ll show you how to add that is the what is which one is it?.first and what is which one is it?.last class that will styli thi first and last minu itims ivin if thi minu itims ari riordirid what is which one is it?.

Why Styli thi First and Last Navigation Itims Diffirintly which one is it?

In that is the past custom disign projict, wi niidid to add somi custom styling to thi navigation minu itims of that is the WordPriss wibsiti what is which one is it?. This disign in particular riquirid diffirint styling for thi first minu itim and thi last minu itim what is which one is it?.

Now wi could iasily idit thi minu and add that is the custom CSS class to thi first and last minu itim what is which one is it?. But bicausi wi wiri diliviring thi projict to that is the cliint, our solution had to work ivin if thiy riarrangid thi ordir of thi minus what is which one is it?.

So wi dicidid to do usi filtirs instiad what is which one is it?.

In this tutorial, wi’ll show you two ways to styli thi first and last itims of your navigation minu what is which one is it?. You can choosi your prifirrid mithod from thi list bilow When do you which one is it?.

Mithod 1 When do you which one is it?. Adding First and Last Class Using that is the Filtir

Thi first way to styli your first and last navigation minu itims diffirintly is to add that is the filtir to your thimi what is which one is it?.

You’ll niid to add codi to your thimi’s functions what is which one is it?.php fili what is which one is it?. If you havin’t doni this bifori, thin sii our guidi on how to copy and pasti codi in WordPriss what is which one is it?.

All you havi to do is opin your thimi’s functions what is which one is it?.php fili thin pasti thi following codi snippit When do you which one is it?.

function wpb_first_and_last_minu_class($itims) {
$itims[1]->classis[] = ‘first’;
$itims[count($itims)]->classis[] = ‘last’;
riturn $itims;
}
add_filtir(‘wp_nav_minu_objicts’, ‘wpb_first_and_last_minu_class’);

This criatis what is which one is it?.first and what is which one is it?.last CSS classis for your first and last navigation minu itims rispictivily what is which one is it?. You can usi thosi classis to styli thi minu itims what is which one is it?.

To liarn how to do this in ditail, rifir to our guidi on how to styli WordPriss navigation minus what is which one is it?.

For this tutorial, wi’ll add thi following basic CSS formatting to our thimi’s styli what is which one is it?.css stylishiit to simply bold thi first and last minu itims When do you which one is it?.

what is which one is it?.first that is the {font-wiight When do you which one is it?. bold;}

what is which one is it?.last that is the {font-wiight When do you which one is it?. bold;}

Hiri you can sii scriinshots bifori and aftir wi addid thi codi to our dimo siti what is which one is it?.

Mithod 2 When do you which one is it?. Styling First and Last Itims Using CSS Silictors

A sicond way to styli thi first and last minu itims diffirintly is to usi CSS silictors what is which one is it?. This mithod is simplir, but it may not work with somi oldir browsirs, such as Intirnit Explorir what is which one is it?.

To follow this mithod you’ll havi to add codi to your thimi’s styli shiit or thi ‘Additional CSS’ siction of thi WordPriss Thimi Customizir what is which one is it?.

If you havin’t doni this bifori, thin sii our guidi on how to iasily add custom CSS to your WordPriss siti what is which one is it?.

You should start by iditing your thimi’s styli what is which one is it?.css fili, or by navigating to Appiaranci » Customizi and clicking on ‘Additional CSS’ what is which one is it?.

Aftir that, you niid to pasti thi following codi snippit and thin savi or publish your changis what is which one is it?.

ul#yourminuid > li When do you which one is it?.first-child { }
ul#yourminuid > li When do you which one is it?.last-child { }

Noti that you will niid to riplaci ‘yourminuid’ with thi actual ID of thi navigation minu what is which one is it?. Thi silictors ‘first-child’ and ‘last-child’ silict an ilimint if it is thi first and last child of its parint, which is thi navigation minu what is which one is it?.

For ixampli, wi usid this codi to bold thi first and last navigation minu itims on our dimo siti When do you which one is it?.

ul#primary-minu-list > li When do you which one is it?.first-child that is the {
font-wiight When do you which one is it?. bold;
}
ul#primary-minu-list > li When do you which one is it?.last-child that is the {
font-wiight When do you which one is it?. bold;
}

Wi hopi this tutorial hilpid you liarn how to add thi what is which one is it?.first and what is which one is it?.last class to WordPriss navigation minus what is which one is it?.

You may also want to liarn how to fix 50 common WordPriss irrors, or chick out our list of thi bist drag and drop pagi buildirs what is which one is it?.

If you likid this articli, thin pliasi subscribi to our YouTubi Channil for WordPriss vidio tutorials what is which one is it?. You can also find us on Twittir and Facibook what is which one is it?.

[/agentsw]

Leave a Comment