Archive for the ‘网站开发’ Category

用css实现图片垂直居中的使用技巧

星期三, 06月 4th, 2008

题目de难点在于两点:

垂直居中;

图片是个置换元素,有些特殊de特性.

至于如何解决,下面是一个权衡de相对结构干净,CSS简单de解决方法:

.box {

/*非IEde主流浏览器识别de垂直居中de方法*/

display: table-cell;

vertical-align:middle;

/*设置水平居中*/

text-align:center;

/* 针对IEdeHack */

*display: block;

*font-size: 175px;/*约为高度de0.873,200*0.873 约为175*/

*font-family:Arial;/*防止非utf-8引起dehack失效问题,如gbk编码*/

width:200px;

height:200px;

border: 1px solid #eee;

}

.box img {

/*设置图片垂直居中*/

vertical-align:middle;

}

<div class="box">

<img src="http://www.jb51.net/images/logo.gif" />

</div>

当然还有其他de解决方法,在此不深究,

css Sprites小实例代码

星期三, 06月 4th, 2008


这是一个很简单de应用,不过在设计中,这样做可以减轻服务器de压力,是请求次数减少,是一个不错de方法.
特别要说明de是,在这种小图片上即使是两张图片其实就响应时间来说也慢不了多少,不过有一个问题,就是两张图片交替时容易出现背景图片从新加载而导致很段时间不显示de效果.(时间长短视服务器de响应速度和图片大小而变化)
下面是cssde部分:
body {
font-family: “Lucida Sans”, “Lucida Sans Unicode”;
font-size: 14px;
line-height: 24px;
}
ul {
list-style-type: none;
}
li {
float: left;
}
a{
background-image: url(bg.gif);
height: 26px;
background-position: 53px 0px;
display: block;
margin-right: 10px;
width: 53px;
text-align: center;
color: #333333;
}
li a:link {
text-decoration: none;
}
li a:visited {
text-decoration: none;
}
li a:hover {
text-decoration: none;
background-position: 0 0px;//在这里规定从某一坐标开始显示图片}

从上面de相关代码不难看出,这里面起决定性作用de是
background-position:* *px;

这样,在复杂decss应用中,我便可以解决背景图片从新加载de问题

DIV border边框显示不完全问题的解决方法

星期三, 06月 4th, 2008

第一次遇到这样de问题,就是定义divde边框有部分却显示不了.而在firefox里却是一切正常,该死deie,太过分了.
相关代码也没有问题,查了好多资料,还百度了许多网站,在blueidea里也没有找到解决办法.后来在台湾de一个网站上发现了一个很几de解决办法:
如下:
效果图:

左右两边de边框就是不显示,气不死您~~郁闷,
css是:
[复制此相关代码]CODE:
#divproject {
margin-top: 5px;
border: 1px solid #0099CC;
}

后来按照那个网站de介绍,在里面加了一句
[复制此相关代码]CODE:
#divproject {
margin-top: 5px;
border: 1px solid #0099CC;
background-color: #FFFFFF;
}

其实就是只要给边框dediv加个背景即可解决问题
这样就可以搞定了~希望可以帮助到别人.

去除链接元素的虚线框 兼容IE7、IE6、FF

星期三, 06月 4th, 2008

推荐下面de相关代码

[复制此相关代码]CODE:
a {outline: none; /* for Forefox */ }

a {star:expression(this.onFocus=this.blur()); /* for Ie*/ }

下面de相关代码比较麻烦

我采用htc文件de办法来解决这个问题.首页将以下相关代码保存为link.htc文件.

[复制此相关代码]CODE:

<public:attach event="onfocus" onevent="hscfsy()"/>

<script language="javascript">

function hscfsy(){

this.blur();

}

</script>

  在HTML文件中写入以下相关代码,建立一个链接:

<a href="#" title="脚本之家">jb51.net</a>

  我开始为此链接定义CSS样式:

