# VuePress Tutorial 13 - Navbar Styling
# What We're Doing
Currently, the navbar is not looking too good, so in this tutorial we're going to begin fixing that by adding some styling. We're going to be targeting these CSS classes provided by the default theme (opens new window): navbar
, site-name
, links
, search-box
, nav-links
, nav-item
, and dropdown-title
.
To locate these classes we're going to take a look at the HTML of the blog by inspecting the browser, going to the Elements
tab, and then selecting an element on the page. After locating the classes, we'll be applying the styling to the index.styl
file which will globally apply the styles. Finally, we'll be describing the styling in detail.
Make sure you start the local development server which should be running at http://localhost:8080/ (opens new window) to see the changes we'll be making to the site. If the changes aren't appearing after you save them, then try restarting your local development server.
When adding the styling updates be sure to add each block of code below one at a time to your project then view the changes in the browser to get a better understanding of what each block is responsible for.
You can view all of the code in this tutorial by going to the tutorial-13
branch of the code-monkeys-blog-tutorials (opens new window) repository.
# Navbar Class
Alright, now we're ready to begin styling the navbar.
# HTML Location
We're going to start by locating where the navbar
class is in the HTML:
From the HTML above we can see that the navbar
class is attached to a header
tag and is a child of the div
tag with a class of theme-container
.
# Styling
This means in the index.styl
file we can nest the navbar
class inside of the theme-container
class and apply the following styles:
display: flex
defines a flex container for all of the direct children of theheader
tag with a class ofnavbar
.align-items: center
vertically centers the flex items, i.e., the direct children of theheader
tag with a class ofnavbar
.height: 4.5rem
sets the height of the navbar to be4.5rem
.background-color: $backgroundColor
sets the background color of the navbar to be$backgroundColor
which is a global styling variable we set in thepalette.styl
file.border-bottom-width: 0.125rem
sets the border bottom width of the navbar to be0.125rem
.
# Site Name Class
After locating and styling the navbar
class, we're ready to move on to the site-name
class.
# HTML Location
Here's the location of the site-name
class in the HTML:
From the HTML above we can see that the site-name
class is attached to a span
tag and is a child of the a
tag with a class of home-link
which is a child of the header
tag with a class of navbar
.
# Styling
This means in the index.styl
file we can nest the site-name
class inside of the navbar
class and apply the following style:
font-size: 1.5rem
sets the font size of the site name in the navbar, i.e., Code Monkeys to be1.5rem
.
# Links Class
We're now ready to style the links
class.
# HTML Location
Here's the location of the links
class in the HTML:
From the HTML above we can see that the links
class is attached to a div
tag and is a child of the header
tag with a class of navbar
.
# Styling
This means in the index.styl
file we can nest the links
class inside of the navbar
class and apply the following styles:
background-color: transparent
sets the background color behind the search box and of the links on the right side of the navbar to betransparent
. This has the effect of showing the background color set in thenavbar
class since it's no longer being covered by the background color set in thelinks
class.height: 3rem
sets the height of thediv
tag which contains the search box and the links on the right side of the navbar to be3rem
.align-items: center
vertically centers the flex items, i.e., the direct children of thediv
tag with a class oflinks
which was given a style ofdisplay: flex
by the default theme (opens new window).
# Search Box Class
We've finished styling the links
class, so let's begin the styling of the search-box
class.
# HTML Location
Here's the location of the search-box
class in the HTML:
From the HTML above we can see that the search-box
class is attached to a div
tag and is a child of the div
tag with a class of links
.
# Styling
This means in the index.styl
file we can nest the search-box
class inside of the links
class and apply the following styles:
margin-right: 1.5rem
adds a margin of1.5rem
to the right of the search box.font-size: 1.125rem
sets the font size of the text in theinput
tag to be1.125rem
.background-color: transparent
sets the background color of theinput
tag to betransparent
. This has the effect of showing the background color set in thenavbar
class since it's no longer being covered by the background color set in theinput
tag.border-width: 0.125rem
sets the width of the border around theinput
tag to be0.125rem
.color: $textColor
sets the color of the text in theinput
tag to be$textColor
which is a global styling variable we set in thepalette.styl
file.
Viewing the font-size and color Styles
To see the font-size
and color
styles in action try typing some text in the search box. Once you click in the search box, notice the border gets its color set to the $accentColor
which is a global styling variable we set in the palette.styl
file. This effect is provided by the default theme (opens new window) and uses the focus
pseudo-class.
# Nav Links Class
Now that we have styled the search-box
class and its child input
tag, we're ready to move on to the styling of the nav-links
class.
# HTML Location
Here's the location of the nav-links
class in the HTML:
From the HTML above we can see that the nav-links
class is attached to a nav
tag and is a child of the div
tag with a class of links
.
# Styling
This means in the index.styl
file we can nest the nav-links
class inside of the links
class and apply the following style:
font-size: 1.25rem
sets the font size of the nav links on the right side of the navbar to be1.25rem
.
# Nav Item Class
We're now ready to style the nav-item
class.
# HTML Location
Here's the location of the nav-item
class in the HTML:
From the HTML above we can see that there are four nav-item
classes. Each one is attached to a div
tag and is a child of the nav
tag with a class of nav-links
.
# Styling
This means in the index.styl
file we can nest the nav-item
class inside of the nav-links
class and apply the following styles:
.nav-item:first-child
applies thefirst-child
pseudo-class to the.nav-item
selector. Thefirst-child
pseudo-class applies the styles defined inside of it to the first element among a group of sibling elements which in our case is the firstdiv
tag with a class ofnav-item
in the group of siblingdiv
tags that have the class ofnav-item
.margin-left: 0
adds a margin of0
to the left of the firstdiv
tag with a class ofnav-item
within the group of siblingdiv
tags that have a class ofnav-item
.margin-left: 1.75rem
adds a margin of1.75rem
to the left of all of thediv
tags that have a class ofnav-item
except for the first one since that is being styled by thefirst-child
pseudo-class.
# Dropdown Title Class
After finishing the styling for the nav-item
class, we're now ready to move on to the styling of the dropdown-title
class.
# HTML Location
Here's the location of the dropdown-title
class in the HTML:
From the HTML above we can see that the dropdown-title
class is attached to a button
tag and is a child of the div
tag with a class of dropdown-wrapper
which is a child tag of the div
tag with a class of nav-item
.
# Styling
This means in the index.styl
file we can nest the dropdown-title
class inside of the nav-item
class and apply the following style:
font-size: 1.25rem
sets the font size of the dropdown title, i.e., Posts to be1.25rem
.
If you have questions about the CSS discussed above, then check out these resources:
- CSS Tutorial (opens new window)
- Basic Concepts of Flexbox (opens new window)
- A Complete Guide to Flexbox (opens new window)
- Pseudo-classes (opens new window)
- :focus (opens new window)
- :first-child (opens new window)
# Next Steps
In the next tutorial we'll be styling the dropdown menu in the navbar by updating the background color, border, padding, etc.