css的navigation,CSS: Responsive Navigation Menu

本文介绍了一种无需使用JavaScript即可创建响应式导航菜单的方法。该方法适用于不同屏幕尺寸的设备,包括桌面和移动浏览器,并且支持IE8及以上版本。菜单采用语义化的HTML5标记,可以设置左对齐、居中或右对齐。通过简单的CSS技巧实现菜单项的悬停展开效果,提升用户体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Previously I wrote a tutorial on how to make a mobile navigation for responsive design, now I've discovered a new technique to produce a responsive menu without having to use Javascript. It uses clean and semantic HTML5 markup. The menu can be aligned left, center or right. Unlike the previous tutorial where it is clicked to toggle, this menu toggles on hover which is more user friendly. It also has an indicator to show the active/current menu item. It works on all mobile and desktop browsers including Internet Explorer!

The Purpose

The purpose of this tutorial is to show you how turn a regular list menu into a dropdown menu on smaller display.

34249153_1.png

This trick is more useful on navigation with a lot of links like the screenshot below. You can condense all the buttons into an elegant dropdown.

34249153_2.png

Nav HTML Markup

Here is the markup for the navigation. The tag is required to create the dropdown with the css property absolute position. I will explain this later in the tutorial. The .current class indicates the active/current menu link.

CSS

The CSS for the navigation (desktop view) is pretty straight forward, so I'm not going to get into the details. Note that I specified display:inline-block instead of float:left for the nav

element. This allows the menu buttons to be able to align left, center or right by specifying text-align on the
  • element.

/* nav */

.nav {

position: relative;

margin: 20px 0;

}

.nav ul {

margin: 0;

padding: 0;

}

.nav li {

margin: 0 5px 10px 0;

padding: 0;

list-style: none;

display: inline-block;

}

.nav a {

padding: 3px 12px;

text-decoration: none;

color: #999;

line-height: 100%;

}

.nav a:hover {

color: #000;

}

.nav .current a {

background: #999;

color: #fff;

border-radius: 5px;

}

Center and Right Alignment

As mentioned above, you can change the alignment of the buttons by using text-align property.

/* right nav */

.nav.right ul {

text-align: right;

}

/* center nav */

.nav.center ul {

text-align: center;

}

Internet Explorer Support

HTML5 tag and media query is not supported by Internet Explorer 8 or older. Include css3-mediaqueries.js (or respond.js) and html5shim.js to provide fallback support. If you don't want to add html5shim.js, replace the tag with a

tag.

Responsive

Now here comes the fun part - making the menu responsive with media query! Read my previous articles on responsive design and media query if you are not familar with responsive design.

On 600px breakpoint, I set the nav element to relative position so I can place the

  • menu list on top with absolute position. I hide all
  • elements by specifying display:none, but keep the .current
  • displaying as block. Then on the nav hover, I set all
  • back to display:block (this will produce the dropdown list result). I added a check icon graphic on the .current element to indicate it is the active item. For the center and right alignment menu, use left and right property to position the
    • list. View the demo to see the final result.

    @media screen and (max-width: 600px) {

    .nav {

    position: relative;

    min-height: 40px;

    }

    .nav ul {

    width: 180px;

    padding: 5px 0;

    position: absolute;

    top: 0;

    left: 0;

    border: solid 1px #aaa;

    background: #fff url(images/icon-menu.png) no-repeat 10px 11px;

    border-radius: 5px;

    box-shadow: 0 1px 2px rgba(0,0,0,.3);

    }

    .nav li {

    display: none; /* hide all

  • items */

    margin: 0;

    }

    .nav .current {

    display: block; /* show only current

  • item */

    }

    .nav a {

    display: block;

    padding: 5px 5px 5px 32px;

    text-align: left;

    }

    .nav .current a {

    background: none;

    color: #666;

    }

    /* on nav hover */

    .nav ul:hover {

    background-image: none;

    }

    .nav ul:hover li {

    display: block;

    margin: 0 0 5px;

    }

    .nav ul:hover .current {

    background: url(images/icon-check.png) no-repeat 10px 7px;

    }

    /* right nav */

    .nav.right ul {

    left: auto;

    right: 0;

    }

    /* center nav */

    .nav.center ul {

    left: 50%;

    margin-left: -90px;

    }

    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值