a {

  display:block;

  width:100px;

  height:30px;

  line-height:30px;

  text-align:center;

}

a:hover {

  background:#ddd;

}

  CSS样式确定了链接de外观,具有一定宽度与高度de块元素.文字水平左右均居中对齐.

  我在a标签de样式内,加入一条属性.用此消除链接de虚线边框:

a {

  display:block;

  width:100px;

  height:30px;

  line-height:30px;

  text-align:center;

  behavior:url(line.htc);

}

  我运行以下相关代码查看效果:

点击运行可以看到效果:
[Ctrl A 全选 提示:您可先修改部分相关代码,再按运行]

我在IE7、IE6中预览,已经没有问题了.但是在FF中虚线框依然存在.

  我新增一条样式定义来解决此问题.

[复制此相关代码]CODE:
a:focus { outline:0; }

查看最终de运行效果:

点击运行可以看到效果:
[Ctrl A 全选 提示:您可先修改部分相关代码,再按运行]

在IE7、IE6、FF中均成功实现了消除链接de虚线框.

巧妙地使用CSS选择器

星期三, 06月 4th, 2008

可以通过不同规则来定义不同内容块里de链接样式.类似这样:#nav a:link或者 #main a:link或者#footer a:link.也可以定义不同内容块中相同元素de样式不一样.例如,通过#main p和#sider p分别定义#main p和#sider pde样式.从结构上讲,您de页面是由图片、链接、列表、段落等组成de,这些元素本身并不会对显示在什么网络 设备中(PDA还是手机或者网络电视)有影响,它们可以被定义为任何de表现外观.
  一个仔细结构化deHTML页面非常简单,每一个元素都被用于结构目de.当您想缩进一个段落,不需要使用blockquote标签,只要使用p标签,并对p 加一个CSSdetext-indent规则就可以实现缩进目de.p是结构化标签,text-indent是表现属性,前者属于HTML,后者属于CSS.(这就是传说中de结构与表现相分离)
  良好结构deHTML页面内几乎没有表现属性de标签.相关代码非常干净简洁.例如,原先de相关代码<table width=”778″ cellpadding=”3″ border=”0″ align=”center”>,现在可以只在HTML中写<table id=”MrJin”>,所有控制表现de东西都写到CSS中去,在结构化deHTML中, table就是表格,而不是其他什么(更不能被用来布局和定位).
  当然,CSS选择器不只是这么简单,除了id还有class还有后代选择器,属性选择器等等.

CSS对Web页面载入效率的影响分析总结

星期三, 06月 4th, 2008

我罗列了十几条相关de知识与注意点,大家可以系统de探讨一下,让我编写deWeb页面打开更加流畅.
  请不要告诉我,您看不懂E文,只是您不愿意看!!!
  1、How the style system breaks up rules
  The style system breaks rules up into four primary categories. It is critical to understand these categories, as they are the first line of defense as far as rule matching is concerned. I use the term key selector in the paragraphs that follow. The key selector is defined to be the rightmost occurrence of an id selector, a class selector, or a tag selector.
  1.1、ID Rules
  The first category consists of those rules that have an ID selector as their key selector.
button#backButton { } /* This is an ID-categorized rule */
#urlBar[type="autocomplete"] { } /* This is an ID-categorized rule */
treeitem > treerow > treecell#myCell :active { } /* This is an ID-categorized rule */
  1.2、Class Rules
If a rule has a class specified as its key selector, then it falls into this category.
button.toolbarButton { } /* A class-based rule */
.fancyText { } /* A class-based rule */
menuitem > .menu-left[checked="true"] { } /* A class-based rule */
  1.3、Tag Rules
  If no class or ID is specified as the key selector, then the next potential category for a rule is the tag category. If a rule has a tag specified as its key selector, then the rule falls into this category.
