Tech Junkie Blog: ASP.NET Core : Adding Font-Awesome For Northwind Cafe Navigation Icons

Monday, August 1, 2016

ASP.NET Core : Adding Font-Awesome For Northwind Cafe Navigation Icons

In this tutorial we will add icons to your navbar.  In the previous tutorial we added a responsive layout with bootstrap.  In this post we will add some icons to your navigation.  Font-Awesome gives you professional looking vector graphics, which are implemented using CSS.  Bootstrap will use Font-Awesome as it's icons starting version 4.0 and beyond. Glyphaicons will go away. So we might as start using it, in anticipation of Bootstrap 4.0.

Just to review this is how our layout looks like in our previous post.

























We are going to add font-awesome icons to the first level of the navigation links.  You can decide what icons goes into the products drop down menu.

Here are the steps to add font-awesome to our project:

1.  Open the NorthwindCafe.Web project and add the font-awesome library to the bower.json file

Add the following line to the bower.json file

"font-awesome": "~4.6.3"

the complete bower.json markup should look like following

{
 "name": "asp.net",
 "private": true,
  "dependencies": {
    "bootstrap": "3.3.6",
    "angular": "~1.6.2",
    "jquery": "2.2.3",
    "font-awesome": "~4.7.0"
  }
}


Save the bower.json file and Visual Studio will bring in the font-awesome library automatically.












2.  Now you should see the font-awesome library in your wwwroot file













3. We are ready to add some icons to your navbars now.  Font-awesome is really easy to use first we need reference the font-awesome.css file.  We will put the file in our _Layout.cshtml file so that we can use it through out the site.  Add the following line to between the <head> section of the layout page.

    <link href="~/lib/font-awesome/css/font-awesome.css" rel="stylesheet" />

Your head markup should look like the following

<head>
    <meta name="viewport" content="width=device-width" />
    <title>@ViewBag.Title</title>
    <link href="~/lib/font-awesome/css/font-awesome.css" rel="stylesheet" />
    <link href="~/lib/bootstrap/dist/css/bootstrap.css" rel="stylesheet" />
    <link href="~/css/Styles.css" rel="stylesheet" />
    <script src="~/lib/jquery/dist/jquery.js"></script>
    <script src="~/lib/bootstrap/dist/js/bootstrap.js"></script>
</head>


4. Navigate to the font-awesome icons page to see which icons you want to use at http://fontawesome.io/icons/



















5. For the "Home" icon we will use the "home" icon in font-awesome





6.  If you click on the text link or the icon itself you will see the code to implement the icon in your website

To add a font-awesome to your website you just need the <i> tag with the class being the font-awesome class name for the icon that you want.  Since the home icon has a class name of "fa-home", the markup is <i class="fa-home"></i>, the aria-hidden="true" is for assistance accessibility technology to ignore it.  So that it does not announce the the icon to the world, since it's purpose is only for decoration and has no other meaning.

7. To add the home icon to the NorthwindCafe.Web project, open the _Layout.cshtml page.  Then next to the home navigation link add the icon with the following code.

<li class="active"><a href="#"><i class="fa fa-home" aria-hidden="true"></i>&nbsp;Home</a></li>

The navigation should now look like this















8.  I have choose the fa-coffee for "About", fa-envelope for "Contact" and fa-product-hunt for "Products"

The navigation should look like this in desktop mode















And look like the following in mobile mode:



















Your final markup should look like the following:

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>@ViewBag.Title</title>
    <link href="~/lib/font-awesome/css/font-awesome.css" rel="stylesheet" />
    <link href="~/lib/bootstrap/dist/css/bootstrap.css" rel="stylesheet" />
    <link href="~/css/Styles.css" rel="stylesheet" />
    <script src="~/lib/jquery/dist/jquery.js"></script>
    <script src="~/lib/bootstrap/dist/js/bootstrap.js"></script>
</head>
<body>
    <div>
        <header>
            <nav class="navbar navbar-default navbar-fixed-top">
                <div class="row">
                    <div class="navbar-header">
                        <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
                            <span class="sr-only">Toggle navigation</span>
                            <span class="icon-bar"></span>
                            <span class="icon-bar"></span>
                            <span class="icon-bar"></span>
                        </button>
                        <a class="navbar-brand" href="#"></a>
                    </div>
                    <div id="navbar" class="navbar-collapse collapse">
                        <ul class="nav navbar-nav">
                            <li class="active"><a href="#"><i class="fa fa-home" aria-hidden="true"></i>&nbsp;Home</a></li>
                            <li><a href="#about"><i class="fa fa-coffee" aria-hidden="true"></i>&nbsp;About</a></li>
                            <li><a href="#contact"><i class="fa fa-envelope" aria-hidden="true"></i>&nbsp;Contact</a></li>
                            <li class="dropdown">
                                <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"><i class="fa fa-product-hunt" aria-hidden="true"></i>&nbsp;Products <span class="caret"></span></a>
                                <ul class="dropdown-menu" role="menu">
                                    <li><a href="#">Beverages</a></li>
                                    <li><a href="#">Condiments</a></li>
                                    <li><a href="#">Confections</a></li>
                                    <li><a href="#">Dairy Products</a></li>
                                    <li><a href="#">Grains/Cereals</a></li>
                                    <li><a href="#">Meat/Poultry</a></li>
                                    <li><a href="#">Produce</a></li>
                                    <li><a href="#">Seafood</a></li>
                                </ul>
                            </li>
                        </ul>
                    </div><!--/.nav-collapse -->
                </div>

    </div>
    </nav>
    <div class="jumbotron" id="header-jumbotron">
        <h2>Northwind Cafe</h2>
    </div>
    </header>
    @RenderBody()
    </div>
</body>
</html>


ASP.NET Core Posts:
  1. How To Create An ASP.NET Core Application From Scratch
  2. ASP.NET Core : Add jQuery, Bootstrap, AngularJS Using bower.json
  3. Enable ASP.NET Core to Serve Static Files
  4. Enable MVC On ASP.NET Core Application
  5. ASP.NET Core : Create Our First Controller and View  
  6. ASP.NET Core : Adding The Default View With _ViewStart.cshtml
  7. ASP.NET Core : Create A Responsive Layout With Bootstrap
  8. ASP.NET Core : Adding Font-Awesome For Northwind Cafe Navigation Icons
  9. ASP.NET Core : Add .json Configuration Files With Microsoft.Extensions.Configuration Library
  10. ASP.NET Core : Entity Framework Core Models For Northwind Cafe
  11. ASP.NET Core : Create The NothwindContext ( EntityFrameworkCore )
  12. ASP.NET Core : Configure project.json File To Support Entity Framework Core
  13. ASP.NET Core : Add NorthwindContext To Startup Class And Create Database
  14. ASP.NET Core: Seeding The NorwindCafe Database
  15. ASP.NET Core: Add Logging To The NorthwindCafe Application



No comments:

Post a Comment