Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7769051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:04:17+00:00 2026-06-01T16:04:17+00:00

I wrote a custom HtmlHelper to apply… class = active to a menu item

  • 0

I wrote a custom HtmlHelper to apply…

class = "active"

to a menu item if that is the current page. After viewing the source, I know that this is working correctly, but within my current set-up, I can’t seem to target the .active class to apply any styles to it.

Current HTML

<ul id="menu">
    <li>@Html.MenuItem("Home", "Index", "Home")</li>
    <li>@Html.MenuItem("About", "About", "Home")</li>
</ul>

Current CSS

#menu { width: 100%;
      padding: 0;
      margin: 0;
      list-style-type: none;
      padding-top: 6px; }

#menu a { width: 100%;
            font-weight: bold;
            text-decoration: none;
            color: #ffffff;
            padding: 6px 8px 6px 8px;
            text-shadow: 1px 1px 1px #8292a2; }

#menu a:hover { background-color: #e8eff6; color: #2a3744; text-shadow: 1px 1px 1px #e8eff6; }

#menu li { display: inline; }

I am assuming I have a specificity issue going on here, but I can’t seem to target the “active” class to apply any type of style to it.

#menu a.active
#menu li a.active

Nothing seems to work. Any ideas?

EDIT:
custom HtmlHelper I am using

public static class MenuExtensions
    {
        public static MvcHtmlString MenuItem(
            this HtmlHelper htmlHelper,
            string text,
            string action,
            string controller
        )
        {
            string value;
            var routeData = htmlHelper.ViewContext.RouteData;
            var currentAction = routeData.GetRequiredString("action");
            var currentController = routeData.GetRequiredString("controller");
            if (string.Equals(currentAction, action, StringComparison.OrdinalIgnoreCase) &&
                string.Equals(currentController, controller, StringComparison.OrdinalIgnoreCase))
            {
                value = htmlHelper.ActionLink(text, action, new { controller = controller }, new { @class = "active" }).ToHtmlString();
                return MvcHtmlString.Create(value.ToString());
            }

            value = htmlHelper.ActionLink(text, action, controller).ToHtmlString();
            return MvcHtmlString.Create(value.ToString());
        }
    }

HTML output by viewing source

<ul id="menu">
   <li><a class="active" href="/">Home</a></li>
   <li><a href="/Home/About">About</a></li>
</ul>

As I’ve said, I know it is working, I just can’t seem to target the “active” class even though it exists…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T16:04:18+00:00Added an answer on June 1, 2026 at 4:04 pm

    you put a.active, you should use the “:” like you did with hover. a:active

    Edit: after re-reading your question, did you mean a custom css class for menu-items that link to the page the user is currently on? In that case, the way I use ASP, it will automatically take care of those menu items for you. Otherwise, I don’t see where you assign that class to your menu items? That is a first problem for sure.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote a custom wordpress plugin that is shown in the left menu bar
I wrote a custom class library for NUnit unit tests. It created a dll
I wrote custom FxCop rules for our project. I wrote a wiki page documenting
I wrote a custom action to create a scheduled task after install. I do
I wrote custom servlet in Liferay and want to know which user page calls
I wrote a custom Symfony (1.4) Textarea class to allow the ability to convert
I wrote a custom serializer that works by setting object properties by reflection. Serializable
I wrote a custom web part for SharePoint 2007 that loads an existing user
I wrote a custom error handler for my site and I'm aware that PHP
I wrote custom Twig loader that fetch templates from database and it works in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.