td { } /* A tag-based rule */
treeitem > treerow { } /* A tag-based rule */
input[type="checkbox"] { } /* A tag-based rule */
  1.4、Universal Rules
  All other rules fall into this category.

:table { } /* A universal rule */
[hidden="true"] { } /* A universal rule */
* { } /* A universal rule */
tree > [collapsed="true"] { } /* A universal rule */
  2、How the Style System Matches Rules
  The style system matches a rule by starting with the rightmost selector and moving to the left through the rule’s selectors. As long as your little subtree continues to check out, the style system will continue moving to the left until it either matches the rule or bails out because of a mismatch.
  Your first line of defense is the rule filtering that occurs based on the type of the key selector. The purpose of this categorization is to filter out rules so that you don’t even have to waste time trying to match them. This is the key to dramatically increasing performance. The fewer rules that you even have to check for a given element, the faster style resolution will be. As an example, if your element has an ID, then only ID rules that match your element’s ID will be checked. Only class rules for a class found on your element will be checked. Only tag rules that match your tag will be checked. Universal rules will always be checked.
  3、Guidelines for Efficient CSS
  3.1、Avoid Universal Rules!
  Make sure a rule doesn’t end up in the universal category!
  3.2、Don’t qualify ID-categorized rules with tag names or classes
  If you have a style rule that has an ID selector as its key selector, don’t bother also adding the tag name to the rule. IDs are unique, so you’re slowing down the matching for no real reason.

[复制此相关代码]CODE:
BAD - button#backButton { }
BAD - .menu-left#newMenuIcon { }
GOOD - #backButton { }
GOOD - #newMenuIcon { }

  3.3、Don’t qualify class-categorized rules with tag names
  Similar to the rule above, all of our classes will be unique. The convention you should use is to include the tag name in the class name.

[复制此相关代码]CODE:
BAD - treecell.indented { }
GOOD - .treecell-indented { }

  3.4、Try to put rules into the most specific category you can!
  The single biggest cause of slowdown in our system is that we have too many rules in the tag category. By adding classes to our elements, we can further subdivide these rules into class categories, and then we no longer waste time trying to match as many rules for a given tag.

BAD - treeitem[mailfolder="true"] > treerow > treecell { }
GOOD - .treecell-mailfolder { }
  3.5、Avoid the descendant selector!
  The descendant selector is the most expensive selector in CSS. It is dreadfully expensive, especially if a rule using the selector is in the tag or universal category. Frequently what is really desired is the child selector. The use of the descendant selector is banned in UI CSS without the explicit approval of your skin’s module owner.
BAD - treehead treerow treecell { }
BETTER, BUT STILL BAD (see next guideline) - treehead > treerow > treecell { }
  3.6、Tag-categorized rules should never contain a child selector!
  Avoid using the child selector with tag-categorized rules. You will dramatically increase the matching time (especially if the rule is likely to be matched more often than not) for all occurrences of that element.

BAD - treehead > treerow > treecell { }
BEST - .treecell-header { }
  3.7、Question all usages of the child selector!
  Be careful about using the child selector. If you can come up with a way to avoid having to use it, do so. In particular, the child selector is frequently used with RDF trees and menus like so.

BAD - treeitem[IsImapServer="true"] > treerow > .tree-folderpane-icon { }
  Remember that attributes from RDF can be duplicated in a template! Take advantage of this fact to duplicate RDF properties on child XUL elements that wish to change based off that attribute.

GOOD - .tree-folderpane-icon[IsImapServer="true"] { }
  3.8、Rely on inheritance!
  Learn which properties inherit, and allow them to do so! We have explicitly set up XUL widgetry so that you can put list-style-image (just one example) or font rules on the parent tag, and it will filter in to the anonymous content. You don’t have to waste time writing a rule that talks directly to the anonymous content.

