Asp.net MVC 3 imitates the layout and style of the Google homepage navigation bar

Source: Internet
Author: User
Tags actionlink

 

I. Behavior Analysis on the Google Homepage

There is a navigation bar at the top of the Google homepage, which uses links to navigate between different functions, including webpage, image, video, and map.

The first page is "webpage". The link does not have any underscores and is displayed in black box. But it is still a link. Clicking it will refresh the current page.

When we click the second item, we can see that the other "Web pages" are restored to the form of links, and the links we click are changed to the form of simhei text.

There is a horizontal line under the navigation bar of Google's first line.

On the Google homepage, div Gog includes the DIV gbar in the navigation area and the DIV guser In the logon area. These are the three main Div groups, and the horizontal line is defined by the bodder-top in the lower workspace.

 

2. Style Analysis on the Google Homepage

The style related to the navigation bar is as follows:

Body
{
Margin: 0;
}
# Gog
{
Padding: 3px 8px 0;
}

Body, TD, A, P,. h
{
Font-family: Arial, sans-serif;
}

A em
{
Text-Decoration: underline;
}

A. gb1, A. GB2, A. gb3, A. gb4
{
Color: # 11C! Important;
}
# Gog
{
Background: # FFF;
}
# Gbar, # guser
{
Font-size: 13px;
Padding-top: 1px! Important;
}
# Gbar
{
Float: left;
Height: 22px;
}
# Guser
{
Padding-bottom: 7px! Important;
Text-align: right;
}

. Gb1
{
Margin-Right:. 5em;
}
. Gb1,. gb3
{
Zoom: 1;
}

A. gb1, A. gb4
{
Text-Decoration: underline! Important;
}

A. gb1, A. GB2, A. gb3, A. gb4
{
Color: # 00c! Important;
}

# Gbar. gbz0l
{
Color: #000! Important;
Cursor: default;
Font-weight: bold;
Text-Decoration: none! Important;
}
Body
{
Background: # FFF;
Color: black;
}

A
{
Color: # 11C;
Text-Decoration: none;
}
A: hover, A: active
{
Text-Decoration: underline;
}

A: visited
{
Color: # 551a8b;
}
A. gb1, A. gb4
{
Text-Decoration: underline;
}

 

Iii. Relationship between elements on the Google homepage and Asp.net MVC

Div Gog corresponds to Div Header

Div gbar corresponds to Div menucontainer

Div guser for div logindisplay

 

Therefore, we can refer to the above style to define the main style, so as to get an uncompromising Google navigation bar:

Body
{
Margin: 0;
Font-family: Arial, sans-serif;
Background: # FFF;
Color: black;
}

/* Primary layout elements
----------------------------------------------------------*/

# Header
{
Padding: 3px 8px 0;
Background: # FFF;
}

# Menucontainer, # logindisplay
{
Font-size: 13px;
Padding-top: 1px! Important;
}
# Menucontainer
{
Float: left;
Height: 22px;
}

# Logindisplay
{
Padding-bottom: 7px! Important;
Text-align: right;
}

/* Set the link attribute in the header. The weight is 100 + 1, indicating the link in the hearder */
# Header
{
Color: # 11C! Important;
Margin-Right:. 5em;
Text-Decoration: underline! Important;
/* Color: # 00c! Important ;*/
}

/* Set the. Selected class style in the header. The weight is 100 + 1 */
# Header. Selected
{
Color: #000! Important;
Cursor: default;
Font-weight: bold;
Text-Decoration: none! Important;
}

 

4. Define the style of the current menu item:

1. How to add a style for actionlink:

Actionlink has 10 reloads, of which html. actionlink ("linktext", "actionname", "controlname", routevalues, htmlattributes)

Routevalues is used to pass Parameters. htmlattributes is used to pass attributes of a tag, for example:

HTML. actionlink ("product", "detail", "product", new {id = 1}, new {@ class = "selected"}), which is generated here: <a href = "products/detail/1" class = "selected"> product </a>

Of course, if other attributes are added, @ is not required. Because class is a keyword, @ class is required when passed in.

We can also consider using generatelink to viewSource code, You can see that basically generatelink is called:

Htmlhelper. generatelink (helper. viewcontext. requestcontext, helper. routecollection, linktext, null/* routename */, actionname, controllername, rout, null ));

Here, you need to be familiar with things like viewcontext of helper.

2. Create a hemlhelper to define the menu items of the home page: when it is the current page, add the selected style to it.

/// <Summary>
/// Replace actionlink in _ layout. If it is the link of the current page, the selected style is added.
/// </Summary>
Public static class actionitemhelper
{
Public static mvchtmlstring actionitem (this htmlhelper helper, string linktext, string actionname, string controllername)
{
String currentcontrollername = (string) helper. viewcontext. routedata. Values ["controller"];
String currentactionname = (string) helper. viewcontext. routedata. Values ["action"];

// If the menu item is the current page
If (currentcontrollername. Equals (controllername, stringcomparison. currentcultureignorecase )&&
Currentactionname. Equals (actionname, stringcomparison. currentcultureignorecase ))
Return helper. actionlink (linktext, actionname, controllername, null, new {@ class = "selected "});
Else
Return helper. actionlink (linktext, actionname, controllername );
}
}

5. In CSS, pay attention to the priority of the style application:

Priority rules:

1. Mark the same group of CSS settings! Important settings, top priority

2. the CSS of the webpage is higher than the default style of the browser. This is of course

3. Priority of a large selector weight

4. When the selector weights are equal

how to calculate the weight: 1. inline style: 1000; 2. "# ID": 100; 3. ". class ": 10; 4. element, such as a {}: 1

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.