导航分隔符

发布于 2024-11-01 23:21:04 字数 330 浏览 5 评论 0原文

我需要在导航元素之间添加分隔符。分隔符是图像。

元素之间的分隔符。

我的 HTML 结构如下: ol >力> a> img.

这里我提出了两种可能的解决方案:

  1. 要添加更多 li 标签进行分隔(嘘!),
  2. 在每个元素的图像中包含分隔符(这更好,但它使用户可能点击,例如,“主页”,但进入“服务”,因为它们一个接一个,用户可能会意外单击属于“服务”的分隔符);

该怎么办?

I need to add separators between elements of navigation. Separators are images.

Separators between elements.

My HTML structure is like: ol > li > a > img.

Here I come to two possible solutions:

  1. To add more li tags for separation (boo!),
  2. Include separator in image of each element (this is better, but it makes possibility that user may click on, example, "Home", but get to "Services", because they are one behind the other and user may accidentally click on separator that belongs to "Services");

What to do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

西瑶 2024-11-08 23:21:04

如果没有迫切需要使用图像作为分隔符,您可以使用纯 CSS 来完成此操作。

nav li + li:before{
    content: " | ";
    padding: 0 10px;
}

这会在每个列表项之间放置一个栏,就像原始问题中所描述的图像一样。但由于我们使用的是相邻选择器,所以它不会将横线放在前面第一个元素。由于我们使用的是 :before 伪选择器,因此它不会在末尾添加一个。

If there isn't a pressing need to use images for the separators, you could do this with pure CSS.

nav li + li:before{
    content: " | ";
    padding: 0 10px;
}

This puts a bar between each list item, just as the image in the original question described. But since we're using the adjacent selectors, it doesn't put the bar before the first element. And since we're using the :before pseudo selector, it doesn't put one at the end.

余厌 2024-11-08 23:21:04

只需使用分隔符图像作为 li 上的背景图像即可。

要使其仅出现在列表项之间,请将图像放置在 li 的左侧,但不要放在第一个项上。

例如:

#nav li + li {
    background:url('seperator.gif') no-repeat top left;
    padding-left: 10px
}

此 CSS 将图像添加到另一个列表项后面的每个列表项 - 换句话说,除了第一个之外的所有列表项。

注意。请注意,相邻选择器 (li + li) 在 IE6 中不起作用,因此您必须将背景图像添加到常规 li(带有条件样式表),并且可能应用负边距到边缘之一。

Simply use the separator image as a background image on the li.

To get it to only appear in between list items, position the image to the left of the li, but not on the first one.

For example:

#nav li + li {
    background:url('seperator.gif') no-repeat top left;
    padding-left: 10px
}

This CSS adds the image to every list item that follows another list item - in other words all of them but the first.

NB. Be aware the adjacent selector (li + li) doesn't work in IE6, so you will have to just add the background image to the conventional li (with a conditional stylesheet) and perhaps apply a negative margin to one of the edges.

情绪失控 2024-11-08 23:21:04

其他解决方案都可以,但是如果使用 :after 则无需在最后添加分隔符,如果使用 :before 则无需在最开头添加分隔符。

SO:

案例:之后

.link:after {
  content: '|';
  padding: 0 1rem;
}

.link:last-child:after {
  content: '';
}

案例:之前

.link:before {
  content: '|';
  padding: 0 1rem;
}

.link:first-child:before {
  content: '';
}

The other solution are OK, but there is no need to add separator at the very last if using :after or at the very beginning if using :before.

SO:

case :after

.link:after {
  content: '|';
  padding: 0 1rem;
}

.link:last-child:after {
  content: '';
}

case :before

.link:before {
  content: '|';
  padding: 0 1rem;
}

.link:first-child:before {
  content: '';
}
迟月 2024-11-08 23:21:04

要使分隔符相对于菜单文本垂直居中,

.menustyle li + li:before {
  content: ' | ';
  padding: 0;
  position: relative;
  top: -2px;
}

To get the separator vertically centered relative to the menu text,

.menustyle li + li:before {
  content: ' | ';
  padding: 0;
  position: relative;
  top: -2px;
}
旧城烟雨 2024-11-08 23:21:04

您可以在要添加分隔符的位置添加一个 li 元素

<ul>
    <li> your content </li>
    <li class="divider-vertical-second-menu"></li>
    <li> NExt content </li>
    <li class="divider-vertical-second-menu"></li>
    <li> last item </li>
</ul>

在 CSS 中,您可以添加以下代码。

.divider-vertical-second-menu{
   height: 40px;
   width: 1px;
   margin: 0 5px;
   overflow: hidden;
   background-color: #DDD;
   border-right: 2px solid #FFF;
}

这将提高执行速度,因为它不会加载任何图像。只是测试一下..:)

You can add one li element where you want to add divider

