#11 Main Navigation (Logo)
Saturday, December 29, 2018
In this article we will start creating the header of our web pages which will contain the main navigation component. We will start by including a css reset and some basic styling by importing a few files into our main sass file. Once that is done we will create the basic layout of the main navigation starting with a logo area which we will style in a separate sass file and once again importing into our main sass file.
Parts
- Part 29: Offset Pager Urls
- Part 28: Offset Pager Start
- Part 27: Mock Context Builder
- Part 26: Mock Repository
- Part 25: Mock Async
- Part 24: Picture Tag Helper
- Part 23: Img DPR Tag Helper
- Part 22: Img Responsive Tag Helper
- Part 21: Img Optimized Display
- Part 20: Img Optimization
- Part 19: Img Lazy Loading
- Part 18: Img Responsive
- Part 17: Bottom Nav
- Part 16: Main Nav Cookie
- Part 15: Main Nav Mobile
- Part 14: Main Nav Search
- Part 13: Main Nav Auth
- Part 12: Main Nav Anchors
- Part 11: Main Nav Logo
- Part 10: Search Results
- Part 9: Search Manager
- Part 8: Search Start
- Part 7: Seeding the Database
- Part 6: Domain Database
- Part 5: Emailing Exceptions
- Part 4: Mailkit
- Part 3: View Renderer
- Part 2: Upgrade to 2.1
- Part 1: Quick Start
Including some base styling
- WebUi
- package.json
To start things off we are going to include a css reset. In this case we are going to use normalize.css . We can install the npm package using the command shown in (a) and as of the writing of this article the version number is shown in (b).
Terminal
npm install --save-dev normalize.css
package.json
{
...
"devDependencies": {
...
"normalize.css": "^8.0.0",
...
}
}
Including some basic styling
- WebUi
- Source
- main.site.scss
- Source
Along with the css reset that we just included I am also going to be using some base styling and addition functions and mixins provided by bourbon and bitters . We are going to include them both by adding a couple of include statements in our main sass (c) file where we are also going to add a little extra styling just while we are developing our main navigation.
main.site.scss
@import "../node_modules/normalize.css/normalize.css";
@import "./sass/0-bourbon/_bourbon.scss";
@import "./sass/1-base/_base.scss";
body { /* Only for testing...remove when done */
height: 3000px;
background-color: aqua;
.body-content {
background-color: yellow;
padding-top: em(56.33px);
}
}
Starting the main navigation
- WebUi
- Pages
- Shared
- _MainNav.cshtml
- Shared
- Pages
The next step is to create the starting html structure of our main navigation (d). We are going to break the creation of the header bar into different pieces and the first is going to be the logo area.
_MainNav.cshtml
<header>
<nav class="main-navigation">
<a href="/" class="logo">
<div class="logo-img">
<img src="/images/logo.svg" alt="Exciton Interactive Logo"/>
</div>
<div class="logo-text">
Exciton Interactive
</div>
</a>
</nav>
</header>
Adding the main navigation to our page
- WebUi
- Pages
- Shared
- _Layout.cshtml
- Shared
- Pages
Since we will want this header bar to included in every page we will add it to our common layout page (e). With that done if we refresh the browser we should see something like what is shown in (f).
_Layout.cshtml
...
<body>
<partial name="_MainNav"/>
<div class="body-content">
@RenderBody()
</div>
...
</body>
...
Time for some styling
- WebUi
- Source
- components
- main-nav
- main-nav.component.scss
- main-nav
- components
- Source
In order to add some additional styling to the header we will create a separate file that will contain the sass (g). For now it of course just contains the styling for the header itself and the logo.
main-nav.component.scss
@import "../../sass/_non-rendering.scss";
$dark-gray: #333 !default;
$navigation-anchor-color: lighten($dark-gray, 20%) !default;
$navigation-background-color: white !default;
$navigation-border-color: $navigation-anchor-color !default;
$mobile-break-width: 1024px !default;
$gutter: 5px !default;
header {
position: fixed;
width: 100%;
z-index: 2000;
.main-navigation {
display: grid;
grid-template-areas: "logo";
grid-template-columns: auto;
background-color: $navigation-background-color;
border-color: $navigation-border-color;
border-style: solid;
@include border-width(0 0 1px 0);
@include padding(null em($gutter));
position: relative;
z-index: 1;
@include media-gt($mobile-break-width) {
grid-template-areas: "logo";
grid-template-columns: auto;
}
}
.logo {
grid-area: logo;
display: flex;
flex-direction: row;
align-items: center;
padding-left: 0;
.logo-img {
display: flex;
flex-direction: row;
align-items: center;
width: em(48px);
}
.logo-text {
display: none;
white-space: nowrap;
font-size: 1.5em;
@include margin(null null em(2px) em(10px));
font-weight: 600;
@include media-gt-or-eq($mobile-break-width) {
display: block;
}
}
}
}
Styles do not help unless they are included
- WebUi
- Source
- main.page.scss
- Source
In order to include the styles for the header we will just add another import statement to our main sass file (h). With that file being imported if we check our browser now we should see our header and main navigation should look something like (i).
main.page.scss
...
@import "./components/main-nav/main-nav.component.scss";
...
Common anchor styles
- WebUi
- Source
- components
- main-nav
- main-nav.component.scss
- main-nav
- components
- Source
There is a common styling that I would like to have applied to all anchor tags within our main navigation so we will return to the main navigation sass file and add them now (j). And with this change our logo area should look like (k). Which takes care of this section of our main navigation.
main-nav.component.scss
...
header {
...
a {
display: flex;
align-items: center;
text-decoration: none;
color: $navigation-anchor-color;
height: 100%;
@include padding(null em(10px));
}
...
}