search
HomeWeb Front-endHTML TutorialDIV CSS: Menu without tables_html/css_WEB-ITnose

布局初步搭建起来,我开始填充里面的内容。首先是定义logo图片:

样式表:#logo {MARGIN: 0px;padding:0px;WIDTH: 200px;HEIGHT:80px;}
页面代码:


以上代码现在应该容易理解。先在CSS定义了一个logo的层,然后在页面中调用它。需要说明的是,为了使网页有更好的易用性,web标准要求大家给所有的、属于正式内容的图片,加一个alt属性。这个alt属性是用来说明图片的作用(当图片不能显示的时候就显示替换文字),所以不要只写成无意义的图片名称。

接下来是定义菜单。

1.不用表格的菜单(纵向)
我们先来看菜单的最终效果:

什么是网站标准
使用标准的好处
怎样过渡
相关教程
工具
资源及链接

通常方法我们至少嵌套2层表格来实现这样的菜单,间隔线采用在td中设置背景色并插入1px高的透明GIF图片实现;背景色的交替效果采用td的onmouseover事件实现。但查看本菜单的页面代码,你会看到只有如下几句:


没有用任何table,而用的是无序列
  • ,整个菜单的效果实现的秘密完全在于id="menu",我们再来看CSS中关于menu的定义:

    (1)首先定义了menu层的主要样式:

    #menu {
    MARGIN: 15px 20px 0px 15px; /*定义层的外边框距离*/
    PADDING:15px; /*定义层的内边框为15px*/
    BACKGROUND: #dfdfdf; /*定义背景颜色*/
    COLOR: #666; /*定义字体颜色*/
    BORDER:#fff 2px solid; /*定义边框为2px白色线条*/
    WIDTH:160px; /*定义内容的宽度为160px*/
    }
    (2)其次定义无序列表的样式:

    #menu ul {
    MARGIN: 0px;
    PADDING: 0px;
    BORDER: medium none; /*不显示边框*/
    LINE-HEIGHT: normal;
    LIST-STYLE-TYPE: none;

    }
    #menu li {BORDER-TOP: #FFF 1px solid; MARGIN: 0px;}
    说明:这里用的是id选择器的派生方法定义(参考第7天:CSS入门的介绍)了在menu层中的子元素
    • 的样式。LIST-STYLE-TYPE: none一句表示不采用无序列表的默认样式,即:不显示小圆点(我们后面用自己的图标来代替小圆点)。BORDER-TOP: #FFF 1px solid;则定义了菜单之间的1px间隔线。

      (3)定义onmouseover效果

      #menu li a {
      PADDING:5px 0px 5px 15px;
      DISPLAY: block;
      FONT-WEIGHT: bold;
      BACKGROUND: url(/bok/images/icon_dot_lmenu.gif) transparent no-repeat 2px 8px;
      WIDTH: 100%;
      COLOR: #444;
      TEXT-DECORATION: none;
      }
      #menu li a:hover { BACKGROUND: url(/bok/images/icon_dot_lmenu2.gif) #C61C18 no-repeat 2px 8px;
      COLOR: #fff; }
      解释如下:

      "display:block;"表示将标签a当作块级元素来显示,使得链接变成一个按钮;
      "BACKGROUND: url(/bok/images/icon_dot_lmenu.gif) transparent no-repeat 2px 8px;"这一句定义了替代li的小圆点的图标。"transparent"指背景为透明,"2px 8px"指定图标的位置是距左边2px,距上边8px。这一句也可以拆分写成四句:"BACKGROUND-IMAGE: url(/bok/images/icon_dot_lmenu.gif); BACKGROUND-POSITION: 2px 8px; BACKGROUND-REPEAT: no-repeat; BACKGROUND-COLOR: transparent;"
      "#menu li a:hover"定义了当鼠标移动到链接上以后的颜色变化和小图标变化。
      ok,不用表格的菜单就这样实现了。大家可以明显感觉到,原来写在HTML里的表现样式全部剥离放到CSS文件里去了。页面代码节约了大半。通过CSS要修改菜单样式就很简单了。

      2.不用表格的菜单(横向)
      上面是纵向的菜单,如果要显示横向菜单,用li也可以吗?当然是可以的,下面给出代码,效果就在本页顶部:

      页面代码


      样式表代码

      #submenu {
      MARGIN: 0px 8px 0px 8px;
      PADDING: 4px 0px 0px 0px;
      BORDER: #fff 1px solid;
      BACKGROUND: #dfdfdf;
      COLOR: #666;
      HEIGHT:25px; }

      #submenu ul {
      CLEAR: left;
      MARGIN: 0px;
      PADDING:0px;
      BORDER: 0px;
      LIST-STYLE-TYPE: none;
      TEXT-ALIGN: center;
      DISPLAY:inline;
      }

      #submenu li {
      FLOAT: left;
      DISPLAY: block;
      MARGIN: 0px;
      PADDING: 0px;
      TEXT-ALIGN: center}

      #submenu li a {
      DISPLAY: block;
      PADDING:2px 3px 2px 3px;
      BACKGROUND: url(/bok/images/icon_dot_lmenu.gif) transparent no-repeat 2px 8px;
      FONT-WEIGHT: bold;
      WIDTH: 100%;
      COLOR: #444;
      TEXT-DECORATION: none;
      }

      #submenu li a:hover {
      BACKGROUND: url(/bok/images/icon_dot_lmenu2.gif) #C61C18 no-repeat 2px 8px;
      COLOR: #fff; }

      #submenu ul li#one A { WIDTH: 60px}
      #submenu ul li#two A { WIDTH: 80px}
      #submenu ul li#three A { WIDTH: 80px}
      #submenu ul li#four A { WIDTH: 90px}
      #submenu ul li#five A { WIDTH: 80px}
      #submenu ul li#six A { WIDTH: 80px}
      #submenu ul li#seven A { WIDTH: 60px}
      #submenu ul li#eight A { WIDTH: 90px}
      #submenu ul li#nine A { WIDTH: 80px}

      以上代码不逐一分析了。横向菜单的关键在于:定义
    • 样式时的"FLOAT: left;"语句。另外注意UL定义中的DISPLAY:inline;一句表示将li强制作为内联对象呈递,从对象中删除行,通俗讲就是li不换行。实现横向排列。你也可以象例子中定义每个子菜单的宽度,控制菜单的间隔。好了,你也可以动手试试,用li实现各种各样的菜单样式。

      Tips:如果你子菜单的宽度总和大于层的宽度,菜单会自动折行,利用这个原理可以实现单个无序列表的2列或者3列排版,这是原来HTML很难实现的。
  • Statement
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update?Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update?Mar 04, 2025 pm 12:32 PM

    The official account web page update cache, this thing is simple and simple, and it is complicated enough to drink a pot of it. You worked hard to update the official account article, but the user still opened the old version. Who can bear the taste? In this article, let’s take a look at the twists and turns behind this and how to solve this problem gracefully. After reading it, you can easily deal with various caching problems, allowing your users to always experience the freshest content. Let’s talk about the basics first. To put it bluntly, in order to improve access speed, the browser or server stores some static resources (such as pictures, CSS, JS) or page content. Next time you access it, you can directly retrieve it from the cache without having to download it again, and it is naturally fast. But this thing is also a double-edged sword. The new version is online,

    How do I use HTML5 form validation attributes to validate user input?How do I use HTML5 form validation attributes to validate user input?Mar 17, 2025 pm 12:27 PM

    The article discusses using HTML5 form validation attributes like required, pattern, min, max, and length limits to validate user input directly in the browser.

    How to efficiently add stroke effects to PNG images on web pages?How to efficiently add stroke effects to PNG images on web pages?Mar 04, 2025 pm 02:39 PM

    This article demonstrates efficient PNG border addition to webpages using CSS. It argues that CSS offers superior performance compared to JavaScript or libraries, detailing how to adjust border width, style, and color for subtle or prominent effect

    What are the best practices for cross-browser compatibility in HTML5?What are the best practices for cross-browser compatibility in HTML5?Mar 17, 2025 pm 12:20 PM

    Article discusses best practices for ensuring HTML5 cross-browser compatibility, focusing on feature detection, progressive enhancement, and testing methods.

    What is the purpose of the <datalist> element?What is the purpose of the <datalist> element?Mar 21, 2025 pm 12:33 PM

    The article discusses the HTML <datalist> element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

    What is the purpose of the <meter> element?What is the purpose of the <meter> element?Mar 21, 2025 pm 12:35 PM

    The article discusses the HTML <meter> element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates <meter> from <progress> and ex

    How do I use the HTML5 <time> element to represent dates and times semantically?How do I use the HTML5 <time> element to represent dates and times semantically?Mar 12, 2025 pm 04:05 PM

    This article explains the HTML5 <time> element for semantic date/time representation. It emphasizes the importance of the datetime attribute for machine readability (ISO 8601 format) alongside human-readable text, boosting accessibilit

    What is the purpose of the <progress> element?What is the purpose of the <progress> element?Mar 21, 2025 pm 12:34 PM

    The article discusses the HTML <progress> element, its purpose, styling, and differences from the <meter> element. The main focus is on using <progress> for task completion and <meter> for stati

    See all articles

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    AI Hentai Generator

    AI Hentai Generator

    Generate AI Hentai for free.

    Hot Article

    R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
    2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
    Repo: How To Revive Teammates
    1 months agoBy尊渡假赌尊渡假赌尊渡假赌
    Hello Kitty Island Adventure: How To Get Giant Seeds
    4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

    Hot Tools

    SAP NetWeaver Server Adapter for Eclipse

    SAP NetWeaver Server Adapter for Eclipse

    Integrate Eclipse with SAP NetWeaver application server.

    MinGW - Minimalist GNU for Windows

    MinGW - Minimalist GNU for Windows

    This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

    VSCode Windows 64-bit Download

    VSCode Windows 64-bit Download

    A free and powerful IDE editor launched by Microsoft

    MantisBT

    MantisBT

    Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

    mPDF

    mPDF

    mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),