Sharing Code and Resources (Part 3)
Adding Static Files
Blazor has the capability
to utilize static files like images, CSS, and JavaScript. By placing our files
in the wwwroot folder, they will be automatically available online and
reachable from the main directory of our website. One advantage of Blazor is
that we can also accomplish this with a library, making it incredibly simple to
share static files through a library.
Blazor ultimately relies
on HTML, which can be styled with CSS. As noted earlier, the default Blazor
templates utilize Bootstrap, and we will also stick with that. You can find a
fantastic website featuring user-friendly Bootstrap themes that are available
for download at https://bootswatch.com/.
CSS versus LESS versus
SASS
CSS represents Cascading
Style Sheets, which allows you to apply styles to the appearance of your
website. LESS denotes Leaner Style Sheets and builds upon CSS. SASS, or
Syntactically Awesome Style Sheets, functions similarly to LESS.
SASS requires less coding
and is simpler to manage. Additionally, it offers benefits like variables,
loops, and more. There are two versions of SASS- SASS and SCSS. We can opt for
SCSS, which is the latest version and utilizes brackets. Since SASS needs to be
transpiled, we require a tool to convert SASS into CSS. Therefore, the initial
step will be to install Web Essentials 2019.
Preparing CSS/SASS
- In Visual Studio, click on the Extensions menu and select Manage Extensions.
- Search for Web essentials 2019, select it from the search result, and then click Download.
- You will be prompted to restart Visual Studio. Please do so (all instances if you have more than one open) to finish the installation. Web Essentials 2019 is a collection of many different extensions that are useful when developing web apps.
- Next, we need to download
the SASS files for Bootstrap. Open a web browser and navigate to https://getbootstrap.com/docs/5.0/getting-started/
download/.
- Click the Download Source button and extract the ZIP file.
- In the MyBlog.Shared project, create a folder called Bootstrap.
- Copy the scss folder from the ZIP file into the Bootstrap folder.
- Next, we need to download a new theme. Navigate to https://bootswatch. com/darkly/.
- In the top menu called Darkly, there are some links. Download _bootswatch. scss and _variables.scss.
- In the MyBlog.Shared project, create three new folders so that the new structure looks like this- Bootswatch/Dist/Darkly.
- Copy _bootswatch.scss and _variables.scss into the Darkly folder.
- Since we have installed Web Essentials, we can now use one of the extensions.
- We can now create a new
SASS file by doing this- Select the wwwroot folder and press Shift +
F2, which will show a small dialog where you can supply a filename. It will
use the file extension to load the correct template. Name the file MyBlogStyle.scss.
- We get some styles with
Blazor that we can move to our SCSS file. We want to keep the styles for the
error message box from site.css.
- Right-click on the MyBlogStyle.scss file and select Web compiler | Compile file. It will create four new files. First, we have MyBlogStyle. css and MyBlogStyle.min.css, which is the generated CSS and a minified version of CSS. They are located under the MyBlogStyle.css node. We also get compilerconfig.json and compilerconfig.json.defaults, which are the settings for the web compiler.
Conclusion
Various files have been
successfully added and all the prerequisites that could be added are in place.
Comments
Post a Comment