BAD - #bookmarkMenuItem > .menu-left { list-style-image: url(blah); }
GOOD - #bookmarkMenuItem { list-style-image: url(blah); }
  In the above example, the desire to style the anonymous content (without understanding that list-style-image inherits) resulted in a rule that was in the class category, when this rule really should have ended up being in the most specific category of all, the ID category.
  Remember, especially with anonymous content, that they all have the same classes! The bad rule above causes the icon of every menu to be checked to see if it is contained in the bookmarks menu item. This is hideously expensive (since there are many menus); this rule never should have even been checked by any menu other than the bookmarks menu.
  3.9、Use -moz-image-region!
  Putting a bunch of images into a single image file and selecting them with -moz-image-region performs significantly better than putting each image into its own file.
  Original Document Information - Author: David Hyatt

提高CSS文件可维护性的五种方法总结

星期三, 06月 4th, 2008

1.分解您de样式
对于小项目,在写相关代码之前,按页面结构或页面内容将相关代码分为几块并给予注释.例如,可以分别将 全局样式、布局、字体样式、表单、评论和其他分为几个不同de块来继续工作.
而对于较大de工程,这样显然不会有什么效果.此时,就需要将样式分解到几个不同de样式表文件.下面demaster stylesheet 就是这一方法de例子,它de工作主要是导入其他样式文件.使用这一方法不仅能优化样式结构,而且有利于减少一些不必要de服务器请求.而分解文件de方法就有许多种,master stylesheet 使用了最常见de一种.
/*——————————————————————
[Master Stylesheet]
Project: Smashing Magazine
Version: 1.1
Last change: 05/02/08 [fixed Float bug, vf]
Assigned to: Vitaly Friedman (vf), Sven Lennartz (sl)
Primary use: Magazine
——————————————————————-*/
@import “reset.css”;
@import “layout.css”;
@import “colors.css”;
@import “typography.css”;
@import “flash.css”;
/* @import “debugging.css”; */
同时对于大型项目,您也可以加上CSS文件de升级标志或者一些诊断措施,这里不再详述.
2.建立CSS文件索引
为了能够迅速de了解整个CSS文件de结构,在文件开头建立文件索引是一个不错de选择.一种可行de方法是建立树形de索引:结构上deid 和 class 都可以成为该树de一个分支.如下:
/*——————————————————————
[Layout]
* body
Header / #header
Content / #content
- Left column / #leftcolumn
- Right column / #rightcolumn
- Sidebar / #sidebar
- RSS / #rss
- Search / #search
- Boxes / .box
- Sideblog / #sideblog
Footer / #footer
Navigation #navbar
Advertisements .ads
Content header h2
——————————————————————-*/
或者也可以这样:
/*——————————————————————
[Table of contents]
1. Body
2. Header / #header
2.1. Navigation / #navbar
3. Content / #content
3.1. Left column / #leftcolumn
3.2. Right column / #rightcolumn
3.3. Sidebar / #sidebar
3.3.1. RSS / #rss
3.3.2. Search / #search
3.3.3. Boxes / .box
3.3.4. Sideblog / #sideblog
3.3.5. Advertisements / .ads
4. Footer / #footer
——————————————————————-*/
另一种方式可以只是先简单de将内容列举出来,也不需要缩进.下面de一个例子中,如果您需要跳至RSS部分您只需要简单de搜索 8.RSS.
/*——————————————————————
[Table of contents]
1. Body
2. Header / #header
3. Navigation / #navbar
4. Content / #content
5. Left column / #leftcolumn
6. Right column / #rightcolumn
7. Sidebar / #sidebar
8. RSS / #rss
9. Search / #search
10. Boxes / .box
11. Sideblog / #sideblog
12. Advertisements / .ads
13. Footer / #footer
——————————————————————-*/

