{"id":139311,"date":"2015-04-07T08:00:39","date_gmt":"2015-04-07T12:00:39","guid":{"rendered":"http:\/\/premium.wpmudev.org\/blog\/?p=139311"},"modified":"2022-03-08T04:40:29","modified_gmt":"2022-03-08T04:40:29","slug":"add-icons-wordpress-menus","status":"publish","type":"post","link":"https:\/\/wpmudev.com\/blog\/add-icons-wordpress-menus\/","title":{"rendered":"How to Add Icons to Custom WordPress Menus Without Plugins"},"content":{"rendered":"<p>Adding icons to your site&#8217;s navigation menu gives your visitors some visual clues about the content and also adds a nice design touch to your site.<\/p>\n<p>In this post I&#8217;ll demonstrate how you can do this without using any plugins or uploading any images. Instead you&#8217;ll make some tweaks to your navigation menu via the WordPress admin and then add some code in your theme file.<\/p>\n<p>When you&#8217;ve finished you&#8217;ll have some simple icons next to each item in your navigation menu, and the best part is that you won&#8217;t have to upload any images or files that could slow your site down. Here&#8217;s how the navigation menu will look once we&#8217;ve finished:<\/p>\n<div  class=\"wpdui-pic-large   \" >\n<div class=\"image-grid cgrid-row\">\n<div class=\"cgrid-col cgrid-col-span-full\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-139322\" src=\"https:\/\/wpmudev.com\/blog\/wp-content\/uploads\/2015\/03\/final-navigation-menu-with-icons.png\" alt=\"The navigation menu with fonts and font size corrected\" width=\"735\" height=\"390\" \/><\/div>\n<\/div>\n<\/div>\n<p>We&#8217;ll use the <a href=\"http:\/\/fontawesome.io\" target=\"_blank\">Font Awesome<\/a> icon library, which you could add via a plugin but you don&#8217;t need to as you can easily call it from your theme&#8217;s functions file.<\/p>\n<p>Continue reading, or jump ahead using these links:<\/p>\n<ul>\n<li><a href=\"#overview\">Adding Menu Icons \u2013 An Overview<\/a><\/li>\n<li><a href=\"#activating-font-awesome\">Activating Font Awesome<\/a><\/li>\n<li><a href=\"#adding-icons-to-the-menu-items\">Adding Icons to the Menu Items<\/a><\/li>\n<li><a href=\"#styling-the-menu-items\">Styling the Menu Items<\/a><\/li>\n<\/ul>\n<h2 id=\"overview\">Adding Menu Icons \u2013 An Overview<\/h2>\n<p>These are the steps you&#8217;ll be taking if you follow along with this post:<\/p>\n<ol>\n<li>Create a child theme of the twenty fifteen theme to work on (you can skip this step if you&#8217;re working with your own theme).<\/li>\n<li>Activate the font awesome library from your theme&#8217;s functions file.<\/li>\n<li>Add CSS classes to the items in your navigation menu, via the WordPress admin.<\/li>\n<li>Add some styling to your theme&#8217;s stylesheet to get the layout and fonts right.<\/li>\n<\/ol>\n<p>Let&#8217;s get started.<\/p>\n<h3>Creating a Child Theme of Twenty Fifteen<\/h3>\n<p>If you don&#8217;t already have a theme you&#8217;re working with, you&#8217;ll need to create one. The easiest way to do this is by creating a child theme of an existing theme, which means you can add your own changes without doing anything to the original theme. I&#8217;m going to create a child theme of the default theme, which is <a href=\"https:\/\/wordpress.org\/themes\/twentyfifteen\/\" target=\"_blank\">twenty fifteen<\/a>.<\/p>\n<p>If you&#8217;re new to child theme, check out our <a title=\"How to Create a Child Theme\" href=\"https:\/\/wpmudev.com\/blog\/how-to-create-wordpress-child-theme\/\" target=\"_blank\">comprehensive guide<\/a>\u00a0that explains how to do it.<\/p>\n<h2 id=\"activating-font-awesome\">Activating Font Awesome<\/h2>\n<p>The next step is to activate the Font Awesome library in your theme. Before doing that, let&#8217;s take a quick look at <a href=\"http:\/\/fontawesome.io\" target=\"_blank\">Font Awesome<\/a>:<\/p>\n<div  class=\"wpdui-pic-large   \" >\n<div class=\"image-grid cgrid-row\">\n<div class=\"cgrid-col cgrid-col-span-full\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-139314\" src=\"https:\/\/wpmudev.com\/blog\/wp-content\/uploads\/2015\/03\/font-awesome.png\" alt=\"Font Awesome - the iconic font and CSS toolkit. Website home page.\" width=\"735\" height=\"592\" \/><\/div>\n<\/div>\n<\/div>\n<p>Font Awesome is a library of icons created using an icon font. This means it doesn&#8217;t use background images, as you might have done in the past when adding icons to your site. Instead it uses CSS classes, adding a pseudo-element to anything with an icon class. This pseudo-element adds a special character\u00a0before the element, with the icon style. What you end up with is an icon being output immediately before the element\u00a0you&#8217;ve added\u00a0the CSS class to.<\/p>\n<p>If you want to learn more about how Font Awesome uses CSS, take a look at the <a href=\"http:\/\/fontawesome.io\/examples\/\" target=\"_blank\">examples<\/a> page on its website. You&#8217;ll also learn how to use classes to display\u00a0icons as we work through this tutorial, which is probably the easiest way to get to grips with it.<\/p>\n<p>So, let&#8217;s start by activating Font Awesome in our theme.<\/p>\n<p>In your theme folder, create a new file called <code>functions.php<\/code>. If your theme already has a functions file, open that.<\/p>\n<p>Add this code to your functions file:<\/p>\n<div class=\"gist\" data-gist=\"76e9ab6f27c66de24d3b0c132521e214\" data-gist-file=\"Activate Font Awesome\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/76e9ab6f27c66de24d3b0c132521e214.js?file=Activate+Font+Awesome\">Loading gist 76e9ab6f27c66de24d3b0c132521e214<\/a><div class=\"gist-consent-notice\" style=\"display:none\"><p>Please <a href=\"javascript:Cookiebot.renew()\">update your cookie preferences<\/a> to enable preference cookies to view this gist.<\/p><\/div><\/div>\n<p>Let&#8217;s take a look at what that does:<\/p>\n<ol>\n<li>It creates a function called\u00a0<code>wmpudev_enqueue_icon_stylesheet()<\/code>.<\/li>\n<li>Inside that function, it uses <code><a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/wp_register_style\/\" rel=\"noopener\" target=\"_blank\">wp_register_style()<\/a><\/code> to register the Font Awesome stylesheet, which is hosted outside your site. You can find this link on the Font Awesome site&#8217;s <a href=\"http:\/\/fontawesome.io\/get-started\/\" target=\"_blank\">getting started<\/a> page.<\/li>\n<li>Next it uses <code><a href=\"https:\/\/codex.wordpress.org\/Function_Reference\/wp_enqueue_style\" target=\"_blank\">wp_enqueue_style()<\/a><\/code> to enqueue the style it just registered.<\/li>\n<li>Finally, it hooks the function to the <code>wp_enqueue_scripts<\/code> action hook, which tells WordPress when to run the function.<\/li>\n<\/ol>\n<p>Note that in the Font Awesome&#8217;s getting started page, you&#8217;re given instructions to call the stylesheet from the <code>&lt;head&gt;<\/code> section of your web pages (which would be in the <code>header.php<\/code> file of a WordPress theme. That\u00a0isn&#8217;t the best way to register stylesheets in WordPress: the method above is the way you should\u00a0do it.<\/p>\n<p>Now save your <code>functions.php<\/code> file.<\/p>\n<h2 id=\"adding-icons-to-the-menu-items\">Adding Icons to the Menu Items<\/h2>\n<p>Now that we&#8217;ve got Font Awesome registered, we can use the CSS classes it provides to add icons to our menu items.<\/p>\n<p>In the WordPress Admin, go to <strong>Appearance &gt; Menus<\/strong> so that you can edit your navigation menu. If you haven&#8217;t already created a navigation menu, create one now and make sure you&#8217;ve ticked the checkbox in the menus screen so that it&#8217;s in the &#8216;Primary Navigation&#8217; slot in your theme.<\/p>\n<p>WordPress lets you add a CSS class to each item in your menu, but you can&#8217;t see the field for this by default: you need to switch it on first:<\/p>\n<ol>\n<li>At the top of the screen, click the Screen Options tab.<\/li>\n<li>Make sure the box next to &#8216;CSS Classes&#8217; is checked.<\/li>\n<li>Close the Screen Options tab.<\/li>\n<\/ol>\n<p>Now you can add a class to each of your menu items. Start with the &#8216;Home&#8217; menu item. Next to the menu item, click on the downwards arrow to view more options relating to that menu item. In the &#8216;CSS Classes&#8217; field, type the following:<\/p>\n<div class=\"gist\" data-gist=\"b53ea6bc20705ccefef008078360a9a0\" data-gist-file=\"CSS for Home menu item\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/b53ea6bc20705ccefef008078360a9a0.js?file=CSS+for+Home+menu+item\">Loading gist b53ea6bc20705ccefef008078360a9a0<\/a><div class=\"gist-consent-notice\" style=\"display:none\"><p>Please <a href=\"javascript:Cookiebot.renew()\">update your cookie preferences<\/a> to enable preference cookies to view this gist.<\/p><\/div><\/div>\n<p>This adds three classes to your menu item:<\/p>\n<ul>\n<li><code>fa<\/code> is used\u00a0for all items with a Font Awesome icon<\/li>\n<li><code>fa-lg<\/code> sets the size of the icon to large<\/li>\n<li><code>fa-home<\/code> relates to the specific icon you want to display.<\/li>\n<\/ul>\n<p>The admin interface for your menu will now look like this:<\/p>\n<div  class=\"wpdui-pic-large   \" >\n<div class=\"image-grid cgrid-row\">\n<div class=\"cgrid-col cgrid-col-span-full\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-139317\" src=\"https:\/\/wpmudev.com\/blog\/wp-content\/uploads\/2015\/03\/menu-screen-class-added.png\" alt=\"Menus Admin Screen with CSS Classes added\" width=\"735\" height=\"592\" \/><\/div>\n<\/div>\n<\/div>\n<p>Now do the same for each of your menu items. You&#8217;ll find more icons and their classes on\u00a0the Font Awesome <a href=\"http:\/\/fontawesome.io\/icons\/\" target=\"_blank\">icons<\/a> page.<\/p>\n<p>I&#8217;m using the following classes:<\/p>\n<ul>\n<li>Home:\u00a0<code>fa fa-lg fa-home<\/code> (as above: a house icon)<\/li>\n<li>News:\u00a0<code>fa fa-lg fa-newspaper-o<\/code> (a newspaper icon)<\/li>\n<li>Gallery:\u00a0<code>fa fa-lg fa-camera-retro<\/code> (a camera icon)<\/li>\n<li>About:\u00a0<code>fa fa-lg fa-info-circle<\/code> (an info icon)<\/li>\n<li>Contact:\u00a0<code>fa fa-lg fa-envelope-o<\/code> (an envelope icon)<\/li>\n<\/ul>\n<p>You can use whichever icons you want. Just make sure you add <code>fa fa-lg<\/code> and then the class for the icon to each navigation menu item in the &#8216;CSS Classes&#8217; field.<\/p>\n<p>Now save your menu by clicking the <strong>Save Menu<\/strong> button.<\/p>\n<h2 id=\"styling-the-menu-items\">Styling the Menu Items<\/h2>\n<p>Now take a look at your site&#8217;s menu in the front end. Here&#8217;s mine:<\/p>\n<div  class=\"wpdui-pic-large   \" >\n<div class=\"image-grid cgrid-row\">\n<div class=\"cgrid-col cgrid-col-span-full\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-139318\" src=\"https:\/\/wpmudev.com\/blog\/wp-content\/uploads\/2015\/03\/menu-icons-added-no-styling.png\" alt=\"Navigation menu with Icons added but no styling\" width=\"735\" height=\"383\" \/><\/div>\n<\/div>\n<\/div>\n<p>That&#8217;s a bit of a mess! The fonts in my menu have changed, the icons are above the text when I want them to the left, and my menu items are next to each other when they should be one above the other.<\/p>\n<p>If you&#8217;re working with your own theme, you may find that your menu looks better (or worse!) and you may need to make some different tweaks.\u00a0To fix the problems in my site, I&#8217;m going to add some styling to my stylesheet.<\/p>\n<p>Let&#8217;s\u00a0start by making the menu items one above the other.<\/p>\n<p>Open your theme&#8217;s stylesheet and add the following to it:<\/p>\n<div class=\"gist\" data-gist=\"de419105fd2929459d87ee5e97274c58\" data-gist-file=\"Navigation Menu width styling\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/de419105fd2929459d87ee5e97274c58.js?file=Navigation+Menu+width+styling\">Loading gist de419105fd2929459d87ee5e97274c58<\/a><div class=\"gist-consent-notice\" style=\"display:none\"><p>Please <a href=\"javascript:Cookiebot.renew()\">update your cookie preferences<\/a> to enable preference cookies to view this gist.<\/p><\/div><\/div>\n<p>This will ensure that each menu item takes up 100% width of its containing element.<\/p>\n<p><em>Note: If you&#8217;re working with your own theme you may need to target different classes or IDs\u00a0for your menu depending on how your theme is coded. Use the web inspector in your browser to identify the classes to target.<\/em><\/p>\n<p>&nbsp;<\/p>\n<p>Now when you refresh the screen the menu is looking a bit better:<\/p>\n<div  class=\"wpdui-pic-large   \" >\n<div class=\"image-grid cgrid-row\">\n<div class=\"cgrid-col cgrid-col-span-full\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-139319\" src=\"https:\/\/wpmudev.com\/blog\/wp-content\/uploads\/2015\/03\/menu-icons-added-width-styled.png\" alt=\"Menu icons on home page - menu item width corrected to full width of container\" width=\"735\" height=\"390\" \/><\/div>\n<\/div>\n<\/div>\n<p>The menu items are now in the right place but the icons should be to the left of each menu item instead of above it.<\/p>\n<p>Fix this by adding the following to your stylesheet:<\/p>\n<div class=\"gist\" data-gist=\"1e446d1349f71cf607b199e5f3a10fce\" data-gist-file=\"Float for menu icon\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/1e446d1349f71cf607b199e5f3a10fce.js?file=Float+for+menu+icon\">Loading gist 1e446d1349f71cf607b199e5f3a10fce<\/a><div class=\"gist-consent-notice\" style=\"display:none\"><p>Please <a href=\"javascript:Cookiebot.renew()\">update your cookie preferences<\/a> to enable preference cookies to view this gist.<\/p><\/div><\/div>\n<p>This uses the <code>::before<\/code> pseudo-element that\u00a0Font Awesome adds to each element you assign its classes to. In this case we&#8217;re making the contents of the pseudo-element float left, which means the icon will appear on the\u00a0left of the menu text.<\/p>\n<p>Now save your stylesheet and refresh your screen. The icons will be in the right place:<\/p>\n<div  class=\"wpdui-pic-large   \" >\n<div class=\"image-grid cgrid-row\">\n<div class=\"cgrid-col cgrid-col-span-full\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-139320\" src=\"https:\/\/wpmudev.com\/blog\/wp-content\/uploads\/2015\/03\/menu-icons-added-floats-styled.png\" alt=\"Menu icons floated left\" width=\"735\" height=\"390\" \/><\/div>\n<\/div>\n<\/div>\n<p>The icons are in the right place now but they could do with tidying up. Let&#8217;s add\u00a0some more styling for a margin and width. Edit the declaration for the pseudo-element that you just added to your stylesheet so it reads like this:<\/p>\n<div class=\"gist\" data-gist=\"5c4f7d9babc0f49fb40bac9b3c683596\" data-gist-file=\"Styling for icon\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/5c4f7d9babc0f49fb40bac9b3c683596.js?file=Styling+for+icon\">Loading gist 5c4f7d9babc0f49fb40bac9b3c683596<\/a><div class=\"gist-consent-notice\" style=\"display:none\"><p>Please <a href=\"javascript:Cookiebot.renew()\">update your cookie preferences<\/a> to enable preference cookies to view this gist.<\/p><\/div><\/div>\n<p>This adds a margin above the element to line it up with the text and also gives it a fixed width so that there will be a space between it and the text but the text will be aligned.<\/p>\n<p>Now your menu will look like this:<\/p>\n<div  class=\"wpdui-pic-large   \" >\n<div class=\"image-grid cgrid-row\">\n<div class=\"cgrid-col cgrid-col-span-full\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-139321\" src=\"https:\/\/wpmudev.com\/blog\/wp-content\/uploads\/2015\/03\/menu-icons-correct-layout.png\" alt=\"Menu icons with the correct layout\" width=\"735\" height=\"390\" \/><\/div>\n<\/div>\n<\/div>\n<p>That&#8217;s better. Now the final step is to adjust the font for the menu text.<\/p>\n<p>Adding the Font Awesome classes to each menu item changed the font used for the text as well as the icons, so we need to change that back. I&#8217;m going to add styling for the link within each list item in the menu, as that won&#8217;t affect the icons, and I&#8217;ll use the font from the twenty twelve theme.<\/p>\n<p>In your stylesheet, add this:<\/p>\n<div class=\"gist\" data-gist=\"4c68f69ec4d210d5a7b15f6896b278c1\" data-gist-file=\"Menu text styling\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/4c68f69ec4d210d5a7b15f6896b278c1.js?file=Menu+text+styling\">Loading gist 4c68f69ec4d210d5a7b15f6896b278c1<\/a><div class=\"gist-consent-notice\" style=\"display:none\"><p>Please <a href=\"javascript:Cookiebot.renew()\">update your cookie preferences<\/a> to enable preference cookies to view this gist.<\/p><\/div><\/div>\n<p>This targets the link in any menu item that&#8217;s been given the fa and fa-lg classes, and adjusts the font and font size.<\/p>\n<p>Here&#8217;s what your final stylesheet will consist of:<\/p>\n<div class=\"gist\" data-gist=\"40637e3f0288bc21f5f6886306ce9699\" data-gist-file=\"Final stylesheet\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/40637e3f0288bc21f5f6886306ce9699.js?file=Final+stylesheet\">Loading gist 40637e3f0288bc21f5f6886306ce9699<\/a><div class=\"gist-consent-notice\" style=\"display:none\"><p>Please <a href=\"javascript:Cookiebot.renew()\">update your cookie preferences<\/a> to enable preference cookies to view this gist.<\/p><\/div><\/div>\n<p>Now the menu looks as it should do:<\/p>\n<div  class=\"wpdui-pic-large   \" >\n<div class=\"image-grid cgrid-row\">\n<div class=\"cgrid-col cgrid-col-span-full\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-139322\" src=\"https:\/\/wpmudev.com\/blog\/wp-content\/uploads\/2015\/03\/final-navigation-menu-with-icons.png\" alt=\"The navigation menu with fonts and font size corrected\" width=\"735\" height=\"390\" \/><\/div>\n<\/div>\n<\/div>\n<p>Now my navigation menu is complete!<\/p>\n<h2>Summary<\/h2>\n<p>Adding icons to your site is a very different process from what it was a year or two ago. Now, instead of uploading images and using styling to place them in the background of elements\u00a0in the page, you can add icons using a font-based icon library.<\/p>\n<p>In this post you&#8217;ve learned how to use the Font Awesome library to add icons to your menu items and then style them so that they look tidy and the text font is as it should be. You can use this technique with other elements in your site, such as lists, callouts and buttons, simply by using the CSS classes provided by the Font Awesome library.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Adding icons to your navigation gives your visitors some visual clues about the content and also adds a nice design touch to your site. In today&#8217;s post we show you how to do this without using plugins or having to create and upload any images.<\/p>\n","protected":false},"author":347011,"featured_media":199307,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"blog_reading_time":"","wds_primary_category":0,"wds_primary_tutorials_categories":0,"footnotes":""},"categories":[263],"tags":[390,87],"tutorials_categories":[],"class_list":["post-139311","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials","tag-code","tag-menus"],"_links":{"self":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts\/139311","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/users\/347011"}],"replies":[{"embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/comments?post=139311"}],"version-history":[{"count":13,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts\/139311\/revisions"}],"predecessor-version":[{"id":206787,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts\/139311\/revisions\/206787"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/media\/199307"}],"wp:attachment":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/media?parent=139311"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/categories?post=139311"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/tags?post=139311"},{"taxonomy":"tutorials_categories","embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/tutorials_categories?post=139311"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}