Tech Junkie Blog - Real World Tutorials, Happy Coding!: Hour 7: ASP.NET Core : Create A Responsive Layout With Bootstrap 4+

Wednesday, July 31, 2019

Hour 7: ASP.NET Core : Create A Responsive Layout With Bootstrap 4+

In our previous blog we created a simple _Layout.cshtml file that does not have any markup just to make things simple.  In this blog we will use Bootstrap to make the layout look more professional and responsive, so that it can be viewed in any screen size.  The previous layout looks like screenshot below.




The Bootstrap version will look like the screenshot below.


Step-By-Step Instructions:

1.  Open the NorthwindCafe.Web project

2. Make sure the markup for the index.cshtml is only the bare essential without the head or body tag, it should have only this markup

<h3>This is from Index.cshtml</h3>

3.  Now we are ready to add the header navigation to the _Layout.cshtml file, which is the layout specified as the default layout in the _ViewStart.cshtml file.

4. Create a folder call "css" in the wwroot folder, and add a blank css file call "Styles.css"

5.  Open the _Layout.cshtml file, then type in the following code

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>@ViewBag.Title</title>
    <link href="~/lib/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet" />
    <link href="~/css/Styles.css" rel="stylesheet" />
    <script src="~/lib/jquery/dist/jquery.min.js"></script>
    <script src="~/lib/bootstrap/dist/js/bootstrap.min.js"></script>
    @RenderSection("head", required: false)
</head>

The code above defines the viewpoint as the device-width, a viewport is the width of the screen used to determine how bootstrap will display the elements on the screen. The device-width viewport means that the elements will be render to the screen's device width. The rest of the code just links to Bootstrap css and JavaScript files, and jQuery.  You can find these files in the wwwroot → lib folder

















6.  Once we have the head section defined we can define the header section of the layout in the _Layout.cshtml file

    <body>
        <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="#">Home</a></li>
                                    <li><a href="#about">About</a></li>
                                    <li><a href="#contact">Contact</a></li>
                                    <li class="dropdown">
                                        <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">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 Store</h2>
            </div>
        </header>

The code above looks complicated, but it's not that complicated. First we use the bootstrap class "navbar navbar-default navbar-fixed-top" to tell the browser that we want the navbar that is fixes on the top of the screen even when the user scrolls. All we have to do is define the class and bootstrap does the rest for us.  The navbar class defines the top navigation of the layout like the screenshot below


















7.  The following section defines the menu when the screen gets really small and the elements have to be stacked because it does not have the width to display everything horizontally.

                 <div class="navbar-header">
                    <a class="navbar-brand" href="#"></a>
                    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
                        <span class="navbar-toggler-icon"></span>
                    </button>
                </div>

The menu looks like the following when the screen gets really small


8.  Now we want to define the actual navigation bar, type in the following code to define the "Home", "About","Contact" tabs, and the "Products" drop down.


                 <div id="navbarSupportedContent" class="collapse navbar-collapse">
                    <ul class="navbar-nav mr-auto">
                        <li class="nav-item active"><a class="nav-link" href="#">Home</a></li>
                        <li class="nav-item"><a class="nav-link" href="#about">About</a></li>
                        <li class="nav-item"><a class="nav-link" href="#contact">Contact</a></li>
                        <li class="dropdown">
                            <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                                Products</a>
                                <ul class="dropdown-menu" role="menu">
                                    <li><a class="dropdown-item" href="#">Beverages</a></li>
                                    <li><a class="dropdown-item" href="#">Condiments</a></li>
                                    <li><a class="dropdown-item" href="#">Confections</a></li>
                                    <li><a class="dropdown-item" href="#">Dairy Products</a></li>
                                    <li><a class="dropdown-item" href="#">Grains/Cereals</a></li>
                                    <li><a class="dropdown-item" href="#">Meat/Poultry</a></li>
                                    <li><a class="dropdown-item" href="#">Produce</a></li>
                                    <li><a class="dropdown-item" href="#">Seafood</a></li>
                                </ul>
                        </li>
                    </ul>
                </div><!--/.nav-collapse -->

Like everything else in bootstrap we just have to call class "collapse navbar-collapse " and nest the div with the <ul> element with class "navbar-nav" and bootstrap picks it up applies the stylesheets and functionalities automatically. "nav-item", "nav-link", "dropdown-menu" are pretty self explanatory.  That's the great thing about bootstrap. It makes things easy, but the bad thing is a lot of the sites nowadays have the same bootstrapy look and feel to them. But it's great if you want to throw something together quickly and make it look decent.

9. Now we want to add a jumbotron to make the navigation stand out a little bit.

           <div class="jumbotron" id="header-jumbotron">
                        <h2>Northwind Cafe</h2>
            </div>

The code above produces the following output













10.  To style the jumbotron we have to create our own stylesheet so that it does not interfere with the default bootstrap styles.  So create a new css file in the wwwroot → css folder  and give it the name Styles.css.

11.  In the Styles.css file type the following code

body {
}

#header-jumbotron{
    background-color: #6f5499;
}
#navbar {
    margin-left: 50px;
}

#header-jumbotron h2{
    margin-left: 40px;
    color: #fff;
    margin-top: 50px;
}


The code above defines the margin for the navbar and the jumbotron, it also set the text color and background color for the jumbotron.

12. The complete code for the _Layout.cshtml file is the following

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>@ViewBag.Title</title>
    <link href="~/lib/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet" />
    <link href="~/css/Styles.css" rel="stylesheet" />
    <script src="~/lib/jquery/dist/jquery.min.js"></script>
    <script src="~/lib/bootstrap/dist/js/bootstrap.min.js"></script>
    @RenderSection("head", required: false)
</head>
<body>
    <header>
        <nav class="navbar navbar-expand-lg navbar-light bg-light fixed-top">
            <div class="row">
                <div class="navbar-header">
                    <a class="navbar-brand" href="#"></a>
                    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
                        <span class="navbar-toggler-icon"></span>
                    </button>
                </div>
                <div id="navbarSupportedContent" class="collapse navbar-collapse">
                    <ul class="navbar-nav mr-auto">
                        <li class="nav-item active"><a class="nav-link" href="#">Home</a></li>
                        <li class="nav-item"><a class="nav-link" href="#about">About</a></li>
                        <li class="nav-item"><a class="nav-link" href="#contact">Contact</a></li>
                        <li class="dropdown">
                            <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                                Products</a>
                                <ul class="dropdown-menu" role="menu">
                                    <li><a class="dropdown-item" href="#">Beverages</a></li>
                                    <li><a class="dropdown-item" href="#">Condiments</a></li>
                                    <li><a class="dropdown-item" href="#">Confections</a></li>
                                    <li><a class="dropdown-item" href="#">Dairy Products</a></li>
                                    <li><a class="dropdown-item" href="#">Grains/Cereals</a></li>
                                    <li><a class="dropdown-item" href="#">Meat/Poultry</a></li>
                                    <li><a class="dropdown-item" href="#">Produce</a></li>
                                    <li><a class="dropdown-item" href="#">Seafood</a></li>
                                </ul>
                        </li>
                    </ul>
                </div><!--/.nav-collapse -->
            </div>

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


























No comments:

Post a Comment

Search This Blog