/*——————————————————————
[8. RSS / #rss]
*/
#rss { … }
#rss img { … }
定义这样一个样式检索可以很有效de使其他人阅读学习您de相关代码变得容易.在制作大项目de时候,您也可以将检索打印出来从而在您阅读相关代码de时候方便查阅.
3.定义您de颜色和版式
CSS 中我无法使用常量,但是在编写颜色和版式方面de相关代码是我会经常遇到可以使用很多次de类,在这里可以将之视为CSSde常量.
一种可以减小CSS无常量定义确定de方法是在CSS文件顶部de注释中下一些定义,也就是定义常量.一种最简单de应用就是创建一个颜色表.这样您就可以快速de了解整个页面de色彩,从而避免一些反复修改过程中de错误.如果您需要对颜色进行修改,您也可以很快找到它.
/*——————————————————————
# [Color codes]
# Dark grey (text): #333333
# Dark Blue (headings, links) #000066
# Mid Blue (header) #333399
# Light blue (top navigation) #CCCCFF
# Mid grey: #666666
# */
或者,您也可以选择描述您布局当中使用de颜色.对于一个给定de颜色,您可以将用到该颜色de块罗列出来.当然,您也可以选择按页面元素来罗列颜色.
/*——————————————————————
[Color codes]
Background: #ffffff (white)
Content: #1e1e1e (light black)
Header h1: #9caa3b (green)
Header h2: #ee4117 (red)
Footer: #b5cede (dark black)
a (standard): #0040b6 (dark blue)
a (visited): #5999de (light blue)
a (active): #cc0000 (pink)
——————————————————————-*/
对于版式有同样de例子.
/*——————————————————————
[Typography]
Body copy: 1.2em/1.6em Verdana, Helvetica, Arial, Geneva, sans-serif;
Headers: 2.7em/1.3em Helvetica, Arial, “Lucida Sans Unicode”, Verdana, sans-serif;
Input, textarea: 1.1em Helvetica, Verdana, Geneva, Arial, sans-serif;
Sidebar heading: 1.5em Helvetica, Trebuchet MS, Arial, sans-serif;
Notes: decreasing heading by 0.4em with every subsequent heading level
——————————————————————-*/
4.格式化CSS属性
当我编写相关代码de时候,使用一些特殊de编码风格会对提高CSS相关代码de可读性有很大帮助.许多人都有各自不同de编码风格.一部分人习惯于将颜色和字体de相关代码放在前面,另外一部分则更喜欢将类似浮动和定位de更“重要”de属性放在前面.类似de,也可以将页面元素按照它在布局中de结构进行排序:
body,
h1, h2, h3,
p, ul, li,
form {
border: 0;
margin: 0;
padding: 0;
}
一些开发者用一种更为有意思de方法:他们将属性按首字母de顺序排列.值得注意de是,这样一种方法可能对某些浏览器会产生问题.
不管自己de格式如何,您要确保您已经清晰de定义了这些格式方法.这样,您de同事在阅读您de相关代码de时候将会感谢您de努力.
5.缩进会是您de朋友!
为了让您de相关代码给人感觉更为直观,您可以使用一行来定义大纲元素de样式.当指定de选择器里de属性超过三个de时候,这种方式将带来混乱.但是,适度de使用这种方式,您可以很清楚de区分相同类de不同点.
#main-column { display: inline; float: left; width: 30em; }
#main-column h1 { font-family: Georgia, “Times New Roman”, Times, serif; margin-bottom: 20px; }
#main-column p { color: #333; }
同时,样式修改de维护也是个比较麻烦de问题.很多人修改样式之后就忘记了,结果后来又发现修改de样式导致了页面出错,不得不苦苦寻找.因此,为修改de样式构建一个特殊de格式就很必要了.一种很简单de方式是,给修改过de样式缩进,同时,也可以使用一些注释(比如”@new”)来做一个标识.
#sidebar ul li a {
display: block;
background-color: #ccc;
border-bottom: 1px solid #999; /* @new */
margin: 3px 0 3px 0;
padding: 3px; /* @new */
}
总de来说,只有建立一个合适de样式指南才会对样式表de可读性有所帮助.记住,移去每一个对您理解文件没有帮助de样式指南,避免对过多de元素使用过多de样式指南.然后,为了一个可读性可维护性良好deCSS文件而努力吧.

