Tip #28 Did you know… That “Go to definition” is supported for CSS Class?


If you have a style defined as Class = ”fooRed”  as shown below and want to quickly know what does fooRed style contain?

 <div class ="fooRed">
    In RED
 </div>

 

Just place your cursor on fooRed and hit the F12 button. This will highlight the CSS class for you even if the class lives in the Current Page or in an External style sheet.

F12 is the short cut you can use instead of, Right Clicking fooRed and selecting “Go to Definition” action in the context menu as shown in the picture below.

F12ForCssClass

So, when you hit F12, the class is highlighted as shown below.

Highlighted Style

This way you can get to style definition with just one key –>F12

Reshmi Mangalore

SDET, Visual Web Developer

Comments (9)

  1. jc says:

    This is a nice little handy tip. I’ve got to remember to use this.

  2. Dilli says:

    Simple but more usefull tip for web developers.

  3. Useful, but too buggy. I’m using VS 2008 SP1 and this feature sometimes works but sometimes does not. I think there are some problems when trying to navigate to styles from content pages where stylesheet is not explicitly linked.

  4. SelArom says:

    it appears this doesn’t always work, especially if your css class is defined within a theme folder. However, seeing as how these are usually applied at runtime, it’s understandable!

    unfortunately, themes are the primary way I use CSS! Although I’m out of luck, this is still a good tip 🙂

  5. Anup says:

    Potentially useful but also has false positives or can be dangerous. For example, it is perfectly valid for any of these cases (maybe more that I forget):

    – More than one file with same selector. If all else is the same, the last file loaded with the same selector weighting wins; Visual studio (in my test) loaded the first file

    – If an HTML element has many classes (space delimited, e.g. class="class1 class2", class2 cannot be found if you use .class1.class2 in your css

    – CSS can apply to elements as well as classes, so if you had say this: h1 class="post-title" then there might be rules on h1 as well as .post-title; Going to definition will only go to .post-title.

    I appreciate it must be quite difficult for an IDE to cater for all the CSS selector intricacies, but it would be useful to see a list of matches when hitting F12 (it’s almost like doing a global search for whatever is F12’d in any CSS files or CSS sections in HTML code — even that could be difficult to be fair — and then sorting it on CSS selector rule precedence… That would be really tricky I guess but an awesome feature!)

    Anyway, that being aside, this has its uses in places, if people are aware of the pitfalls.

  6. Barbaros Alp says:

    Yes, it doesnt always work. I think, master pages or things like that cause this.

  7. wayne says:

    simple, but more useful, thanks!

  8. Anas Ghanem says:

    What About using CssClass for Server controls ?

    <asp:GridView … CssClass="GridView" .. />

    No "Go To Definition" here.