<ul>
    <li> your content </li>
    <li class="divider-vertical-second-menu"></li>
    <li> NExt content </li>
    <li class="divider-vertical-second-menu"></li>
    <li> last item </li>
</ul>

In CSS you can Add following code.

.divider-vertical-second-menu{
   height: 40px;
   width: 1px;
   margin: 0 5px;
   overflow: hidden;
   background-color: #DDD;
   border-right: 2px solid #FFF;
}

This will increase you speed of execution as it will not load any image. just test it out.. :)

浅唱ヾ落雨殇 2024-11-08 23:21:04

将分隔符添加到 li 背景,并确保链接不会展开以覆盖分隔符,这意味着分隔符将不可点击。

Add the separator to the li background and make sure the link doesn't expand to cover the separator, which means the separator won't be click-able.

缱绻入梦 2024-11-08 23:21:04

对于那些使用 Sass 的人,我写了一个 mixin 用于此目的:

@mixin addSeparator($element, $separator, $padding) {
    #{$element+'+'+$element}:before {
        content: $separator;
        padding: 0 $padding;
    }
}

示例:

@include addSeparator('li', '|', 1em);

这会给你这个:

li+li:before {
  content: "|";
  padding: 0 1em;
}

For those using Sass, I have written a mixin for this purpose:

@mixin addSeparator($element, $separator, $padding) {
    #{$element+'+'+$element}:before {
        content: $separator;
        padding: 0 $padding;
    }
}

Example:

@include addSeparator('li', '|', 1em);

Which will give you this:

li+li:before {
  content: "|";
  padding: 0 1em;
}
草莓酥 2024-11-08 23:21:04

我相信最好的解决方案就是我使用的,这是一个自然的 css 边框:

border-right:1px solid;

您可能需要注意填充,例如:

padding-left: 5px;
padding-right: 5px;

最后,如果您不希望最后一个 li 有那个分离的边框,请给它“border-right”中的最后一个孩子“none”如下所示:

li:last-child{
  border-right:none;
}

祝你好运:)

I believe the best solution for this, is what I use, and that is a natural css border:

border-right:1px solid;

You might need to take care of padding like:

padding-left: 5px;
padding-right: 5px;

Finally, if you don't want the last li to have that seperating border, give it's last-child "none" in "border-right" like this:

li:last-child{
  border-right:none;
}

Good luck :)

网名女生简单气质 2024-11-08 23:21:04

将其作为列表元素的背景:

<ul id="nav">
    <li><a><img /></a></li>
    ...
    <li><a><img /></a></li>
</ul>

#nav li{background: url(/images/separator.gif) no-repeat left; padding-left:20px;} 
/* left padding creates a gap between links */

接下来,我建议使用不同的标记以实现可访问性:
不要内嵌图像,而是将文本作为文本放入,用跨度包围每个图像,将图像应用为背景,然后使用 display:none 隐藏文本 - 这提供了更多的样式灵活性,并允许您使用 1 像素宽的背景图像平铺,可以节省带宽,并且您可以将其嵌入到 CSS 精灵中,从而节省 HTTP 调用:

HTML:

<ul id="nav">
    <li><a><span>link text</span></a></li>
    ...
    <li><a><span>link text</span></a></li>
</ul

CSS:

#nav li{background: url(/images/separator.gif) no-repeat left; padding-left:20px;} 
#nav a{background: url(/images/nav-bg.gif) repeat-x;}
#nav a span{display:none;}

UPDATE
好的,我看到其他人在我之前得到了类似的答案 - 我注意到 John 还包括一种通过使用 li + li 选择器来防止分隔符出现在第一个元素之前的方法 - 这意味着任何 li 都在另一个 li 之后。

Put it in as a background on the list element:

<ul id="nav">
    <li><a><img /></a></li>
    ...
    <li><a><img /></a></li>
</ul>

#nav li{background: url(/images/separator.gif) no-repeat left; padding-left:20px;} 
/* left padding creates a gap between links */

Next, I recommend a different markup for accessibility:
Rather than embedding the images inline, put text in as text, surround each with a span, apply the image as a background the the , and then hide the text with display:none -- this gives much more styling flexibilty, and allows you to use tiling with a 1px wide bg image, saves bandwidth, and you can embed it in a CSS sprite, which saves HTTP calls:

HTML:

<ul id="nav">
    <li><a><span>link text</span></a></li>
    ...
    <li><a><span>link text</span></a></li>
</ul

CSS:

#nav li{background: url(/images/separator.gif) no-repeat left; padding-left:20px;} 
#nav a{background: url(/images/nav-bg.gif) repeat-x;}
#nav a span{display:none;}

UPDATE
OK, I see others got similar answer in before me -- and I note that John also includes a means for keeping the separator from appearing before the first element, by using the li + li selector -- which means any li coming after another li.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文