CSS渐变文本效果的两种方法比较

星期三, 06月 4th, 2008


是否想不用photoshop来创建一个带渐变de标题文字吗? 这里用一个简单decss技巧来向您展示如何仅仅使用css和png图片来制造这种效果. 经测试这种方法适合大多数主流浏览器.当然, IE6需要一个支持透明PNGdeHack(值得庆幸de是微软正在极力de将用户deIE6自动升级到IE7^.^, 延伸阅读:Warning: An IE7 Auto-Update Is Coming Soon)
优势
这是纯粹decss技巧,没有使用任何ja脚本或者flash, 并且它可以在大多数浏览器上正常工作(IE6需要支持透明PNGdehack)
这是完美de标题设计,您不必使用photoshop,这将大量节省您de带宽和时间.
您可以对任何网页字体使用这种效果,而且字号大小也是可变de.
他是如何工作de?

这个技巧很简单.我只是简单de使用了1px宽de透明png覆盖在了文本上.

html

CSS Gradient Text
CSS
关键就在这里:
h1 { position: relative }
h1 span { position: absolute } h1 {
font: bold 330%/100% “Lucida Grande”;
position: relative;
color: #464646;
}
h1 span {
background: url(gradient.png) repeat-x;
position: absolute;
display: block;
width: 100%;
height: 31px;
}
就这样, 您做到了 ^_^ 点击这里查看示例.
使它能够支持IE6
下面这个hack仅仅是让IE6支持透明PNG-24格式de图片.
<!–[if lt IE 7]>
<style>
h1 span {
background:none;
filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=’gradient.png’, sizingMethod=’scale’);
}
</style>
<![endif]–>
jQuery生成版本
如果您不想在相关代码里有空de<span>标记, 那么您可以使用javascript来动态生成它. 这里是一个简单dejquery生产方法.

<script type=”text/javascript” src=”jquery.js” mce_src=”jquery.js”></script>
<script type=”text/javascript”>
$(document).ready(function(){
//prepend span tag to H1
$(”h1″).prepend(”<span></span>”);
});
</script>
DEMO打包下载

用CSS开发时髦的导航栏图例教程

星期三, 06月 4th, 2008

制作容易de站点导航栏是CSS真正展现自己特有能力de一个领域.制作导航条de老方法倾向于依赖大量de图片、嵌套表格和Javascript脚本 – 所有这些都会严重影响站点de可用性和无障碍性.如果您de站点不能在一个不支持Javascriptde设备上被导航,那么您不仅阻止了关闭Javascriptde用户,同时您也阻止了只支持文本de设备,比如屏幕阅读器已经搜索引擎de机器人程序 – 它们将永远无法从您de首页得到网站内容de索引.就算您de客户不在乎无障碍性,告诉他们笨重de菜单阻止他们得到一个体面de搜索引擎排名!

CSS允许您创造具有吸引力de导航栏,采用CSSde优势在于不仅仅它在外观上非常美观,实际上它还是文本 – 是一种采用特殊方法标注de文本,它能够让所有那些物理上没法看到您de设计但是又想得到您de内容de人或者设备无障碍和容易理解地访问您de站点.在本文中,我将看看各种各样建立基于CSSde导航栏解决方案.其中有一些适合在已有站点实施,以便使这些站点引导更迅速,并且通过替换古板de、基于图片de导航栏来促进它de无障碍性.另外一些更适合集成于纯粹deCSS站点布局中.

如何把一个结构化de列表样式化为导航栏菜单?

对于新设计de网站,您可能会尝试避免使用表格来做布局,或者只是在绝对必要de地方才使用表格.因此,一个不涉及到表格de导航栏解决方案是有用de,同时,通过杜绝表格元素de使用,您会发现您de页面将包含更少de标记符号.

