导航栏困扰初学者的小竖线

我们平时做导航栏,在两个超链接中有一条小竖线,该怎么实现呢,讲三种方法:

在这里插入图片描述
html样式

<div>
	<a href="#">首页</a>
	<a href="#">网站导航</a>
	<a href="#">教育网</a>
	<a href="#" class="last">English</a>
</div>

第一种:边框实现法

<style>
* {
	margin: 0;
	padding: 0;
}
div {
	height: 40px;
	background: #999;
}
a {
	text-decoration: none;
	color: #000;
	float: left;
	padding: 0 10px;
	font-size: 12px;
	border-right: 1px solid #000;
	margin-top: 10px;
}
.last {
	border-right: none;
}
</style>

第二种:背景图片实现法

<style>
	* {
		margin: 0;
		padding: 0;
	}
	div {
		height: 40px;
		background: #999;
		font-size: 12px;
		line-height: 40px;
	}
	a {
		text-decoration: none;
		color: #000;
		float: left;
		padding: 0 10px;
		background: url(line.jpg) no-repeat right center;
	}
	.last {
		background: none;
	}
</style>

这种方法就没有那么多margin,padding 了

第三种:手写实现法

html样式

<div>
	<a href="#">首页</a>
	<span>|</span>
	<a href="#">网站导航</a>
	<span>|</span>
	<a href="#">教育网</a>
	<span>|</span>
	<a href="#" class="last">English</a>
</div>

CSS样式

<style>
	* {
		margin: 0;
		padding: 0;
	}
	div {
		height: 40px;
		background: #999;
		font-size: 12px;
		line-height: 40px;
	}
	a {
		text-decoration: none;
		color: #000;
		float: left;
		padding: 0 10px;
	}
	span {
		float: left;
	}
</style>
  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
您可以使用CSS中的伪元素:before或:after来实现竖向导航栏间隔竖线的效果。具体实现方法如下: 1. 首先,给导航栏中的每个选项添一个共同的类名,例如.nav-item。 2. 然后,在CSS中为这个类名添样式,包括宽度、高度、背景颜色等。 3. 接着,使用:before或:after伪元素来添竖线。具体实现方法是,设置伪元素的宽度为1px,高度为导航栏选项的高度,背景颜色为竖线的颜色,然后将伪元素定位到导航栏选项的左侧或右侧。 下面是一个示例代码: HTML代码: ``` <ul class="nav"> <li class="nav-item">选项1</li> <li class="nav-item">选项2</li> <li class="nav-item">选项3</li> </ul> ``` CSS代码: ``` .nav { list-style: none; margin: 0; padding: 0; } .nav-item { display: inline-block; height: 30px; line-height: 30px; padding: 0 10px; position: relative; } .nav-item:before { content: ""; width: 1px; height: 100%; background-color: #ccc; position: absolute; left: -10px; top: 0; } .nav-item:first-child:before { display: none; } ``` 在上面的代码中,我们使用:before伪元素来添竖线,将其宽度设置为1px,高度设置为100%(即与导航栏选项的高度相同),背景颜色设置为#ccc。然后,将伪元素定位到导航栏选项的左侧,使用left属性设置偏移量为-10px(即向左偏移10px),top属性设置为0。最后,使用:first-child伪类来隐藏第一个导航栏选项左侧的竖线

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值