# VuePress Tutorial 9 - Styling the Homepage
# Styling Files
The next step we'll be taking in developing the blog is learning how to override the palette.styl and index.styl files which will allow us to add our own global styling to the blog.
To do this we're going to first discuss what the index.styl and palette.styl files are used for. Then we'll take a look at the predefined styling files provided by the default theme (opens new window), and we'll discuss the overriding priority of the files. Finally, we'll implement what we leaned about by styling the homepage of the blog.
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.
# palette.styl
The palette.styl file is used to define global styling variables and override the predefined styling variables which in our case are being provided by the default theme (opens new window).
Here are some of the predefined styling variables available to use and override in your site:
We'll be using and overriding some of these predefined styling variables as well as defining our own styling variables as we develop the blog.
Here's a link to the full palette.styl file (opens new window) if you're interested. You can also view the full file by navigating to node_modules/@vuepress/core/lib/client/style/config.styl in your project.
Only Define Variables in palette.styl
You should ONLY define variables in the palette.styl file because it will be imported at the end of the root Stylus config file. This means several files will use it, so once you define your styling variables here they will be duplicated multiple times
# Creating the palette.styl File
Now that we have a good understanding of the palette.styl file, we're going to create the file by first creating a styles directory in the theme directory. The docs directory for your site should now look something like this:
.
├── docs
│ ├── .vuepress
│ │ ├── public
│ │ ├── theme
│ │ │ ├── global-components
│ │ │ ├── layouts
│ │ │ ├── styles
│ │ │ └── index.js
│ │ │
│ │ └── config.js
│ │
│ ├── icons
│ └── README.md
After creating the styles directory, we can now create the palette.styl file inside of it like this:
.
├── docs
│ ├── .vuepress
│ │ ├── public
│ │ ├── theme
│ │ │ ├── global-components
│ │ │ ├── layouts
│ │ │ ├── styles
│ │ │ │ └── palette.styl
│ │ │ └── index.js
│ │ │
│ │ └── config.js
│ │
│ ├── icons
│ └── README.md
Alternative Directory Structure
If you recall from the VuePress Tutorial 4 - Directory Structure post, you also have the option of creating your palette.styl file in the following location .vuepress/styles/palette.styl.
We'll be updating the palette.styl file later on in this post, but first we're going to take a look at the index.styl file.
# index.styl
The index.styl file is used to add global styles as well as override predefined styles which in our case are being provided by the default theme (opens new window).
Even though it's a Stylus (opens new window) file, you can also use normal CSS syntax if you prefer.
We won't discuss the predefined index.styl file in detail right now. It's just meant to be a reference in case you want to look for a specific style you want to override, or if you're interested in learning more about the predefined styling.
Here's the predefined index.styl which you can override and add your own styles to:
Here's a link to the index.styl file (opens new window) as well and the path to the file in your project node_modules/@vuepress/theme-default/styles/index.styl.
As we develop the blog we'll be using and overriding some of these predefined styles as well as defining our own styles.
Importing and Requiring Stylus and CSS Files
You may have noticed in the index.styl file that we're requiring other files, e.g., @require './config'. We'll discuss how to import and require .styl and .css files in a future post.
# Creating the index.styl File
Now that we have a good understanding of the index.styl file, we're going to create the file for the blog inside of the styles directory that we previously created. The docs directory for your site should now look something like this:
.
├── docs
│ ├── .vuepress
│ │ ├── public
│ │ ├── theme
│ │ │ ├── global-components
│ │ │ ├── layouts
│ │ │ ├── styles
│ │ │ │ ├── index.styl
│ │ │ │ └── palette.styl
│ │ │ └── index.js
│ │ │
│ │ └── config.js
│ │
│ ├── icons
│ └── README.md
Alternative Directory Structure
If you recall from the VuePress Tutorial 4 - Directory Structure post, you also have the option of creating your index.styl file in the following location .vuepress/styles/index.styl.
We'll be updating the index.styl file we just created later on in this post, but first we're going to take a look at the overriding priority.
# Overriding
Both the palette.styl and index.styl files follow a certain overriding priority.
# palette.styl
The user's palette.styl file which is located in .vuepress/styles/palette.styl has a higher priority than the theme's palette.styl file which is located in theme/styles/palette.styl. The theme's palette.styl file has a higher priority than any predefined palette.styl file which is located in node_modules/@vuepress/core/lib/client/style/config.styl in the case of the default theme (opens new window).
This means a theme can define it's own global styling variables and a user can override them as they see fit.
Here's an example of a global styling variable defined in each type of palette.styl file to make this concept clearer:
Here, the final value for $accentColor will be #ccff33.
Absence of User palette.styl File
As we develop the blog we won't be using the user's palette.styl file located in .vuepress/styles/palette.styl We'll just be using the theme's palette.styl file located in theme/styles/palette.styl which will allow us to override any predefined variables.
# index.styl
The same overriding priority applies to the index.styl file as well. So, a user's index.styl file which is located in .vuepress/styles/index.styl has a higher priority than the theme's index.styl file which is located in theme/styles/index.styl. The theme's index.styl file has a higher priority than any predefined index.styl file which is located in node_modules/@vuepress/theme-default/styles/index.styl in the case of the default theme (opens new window).
This means a theme can define it's own global styles and a user can override them as they see fit.
Both the user's index.styl file and the theme's index.styl file will get generated into the final CSS file used in the site, but the user's style gets generated later which is what gives it a higher priority than the theme's style.
Here's an example of a style defined in a user's index.styl and a style defined in a theme's index.styl file to make this concept clearer:
Here's the final CSS file generated for the site:
Notice how the user's style comes after the theme's style in the final CSS file. This is what gives the user's style a higher priority than the theme's style.
Absence of User index.styl File
As we develop the blog we won't be using the user's index.styl file located in .vuepress/styles/index.styl. Instead we'll just be using the theme's index.styl file located in theme/styles/index.styl which will allow us to override any predefined styles.
Now that we have a good understanding of how overriding works for both the palette.styl and index.styl files, we're now ready to override some predefined styling and add our own styling to the homepage of the blog.
# Homepage Styles
To start the homepage styling we'll be overriding and adding global styling variables to the palette.styl file. These global styling variables will then be used in the index.styl file as well as other files as we continue the development of the blog.
When overriding and adding styles to your site you can look at the predefined styling files to determine what properties you need to override. Along with looking at the predefined styling files you can also inspect your browser, go to the Elements tab, and then locate a tag or class that you want to override.
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.
If you're developing your own site, then you can check out these useful resources for coming up with color schemes and palettes for your site:
- Coolors (opens new window)
- Canva Color Palettes (opens new window)
- Colors Tutorial (opens new window)
- HTML Color Picker (opens new window)
If you don't feel comfortable with the CSS discussed below, then here's a good resource to go through CSS Tutorial (opens new window).
You can also view the palette.styl and index.styl files in the tutorial-9 branch of the code-monkeys-blog-tutorials (opens new window) repository.
Now let's override and add some global styling variables to the palette.styl file.
# palette.styl
Here's what the palette.styl file looks like after overriding some predefined global styling variables and adding our own global styling variables:
$backgroundColoris used as the background color of the homepage as well as the whole blog.$accentColoris used to color the action button on the homepage and to add styling to internal and external links.$textColoris used to color the text on the homepage as well as the navbar text and other text throughout the blog.$borderColoris used to style the border in the features section as well as the border in the navbar and other borders throughout the blog.$darkBorderColorwill be used to style the border and box shadow around the main card on the homepage.
Now let's take a look at overriding and adding some styles to the index.styl file.
# index.styl
We're going to start by overriding the background-color in the html tag:
- Updates the
background-colorof the content below theFootercomponent to be the color specified by the$backgroundColorglobal styling variable we just added in thepalette.stylfile.
Next we'll override the background-color in the body tag:
- Updates the
background-colorof thebodyof the site to be the color specified by the$backgroundColorglobal styling variable.
Now we're going to update the padding around the main tag which we'll select using the home class:
- Updates the
paddingof themaintag with a class ofhometo be zero on all sides.
We're now going to update the styles for the hero class which is given to the header tag inside of the main tag and contains the logo, the title, the description, and the action button.
margin-left: 2remadds a margin of2remto the left of theheadertag.margin-right: 2remadds a margin of2remto the right of theheadertag.border: 0.125rem solid $darkBorderColoradds a border around theheadertag with a thickness of0.125rem, with a style ofsolid, and has a color of$darkBorderColorwhich was defined in thepalette.stylfile.box-shadow: 0 0.5rem 1rem 0 $darkBorderColoradds a box shadow around theheadertag.border-radius: 5.625remadds a border radius to theheadertag border.margin-bottom: 0.625remadds a margin of0.625remto the bottom of theheadertag.padding: 2remadds padding of2remaround theheadertag.
Here's a resource for the CSS box-shadow Property (opens new window) if you are interested in learning more about how it works.
Finally, we're going to update the styles for the features class which is given to the div tag inside of the main tag and contains the Learn, Code, and Crush text.
margin-left: 2remadds a margin of2remto the left of thedivtag.margin-right: 2remadds a margin of2remto the right of thedivtag.border-top: 0.125rem solid $borderColoradds a border to the top of thedivtag with a thickness of0.125rem, with a style ofsolid, and has a color of$borderColorwhich was defined in thepalette.stylfile.border-bottom: 0.125rem solid $borderColoradds a border to the bottom of thedivtag with a thickness of0.125rem, with a style ofsolid, and has a color of$borderColorwhich was defined in thepalette.stylfile.text-align: centerhorizontally aligns the text in eachdivtag with a class offeaturethat are inside of thedivtag with a class offeatures.font-size: 1.4remsets a font size of1.4remfor theh2tags that are inside of thedivtag with a class offeature.
# Next Steps
You may have noticed the main card on the homepage is being covered by the navbar. In the next tutorial we'll be fixing this issue as well as applying some hover effects to the logo and to the box shadow around the main card on the homepage.