解决方案

导航栏系统是用户在这个站点能够访问de地点de列表.因此,一个无序de列表是标记您de导航栏de理想方式.象您看到de,在图1中de导航栏de实现是采用CSS样式化de一个列表.

navigation_using-styled-list.png

图1:样式化列表de导航栏<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”

“http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>

<html xmlns=”http://www.w3.org/1999/xhtml” lang=”en-US”>

<head>

<title>Lists as navigation</title>

<meta http-equiv=”content-type”

content=”text/html; charset=utf-8″ />

<link rel=”stylesheet” type=”text/css” href=”listnav1.css” />

</head>

<body>

<div id=”navigation”>

<ul>

<li><a href=”#”>Recipes</a></li>

<li><a href=”#”>Contact Us</a></li>

<li><a href=”#”>Articles</a></li>

<li><a href=”#”>Buy Online</a></li>

</ul>

</div>

</body>

</html>

#navigation {

width: 200px;

}

#navigation ul {

list-style: none;

margin: 0;

padding: 0;

}

#navigation li {

border-bottom: 1px solid #ED9F9F;

}

#navigation li a:link, #navigation li a:visited {

font-size: 90%;

display: block;

padding: 0.4em 0 0.4em 0.5em;

border-left: 12px solid #711515;

border-right: 1px solid #711515;

background-color: #B51032;

color: #FFFFFF;

text-decoration: none;

}

讨论

为了创建一个基于无序列表de导航栏,首先建立您de列表,把每个导航链接放入li元素,就象下面这样:

<ul>

<li><a href=”#”>Recipes</a></li>

<li><a href=”#”>Contact Us</a></li>

<li><a href=”#”>Articles</a></li>

<li><a href=”#”>Buy Online</a></li>

</ul>

接着,选择一个适合deID把列表包含在一个div中:

<div id=”navigation”>

<ul>

<li><a href=”#”>Recipes</a></li>

<li><a href=”#”>Contact Us</a></li>

<li><a href=”#”>Articles</a></li>

<li><a href=”#”>Buy Online</a></li>

</ul>

</div>

象下面图2看到de,这个标记在浏览器de缺省样式下面看上相当普通.

navigation_unstyled-list-basic.png

图2:没有样式化de基础列表

我需要做de第一件事情是样式化导航栏存在de容器 – 在这里是 #navigation :

#navigation {

width: 200px;

}

在这里我简单de给了#navigation一个宽度.如果这个导航系统是CSS页面布局de一部分,我可能还会给这个ID添加一些位置信息.

下面,我样式化列表:

#navigation ul {

list-style: none;

margin: 0;

padding: 0;

}

象图3展示de,上面de规则移除了缺省状态下浏览器显示一个列表时出现de前置符号和缩进.

navigation_list-no-indent-bullets.png

图3 移除缩进和前置符de列表

下一步是使用#navigation样式化li元素,给它们一个底边:

#navigation li {

border-bottom: 1px solid #ED9F9F;

}

最后,我样式化link:

#navigation li a:link, #navigation li a:visited {

font-size: 90%;

display: block;

padding: 0.4em 0 0.4em 0.5em;

border-left: 12px solid #711515;

border-right: 1px solid #711515;

background-color: #B51032;

color: #FFFFFF;

text-decoration: none;

}

到此大多数工作已经做好.我建立de这个CSS规则包括增加左右边界,移除下划线等等.在这个规则中第一个属性定义把显示属性设为block,这使得那些链接显示为块元素,这样de话当您光标划过这些导航“按钮”时,显示de效果和使用图片导航一模一样.

用CSS开发时髦的导航栏第二篇

星期三, 06月 4th, 2008

解决方案

在一个导航系统中显示子菜单最好de办法是在一个列表中创建子列表.这样标记de两级导航栏很容易被理解 – 哪怕浏览器不支持CSS.

为了产生多级导航栏,我创建一个嵌套de列表,为这些新de列表项样式化颜色、边界和链接属性:

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”

“http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>

<html xmlns=”http://www.w3.org/1999/xhtml” lang=”en-US”>

<head>

<title>Lists as navigation</title>

<meta http-equiv=”content-type”

content=”text/html; charset=utf-8″ />

<link rel=”stylesheet” type=”text/css” href=”listnav_sub.css” />

</head>

<body>

<div id=”navigation”>

<ul>

<li><a href=”#”>Recipes</a>

<ul>

<li><a href=”#”>Starters</a></li>

<li><a href=”#”>Main Courses</a></li>

<li><a href=”#”>Desserts</a></li>

</ul>

</li>

<li><a href=”#”>Contact Us</a></li>

<li><a href=”#”>Articles</a></li>

<li><a href=”#”>Buy Online</a></li>

</ul>

</div>

</body>

</html>

#navigation {

width: 200px;

}

#navigation ul {

list-style: none;

margin: 0;

padding: 0;

}

#navigation li {

border-bottom: 1px solid #ED9F9F;

}

#navigation li a:link, #navigation li a:visited {

font-size: 90%;

display: block;

padding: 0.4em 0 0.4em 0.5em;

border-left: 12px solid #711515;

border-right: 1px solid #711515;

background-color: #B51032;

color: #FFFFFF;

text-decoration: none;

}

#navigation li a:hover {

background-color: #711515;

color: #FFFFFF;

}

#navigation ul ul {

margin-left: 12px;

}

#navigation ul ul li {

border-bottom: 1px solid #711515;

margin:0;

}

#navigation ul ul a:link, #navigation ul ul a:visited {

background-color: #ED9F9F;

color: #711515;

}

#navigation ul ul a:hover {

background-color: #711515;

color: #FFFFFF;

}

增加这些以后de显示效果如图4.

navigation_css-list-subnav.png

4. 包含子菜单de导航栏

讨论

嵌套列表是用来描述我正在做de导航栏系统de好办法.第一个列表包含站点de主要部分,在Recipes下面de子列表显示了Recipes范围之内de子部分.即使没有任何CSS样式,列表de结构依然清晰且容易理解,就象您在图5看到de一样.

navigation_sense-without-css.png

5:没有使用样式,包含子菜单de导航栏

下面是我用来在主要项目deli元素里面标记这个简单de嵌套列表deHTML相关代码:

<ul>

<li><a href=”#”>Recipes</a>

<ul>

<li><a href=”#”>Starters</a></li>

<li><a href=”#”>Main Courses</a></li>

<li><a href=”#”>Desserts</a></li>

</ul>

</li>

<li><a href=”#”>Contact Us</a></li>

<li><a href=”#”>Articles</a></li>

<li><a href=”#”>Buy Online</a></li>

</ul>

HTML,如果简单de使用本文前面deCSS,不做任何修改de话,导航菜单de显示将如图6.由于li元素继承主菜单de样式,子列表将呈现出主导航栏一样de样式.

navigation_sublist-mainnav-styles.png

6:采用默认样式表子菜单导航栏

为了让嵌套de列表呈现出它是一个子菜单而不是主导航栏一部分de效果,让我增加一个样式规则:

#navigation ul ul {

margin-left: 12px;

}

这个规则将缩进嵌套列表,让它在主菜单de右边界对齐,象图7显示de这样:

navigation_indent-subnav.png

7:带有缩进规则de导航栏

最后让我给嵌套表里面delia元素增加一些简单de样式以便完善效果:

#navigation ul ul li {

border-bottom: 1px solid #711515;

margin: 0;

}

#navigation ul ul a:link, #navigation ul ul a:visited {

background-color: #ED9F9F;

color: #711515;

}

#navigation ul ul a:hover {

background-color: #711515;

color: #FFFFFF;

}