简体   繁体   English

CSS:背景如何延伸边框和重叠?

[英]CSS: How the backgrounds can extend the border and overlap?

I'm designing a navigation bar as shown in image below (a) with the following code:我正在设计一个导航栏,如下图(a)所示,代码如下:

<ul>
  <li class="unselected">Step 1</li>
  <li class="selected">Step 2</li>
  <li class="unselected">Step 3</li>
  <li class="unselected">Step 4</li>
  <li class="unselected">Step 5</li>
</ul>

导航栏

I want to have one background image for unselected steps (d) and one for the selected step (c).我想为未选择的步骤 (d) 设置一个背景图像,为选定的步骤 (c) 设置一个背景图像。 For simplicity let's assume Step 1 and Step 5 use the same background as well.为简单起见,我们假设步骤 1 和步骤 5 也使用相同的背景。 I want to adjust the button background in HTML only with a class name.我想仅使用 class 名称调整 HTML 中的按钮背景。

The question is how can I achieve the result with CSS?问题是如何使用 CSS 实现结果? I just want to know how background of two neighbor elements can overlap each other ?我只想知道两个相邻元素的背景如何相互重叠

Edit: the steps are links.编辑:步骤是链接。 the background is a transparent PNG file preferably only containing the blue or gray shape and its border.背景是透明的 PNG 文件,最好只包含蓝色或灰色的形状及其边框。

Answer: http://jsfiddle.net/morrison/99LhB/答案: http://jsfiddle.net/morrison/99LhB/

Notes:笔记:

  • Click-boxes will be messed up on diagonals.点击框会在对角线上弄乱。 I just realized that this will always be the case.我才意识到这将永远是这样。 I'd decrease the width of the arrow if I were you to help avoid this issue.如果我是您帮助避免这个问题,我会减小箭头的宽度。 I would also add a hover state which would help clarify which one you are hovering on.我还将添加一个 hover state 这将有助于澄清您悬停在哪一个上。 If they aren't hyperlinks, this doesn't matter: feel free to remove those css rules.如果它们不是超链接,那没关系:随意删除那些 css 规则。
  • HTML simplicity makes for CSS complexity in this case.在这种情况下,HTML 的简单性导致了 CSS 的复杂性 There are less classes to worry about, but now we rely on CSS selectors.需要担心的类更少,但现在我们依赖 CSS 选择器。 I would personally choose this way over the other, but that's a personal choice.我个人会选择这种方式而不是另一种方式,但这是个人选择。
  • There's only one image.只有一张图。 Uses a CSS sprite to accomplish this.使用 CSS sprite 来完成此操作。 It also speeds up the webpage a little.它还加快了网页速度。
  • Shows how it looks for all 5 steps.显示所有 5 个步骤的外观。

You can do this.你可以这样做。 what you want to do is use a negative margin.你想要做的是使用负边距。

.someclass {
    margin-left: -5px;
}

That should overlap the each of the elements (if applied to all li objects).这应该与每个元素重叠(如果应用于所有 li 对象)。

They can't overlap only the background, but html element might be stacked.它们不能只与背景重叠,但 html 元素可能会堆叠。 However I'd recommend such a solution only if you have no other.但是,仅当您没有其他解决方案时,我才会推荐这样的解决方案。

In your visual example, I guess that must be something like that:在您的视觉示例中,我想一定是这样的:

Html: Html:

<ul>
  <li class="before_selected">Step 1</li>
  <li class="selected">Step 2</li>
  <li class="after_selected">Step 3</li>
  <li class="unselected">Step 4</li>
  <li class="unselected">Step 5</li>
</ul>

CSS: CSS:

.unselected {
  background-image: url('all_grey.jpg');
}
.before_selected {
  background-image: url('left_grey_right_blue.jpg');
}
.after_selected {
  background-image: url('left_blue_right_grey.jpg');
}
.selected {
  background-image: url('all_blue.jpg');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM