简体   繁体   English

如何制作CSS水平导航菜单?

[英]How to make a CSS horizontal navigation menu?

I need to make a css navigation according to the following style: 我需要根据以下样式进行CSS导航:

DESIRED LOOK 期望的外观

在此处输入图片说明

Below are the designs that I have done:- 以下是我已经完成的设计:

1)Exhibit A - made using sprites 1)展览A-使用精灵制作

在此处输入图片说明

Note: Ignore the arrangement of the menu items 注意: 忽略菜单项的排列

Pros: works well and has the desired look 优点:运作良好,并具有所需的外观

Cons: if there is a need to add another menu item, another image must be manually made for that particular menu item. 缺点:如果需要添加另一个菜单项,则必须为该特定菜单项手动制作另一个图像。 ie. 即。 not extensible 不可扩展

2)Exhibit B 2)图B

在此处输入图片说明

Pros: very extensible. 优点:非常可扩展。 If another menu item must be added, new extra images need not be made. 如果必须添加另一个菜单项,则无需制作新的额外图像。 Only the menu name need to be typed in the html code. 仅菜单名称需要在html代码中键入。

Cons: the hover effect is not the same as the desired look. 缺点:悬停效果与所需外观不同。

My Requirement 我的要求

Is to use Exhibit B , along with the hover effect from Exhibit A , but without having to add extra images when a menu item is created(this is what happens in Exhibit A , although it has the desired hover effect). 就是用附录B中 ,与附录A中悬停效果一起,但无需创建一个菜单项时添加额外的图像(这是在附录A发生了什么,尽管它具有所需的悬停效果)。

My approach: 我的方法:

  1. Start working with Exhibit B 开始使用附件B
  2. For the hover effect in the case of a single menu item use 3 images 对于单个菜单项的悬停效果,请使用3张图像

a)left most edge a)最左边 最左边

b)repeating slice of the middle area b)重复中段 中间部分的重复切片

c)right most edge c)最右边 最右边

Is this correct ? 这个对吗 ?

Is this possible ? 这可能吗 ?

Is there a better way? 有没有更好的办法? A link to a tute would be fine. 链接到tute会很好。

Thanks 谢谢


1] css code for Exhibit A 1]附件A的CSS代码

@charset "UTF-8";
* {
margin:0;
padding:0;
list-style: none;
}

ul {
list-style: none;
margin: 0;
padding: 0;
}

img {
    border: none;
}

.clear {
clear:both;
}

.nav-container {
width: 960px;
}
#navMenu{
display: inline;
margin: 0;
padding: 0px;
position: relative;
z-index: 5;
}
#navMenu li{
float: left;
display: inline;
}

#navMenu li.navRepeat{
float: left;
display: inline;
background-image:url("../images/navigation_repeat.gif");
width:425px;
height:40px;
}

#navMenu li.navRepeatEnd{
float: right;
display: inline;
background-image:url("../images/navigation_repeat_end.gif");
width:1px;
height:40px;
}

a.navReservations{
display:block;
float:left;
width:89px;
height:40px;
background-repeat:no-repeat;
background: url("../images/reservations.gif")
}

a.navReservations:hover{
background: url("../images/reservations.gif") 0 40px;
}

a.navRentals{
display:block;
float:left;
width:62px;
height:40px;
background-repeat:no-repeat;
background: url("../images/rentals.gif")
}

a .navReservations {
float: left;
display: inline;
height: 100px;
width: 400px;
}

a.navRentals:hover{
background: url("../images/rentals.gif") 0 40px;
}

a.navTariffs{
display:block;
float:left;
width:59px;
height:40px;
background-repeat:no-repeat;
background: url("../images/tariffs.gif")
}

a.navTariffs:hover{
background: url("../images/tariffs.gif") 0 40px;
}

a.navFleet{
display:block;
float:left;
width:64px;
height:40px;
background-repeat:no-repeat;
background: url("../images/fleet.gif")
}

a.navFleet:hover{
background: url("../images/fleet.gif") 0 40px;
}

a.navTools{
display:block;
float:left;
width:56px;
height:40px;
background-repeat:no-repeat;
background: url("../images/tools.gif")
}

a.navTools:hover{
background: url("../images/tools.gif") 0 40px;
}

a.navReports{
display:block;
float:left;
width:71px;
height:40px;
background-repeat:no-repeat;
background: url("../images/reports.gif")
}

a.navReports:hover{
background: url("../images/reports.gif") 0 40px;
}

a.navSystem-Management{
display:block;
float:left;
width:133px;
height:40px;
background-repeat:no-repeat;
background: url("../images/system_management.gif")
}

a.navSystem-Management:hover{
    background: url("../images/system_management.gif") 0 40px;
}

2] css code for Exhibit B 2]附件B的CSS代码

#navigation {
width: 959px;
height: 36px;
margin: 0;
padding: 0;
background-image: url(images/navigation-bg.gif);
background-repeat: no-repeat;
background-position: left top;
border: 1px solid #CCC;
} 
#navigation ul {
list-style: none;
margin: 0;
padding: 0;
} 
#navigation ul li {
display: inline;
margin: 0px;
} 
#navigation ul li a {
height:27px;
display: block;
float: left;
color: #000;
text-decoration: none;
font-family: Arial;
font-size: 12px;
font-weight: bold;
background-image: url(images/navigation-separator.gif);
background-repeat: no-repeat;
background-position: right center;
padding-top: 6px;
padding-right: 15px;
padding-left: 15px;
vertical-align: 10%;
padding-bottom: 4px;
} 

#navigation ul li a:hover {
color:#FFF;
background-image: url(images/navigation-hover.gif);
background-repeat: repeat-x;
background-position: left top;
}

#navigation ul li#active a {
color:#363636;
background: url(images/navigation-hover.png) repeat-x left top;
}

Well you technically only need two sprites, a wide left + body of the tab and a right side. 好吧,从技术上讲,您只需要两个精灵,即选项卡的左侧+主体和右侧。 By wide, I mean, 400px or some arbitrarily wide size that you don't anticipate hitting. 宽,我的意思是400px或您不希望碰到的任意大的尺寸。 You're trading a kb for easy of use. 您正在交易kb以便于使用。 You can accomplish this by having markup like: 您可以通过以下标记来实现此目的:

<ul class="list">
    <li><a href="#">Text</a></li>
</ul>

with css like: 与CSS像:

ul.list
{
    list-style-type: none;
    margin: 0;
}
ul.list li 
{
    float: left;
    background: url(leftpluswide.png) top left no-repeat;
}
ul.list li a
{
    background: url(right.png) top right no-repeat;
}

The only caveat is that since the right.png will be overlapping the background on the li, you'll need to make sure that it doesn't have any transparency. 唯一的警告是,由于right.png将与li的背景重叠,因此您需要确保它没有任何透明度。

Also for completeness sake, you might need to apply a height to the li and the a (which will potentially require a display:inline-block or a line-height to take it) to make everything line up well. 同样,出于完整性考虑,您可能需要为li和a设置一个高度(这可能需要使用display:inline-block或line-height来使之对齐),以使所有内容对齐。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM