Are there ways to incorporate extra icons into NavMenu in Blazor 8?

I am exploring ways to incorporate extra icons into the NavMenu.razor component of my Blazor version 8 application. In the earlier version, Blazor 7, there was an iconset setup located in wwwroot/css/, which allowed me to include additional icons simply by adding their names in the NavMenu.razor. However, I have noticed that the entire css folder is not present in the new Blazor 8 template.

Currently, there are only three icons defined in the NavMenu.razor.css file, and I am unsure how to include more icons there. As someone who is not well-versed in CSS, I am curious if there is an easy way to obtain new icons similar to how it was done in Blazor 7?

Answer №1

Absolutely, it is definitely possible.
Just keep in mind that doing so could significantly increase the download size for your users.

  1. Within App.razor, insert the following line of code:

    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9dfff2f2e9eee9effcedb0f4fef2f3eeddacb3acacb3ae">[email protected]</a>/font/bootstrap-icons.min.css">

  2. In NavMenu.razor, make sure to remove the -nav-menu suffix from the icon names (e.g., use bi-house-door-fill)

  3. Delete the old icons from NavMenu.razor.css file.

  4. Adjust the .bi class within NavMenu.razor.css by adding top: -0.75rem;

The last step addresses aligning the icons quickly, but you may need to further refine the styling of that class as needed.

Answer №2

After receiving feedback from Kurt, I was able to find a solution to my issue with the icons. Instead of using standard icons, I switched to icons from a linked library called Blazorise.Icons.FontAwesome. You can find more information about this library here: .

To implement this change, I modified a navigation point in the code for NavMenu.razor:

<NavLink class="nav-link" href="counter">
    <Icon Name="IconName.Add" IconSize="IconSize.Large" />  @* new *@
    <span style="margin-right:0.8em;"></span> Counter  @* new *@
    @*<span class="bi bi-plus-square-fill-nav-menu" aria-hidden="true"></span> Counter *@   @* old *@
</NavLink>

All available icons can be found in the documentation provided.

While I may not know how to use certain standard icons like the ones included in the template, by utilizing an alternative library, I am able to access a wider range of icons as needed.

Answer №3

If you're interested in learning how to customize Blazor's NavMenu, I detailed the process here.

  1. First step is to select a desired icon from Bootstrap Icon library
  2. Next, copy the SVG code of the chosen icon to your clipboard
  3. Then proceed to URL encode the copied SVG code
  4. Find an existing style like .bi-list-nested-nav-menu in NavMenu.razor.css and duplicate it with a new name
  5. Finally, insert the URL-encoded SVG code after "data:image/svg+xml," within your newly created style

Answer №4

After perusing through MATT'S WORK BLOG (mentioned below), I devised a PowerShell script to accomplish this task efficiently. The script generates an output that can seamlessly fit into a Blazor 8 project. Just specify the Bootstrap icon name (as identified in the [Bootstrap Icon Link][1])

The generated output will be ready for implementation.

Insert the following Generated CSS snippet into your NavMenu.razor.css:

.bi-1-circle-nav-menu {
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' fill='white' class='bi bi-1-circle' viewBox='0 0 16 16'%3E  %3Cpath d='M1 8a7 7 0 1 0 14 0A7 7 0 0 0 1 8m15 0A8 8 0 1 1 0 8a8 8 0 0 1 16 0M9.283 4.002V12H7.971V5.338h-.065L6.072 6.656V5.385l1.899-1.383z'/%3E%3C/svg%3E");

}

Embed the following HTML code within your NavMenu.razor:

<div class="nav-item px-3">
<NavLink class="nav-link" href="authorform">
    <span class="bi-1-circle-nav-menu" aria-hidden="true"></span> Dashboard
