热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

导航栏中的水平列表项-Horizontallistitemsinanavigationbar

IamtryingtobuildanavigationbarliketheGithubsone(withoutusingtheBootstrapnav*select

I am trying to build a navigation bar like the Github's one (without using the Bootstrap nav* selectors - please see this jsfiddle enter image description here

我正在尝试构建一个像Github一样的导航栏(不使用Bootstrap导航*选择器 - 请看这个jsfiddle

So I have the following HTML:

所以我有以下HTML:


Note the span9 class that I am using to force the navigation bar to occupy only 9/12 of my screen.

请注意我用来强制导航栏只占据屏幕9/12的span9类。

And the following CSS:

以下CSS:

.main-tabs {
    position: relative;
    margin-bottom: 20px;
    font-size: 12px;
    font-weight: bold;
    background-color: #eaeaea;
    background-image: -moz-linear-gradient(#fafafa, #eaeaea);
    background-image: -webkit-linear-gradient(#fafafa, #eaeaea);
    background-image: linear-gradient(#fafafa, #eaeaea);
    background-repeat: repeat-x;
    border: 1px solid #eaeaea;
    border-bottom-color: #cacaca;
    border-radius: 3px;
}

.main-tabs a {
    display: block;
    text-align: center;
    line-height: 35px;
    font-size: 12px;
    color: #777;
    text-decoration: none;
    text-shadow: 0 1px 0 white;
    border-right: 1px solid #eee;
    border-right-color: rgba(0,0,0,0.04);
    border-left: 1px solid #fcfcfc;
    border-left-color: rgba(255,255,255,0.7);
    border-bottom: 2px solid #DADADA;
}

.main-tabs li {
    list-style-type: none;
}

.main-tabs li .active {
    border-bottom: 2px solid greenyellow;
}

The result is: enter image description here

结果是:

I changed .main-tabs li to:

我把.main-tabs li更改为:

 .main-tabs li {
        list-style-type: none;
        float: left;
    }

Then

.main-tabs li {
    list-style-type: none;
    display: block;
}

This still gives the same results :(

这仍然给出相同的结果:(

But this did not really help. enter image description here

但这并没有真正帮助。

QUESTION Without hardcoding the width of li or ul elements (for responsiveness matters), is there a way to get the same navigation bar like the one on Github?

问题没有硬编码li或ul元素的宽度(为了响应性问题),有没有办法获得与Github上相同的导航栏?

2 个解决方案

#1


5  

An old, tried-and-tested way is to use table layout:

一种久经考验的旧方法是使用表格布局:

.nav {
    display: table;
    width: 100%;
    margin: 0;
    padding: 0;
}

.nav li {
    display: table-cell;
}

A modern approach is to use flexbox (IE 10+, may require a lot of prefixes, since there are three versions of the standard):

现代的方法是使用flexbox(IE 10+,可能需要很多前缀,因为标准有三个版本):

.nav-alt {
    display: flex;
    margin: 0;
    padding: 0;
}

.nav-alt li {
    flex: auto;
    list-style-type: none;
}

Demo for both versions: http://jsfiddle.net/3qM5y/1/

两个版本的演示:http://jsfiddle.net/3qM5y/1/

#2


2  

jsFiddle Demo

demo with jQuery and a hover

使用jQuery和悬停进行演示

There are a few issues to address. First, without specifically determining the width of each menu item, you must use infer the width. A common approach is to use the built in algorithm used for calculating table cells. To do this, you will need to set the container to display:table and the items to display:table-cell.

有几个问题需要解决。首先,如果没有专门确定每个菜单项的宽度,则必须使用推断宽度。一种常见的方法是使用用于计算表格单元格的内置算法。为此,您需要将容器设置为display:table和要显示的项:table-cell。

Also, the ul tends to have some default padding, you should probably remove the initial padding by using padding-left: 0px; on the container.

此外,ul倾向于有一些默认填充,你应该删除初始填充使用padding-left:0px;在容器上。

Last, make sure that your anchor elements are going to be properly sized. This means setting them with display: inline-block; and width:100%.

最后,确保您的锚元素的大小合适。这意味着使用display:inline-block设置它们;和宽度:100%。


推荐阅读
author-avatar
jiajian123
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有