热门标签 | 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%。


推荐阅读
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • Ihavethefollowingonhtml我在html上有以下内容<html><head><scriptsrc..3003_Tes ... [详细]
  • Java太阳系小游戏分析和源码详解
    本文介绍了一个基于Java的太阳系小游戏的分析和源码详解。通过对面向对象的知识的学习和实践,作者实现了太阳系各行星绕太阳转的效果。文章详细介绍了游戏的设计思路和源码结构,包括工具类、常量、图片加载、面板等。通过这个小游戏的制作,读者可以巩固和应用所学的知识,如类的继承、方法的重载与重写、多态和封装等。 ... [详细]
  • 本文介绍了闭包的定义和运转机制,重点解释了闭包如何能够接触外部函数的作用域中的变量。通过词法作用域的查找规则,闭包可以访问外部函数的作用域。同时还提到了闭包的作用和影响。 ... [详细]
  • 目录实现效果:实现环境实现方法一:基本思路主要代码JavaScript代码总结方法二主要代码总结方法三基本思路主要代码JavaScriptHTML总结实 ... [详细]
  • Java容器中的compareto方法排序原理解析
    本文从源码解析Java容器中的compareto方法的排序原理,讲解了在使用数组存储数据时的限制以及存储效率的问题。同时提到了Redis的五大数据结构和list、set等知识点,回忆了作者大学时代的Java学习经历。文章以作者做的思维导图作为目录,展示了整个讲解过程。 ... [详细]
  • 阿,里,云,物,联网,net,core,客户端,czgl,aliiotclient, ... [详细]
  • 本文介绍了OC学习笔记中的@property和@synthesize,包括属性的定义和合成的使用方法。通过示例代码详细讲解了@property和@synthesize的作用和用法。 ... [详细]
  • 本文介绍了在Linux下安装Perl的步骤,并提供了一个简单的Perl程序示例。同时,还展示了运行该程序的结果。 ... [详细]
  • FeatureRequestIsyourfeaturerequestrelatedtoaproblem?Please ... [详细]
  • Html5-Canvas实现简易的抽奖转盘效果
    本文介绍了如何使用Html5和Canvas标签来实现简易的抽奖转盘效果,同时使用了jQueryRotate.js旋转插件。文章中给出了主要的html和css代码,并展示了实现的基本效果。 ... [详细]
  • Go Cobra命令行工具入门教程
    本文介绍了Go语言实现的命令行工具Cobra的基本概念、安装方法和入门实践。Cobra被广泛应用于各种项目中,如Kubernetes、Hugo和Github CLI等。通过使用Cobra,我们可以快速创建命令行工具,适用于写测试脚本和各种服务的Admin CLI。文章还通过一个简单的demo演示了Cobra的使用方法。 ... [详细]
  • IjustinheritedsomewebpageswhichusesMooTools.IneverusedMooTools.NowIneedtoaddsomef ... [详细]
  • CSS3选择器的使用方法详解,提高Web开发效率和精准度
    本文详细介绍了CSS3新增的选择器方法,包括属性选择器的使用。通过CSS3选择器,可以提高Web开发的效率和精准度,使得查找元素更加方便和快捷。同时,本文还对属性选择器的各种用法进行了详细解释,并给出了相应的代码示例。通过学习本文,读者可以更好地掌握CSS3选择器的使用方法,提升自己的Web开发能力。 ... [详细]
  • sklearn数据集库中的常用数据集类型介绍
    本文介绍了sklearn数据集库中常用的数据集类型,包括玩具数据集和样本生成器。其中详细介绍了波士顿房价数据集,包含了波士顿506处房屋的13种不同特征以及房屋价格,适用于回归任务。 ... [详细]
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社区 版权所有