</NavLink>
function Add-BlazorNavMenuIconFromUrl {
    param(
        # The name of the Bootstrap icon
        [Parameter(Mandatory = $true)]
        [string]$IconName,  
        # SVG file on GitHub
        [Parameter(Mandatory = $false)]
        [string]$MenuItem = "BlazorPageHere",  
        # URL to the SVG file on GitHub or elsewhere
        [Parameter(Mandatory = $false)]
        [string]$BlazorPage = "BlazorPageHere"  

    )

    try {
        # Construct the URL to the SVG file on GitHub
        $Url = "https://raw.githubusercontent.com/twbs/icons/main/icons/$IconName.svg"

        # Download the SVG file content directly
        $response = Invoke-WebRequest -Uri $Url
        $BootstrapSvgHtml = $response.Content

        # Process the SVG HTML
        $encodedSvgHtml = $BootstrapSvgHtml -replace 'currentColor', 'white' `
            -replace '"', "'" `
            -replace '<', '%3C' `
            -replace '>', '%3E' `
            -replace "\r?\n", "" 

        # Extract the SVG name from the URL for class naming
        $svgName = [System.IO.Path]::GetFileNameWithoutExtension($Url)
        $IconClassName = "bi-$($svgName)-nav-menu"

        # Create the CSS rule using StringBuilder
        $cssBuilder = New-Object System.Text.StringBuilder
        [void]$cssBuilder.AppendLine(".$IconClassName {")
        [void]$cssBuilder.AppendLine("`tbackground-image: url(`"data:image/svg+xml,$encodedSvgHtml`");")
        [void]$cssBuilder.AppendLine("}")

        $newCssRule = $cssBuilder.ToString()

        Write-Host "Add the following Generated CSS to your NavMenu.razor.css:" -ForegroundColor Yellow
        Write-Host $newCssRule -ForegroundColor Cyan

        $htmlBuilder = New-Object System.Text.StringBuilder
        [void]$htmlBuilder.AppendLine('<div class="nav-item px-3">')
        [void]$htmlBuilder.AppendLine('    <NavLink class="nav-link" href="' + $BlazorPage + '">')
        [void]$htmlBuilder.AppendLine("        <span class=`"bi $IconClassName`" aria-hidden=`"true`"></span> $MenuItem")
        [void]$htmlBuilder.AppendLine('    </NavLink>')
        [void]$htmlBuilder.AppendLine('</div>')

        $htmlMarkup = $htmlBuilder.ToString()
        
        Write-Host "Add the following HTML to your NavMenu.razor:" -ForegroundColor Yellow
        Write-Host $htmlMarkup -ForegroundColor Green

        $clipboardBuilder = New-Object System.Text.StringBuilder
        [void]$clipboardBuilder.AppendLine($htmlMarkup)
        [void]$clipboardBuilder.AppendLine()
        [void]$clipboardBuilder.AppendLine()
        [void]$clipboardBuilder.AppendLine($newCssRule)

        Set-Clipboard -Value $clipboardBuilder.ToString()
    } catch {
        Write-Error "An error occurred: $_"
    } 
}
# Run the function with command line arguments
if ($args.Count -ne 3) {
    $ScriptName = ($MyInvocation.MyCommand.Name).Replace(".ps1","")
    Write-Host "Usage: $ScriptName <IconName> <MenuItem> <BlazorPage>"
    exit
}
Add-BlazorNavMenuIconFromUrl `
    -IconName $args[0] `
    -MenuItem $args[1] `
    -BlazorPage $args[2]

# Example usage:

    Add-BlazorNavMenuIconFromUrl -IconName "1-circle" -BlazorPage "/dashboard" -MenuItem="User Dashboard"

  [1]: https://icons.getbootstrap.com/

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is there a way for me to determine which .js script is modifying a particular HTML element?

Let's take a look at a specific website as an example: This particular website calculates value using a .js script embedded in the HTML itself. Upon inspecting the source code by pressing F12, we can locate the element containing the calculated valu ...

Strange occurrences observed with the interaction of multiple CSS classes

Recently, I encountered a situation with a class that had me puzzled. .lifetime .containerrow { text-align: center; height: 20px; } To enhance the appearance of certain elements by making their text bold, I attempted the following: .lifetime .co ...

Tips for avoiding cursor sticking in css rotate transform in firefox?

I have a unique challenge in this code where I want the user to grab the black square and rotate it around the inner circle. Check out the code snippet here. While attempting to rotate the square, you might notice that the cursor sometimes gets stuck in ...

Issue with PHP page not properly connecting to the recently updated CSS file

Yesterday, I successfully linked my CSS in the head tag right below the title tag using this code: <link href="css/main.css" rel="stylesheet"> Yesterday, everything was working fine with the styles. However, today when I try to add new styles, the ...

Updated INNER header and footer - unrelated to the answered questions

After observing, the inner-header and inner-footer div scroll along with the area that contains the select list. How can I make that area scroll down while keeping the inner-header/footer fixed within the content? I hope my query is understandable. Thank ...

Adjust the width of a div in Angular 6 based on a specified condition

I need to adjust the width of a div based on certain conditions. <div [ngStyle]="'width': selectedTab =='Home' ? '50%' : '100%'"> </div> The currently selected tab is stored in "selectedTab". There ...

Tips for making a div expand to take up the available space on the left side of a taller div

Could you take a look at the scenario below? I'm attempting to position my yellow div beneath the red div and on the left side of the lower part of the green div. When I apply clear: left, it moves down but leaves empty space above it. Is there a way ...

Is there a way for me to scroll and bring the block up to the header when the button is clicked?

My goal is to create a functionality where clicking on the button with the class .booking__button will smoothly scroll the block up under the header. The position of the block should remain consistent, only the scrolling effect should be visible to the use ...

What is the method for reverting style properties back to their CSS defaults using Javascript?

The php-generated page contains multiple elements structured like this: <td class="defaultTDStyle" style="color:userDefinedCustomColor" id="myTDId"></td> There is a default style in place with additional styles ap ...

Scaling boxes responsively according to screen size utilizing Bootstrap

Creating a portfolio website with a carousel section to display completed projects in a 2x2 grid layout, transitioning to a 1x4 on smaller screens is proving to be quite challenging. After trying various methods from Bootstrap documentation and YouTube tut ...

Applying a left margin has caused the right border to disappear

After adding a margin-left to the box, I noticed that my right border is missing. You can see the issue in this example. However, when I remove the margin-left property, everything looks fine as shown in this example. Is there any way to solve this proble ...

Looking to adjust the width of a Bootstrap dropdown menu?

I am trying to adjust the width of my dropdown menu Here is a link to jsfiddle with my code: [jsfiddler][1]. On the right side of the main menu, I have a languages dropdown menu. When it expands, its width is very large and I want it to match the main m ...

What causes the discrepancy in pixel size between these two web pages on the same device?

I am currently putting together a portfolio showcasing the projects I have worked on while learning to code. Each project page has a banner at the top, but I'm encountering an issue with one site (Jubilee Austen page) that is unresponsive. After usin ...

Implementing a Fixed Navbar in VueJS on Scroll

I am seeking help with creating a Fixed Navbar on Scrolling using Vue.js. I initially wrote some jQuery code for this functionality, but now I want to transition it to Vue.js. The updated code can be found in a file named navbar.js. Previous jQuery CODE ...

Tips for ensuring a static html element remains visible at the bottom of the screen even when the soft keyboard is opened in iOS Safari

Within a webpage, there is an input field and a fixed div positioned at the bottom of the window using CSS properties such as position:fixed; and bottom:0;. To better illustrate this setup, I have created a Codepen demo which can be viewed here: https://c ...

Issue with input-group background display in bootstrap 4

My new computer is running on Windows 11 with Chrome version 97. There seems to be a bug in the original bootstrap code, as evidenced by the attachment. The issue can be seen on the Bootstrap website By adding "border-radius:0!important" to ".input-group ...

Utilize React Material UI Slider to dynamically adjust Border Radius in your web design

Utilizing React Material UI's components like the slider and button is essential for this project. The main objective is to dynamically change the border radius of the button using the value obtained from the slider. However, there seems to be a chall ...

The lines intersecting with the ethereal image in the background

I'm trying to achieve button links with an image overlay, but so far I've only been able to do it using CSS: section a:link, section a:visited { width: 150px; -moz-border-radius: 10px; -webkit-border-radius: 10px; border-radius: ...

The CSS modifications are only visible in my browser after I delete the browsing data

After setting up a landing page with simple text in the center of the screen using flex, I encountered an issue. Whenever I made changes to the CSS, they would not reflect in my browser unless I cleared the browsing data (history, cookies, etc). This probl ...

When text is wrapped within the <li> tag

In this div, the structure is as follows: <div class="box1" id="infobox"> <h2> Point characteristics: </h2> <div style="padding-left:30px" align="left"> <ul> <li class="infobox_list"><b>X value: </b>< ...