CSS:元素列表的边框,不重叠但可以高亮显示

huangapple go评论65阅读模式
英文:

CSS: Border for list of elements, no overlap but highlighting possible

问题

我有一个带有边框的元素列表。边框不应该重叠。但是:可能有不可见的元素,而且一个元素可能会被不同边框颜色突出显示。

<ul id="navigation_bar">
  <li class="first">Projection</li>
  <li class="highlighted">Real-Time</li>
  <li style="display:none;">Invisible</li>
  <li>Cleanup</li>
</ul>
#navigation_bar {
  overflow: hidden;
  list-style-type: none;
  width: 100%;
}

#navigation_bar li {
  text-align: center;
  width: 32%;
  border: 1px solid black;
  margin-top: -1px;
}

#navigation_bar li.first {
  margin-top: 0px;
}

#navigation_bar li.highlighted {
  border: 1px solid red;
}

问题是边框要么重叠,要么没有完全突出显示(正如您在示例中看到的,“Real-Time”的底部边框是黑色的,但应该是红色的)。

有没有仅使用CSS来解决这个问题的想法?

这是我的fiddle链接:
http://jsfiddle.net/wk9ydo41/1/

英文:

I've a list of elements with a border. The border should not overlap. But: There may be elements which are invisible, and one element might be highlighted by a different border-color:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

#navigation_bar {
  overflow: hidden;
  list-style-type: none;
  width: 100%;
}

#navigation_bar li {
  text-align: center;
  width: 32%;
  border: 1px solid black;
  margin-top: -1px;
}

#navigation_bar li.first {
  margin-top: 0px;
}

#navigation_bar li.hilighted {
  border: 1px solid red;
}

<!-- language: lang-html -->

&lt;ul id=&quot;navigation_bar&quot;&gt;
  &lt;li class=&quot;first&quot;&gt;Projection&lt;/li&gt;
  &lt;li class=&quot;hilighted&quot;&gt;Real-Time&lt;/li&gt;
  &lt;li style=&quot;display:none;&quot;&gt;Invisible&lt;/li&gt;
  &lt;li&gt;Cleanup&lt;/li&gt;
&lt;/ul&gt;

<!-- end snippet -->

My problem is that either the borders overlap, or not the full border is highlighted (as you can see in my example, the bottom-border of "Real-Time" is black, but it should be red).

Any ideas how to do this just using css?

Here's my fiddle:
http://jsfiddle.net/wk9ydo41/1/

答案1

得分: 1

为了使完整的高亮边框可见,您需要使用 z-index 来提升该元素的层级。但是要使用 z-index,您需要设置非静态定位。您可以使用 position: relative

#navigation_bar {
  overflow: hidden;
  list-style-type: none;
  width: 100%;
}

#navigation_bar li {
  text-align: center;
  width: 32%;
  border: 1px solid black;
  margin-top: -1px;
}

#navigation_bar li.first {
  margin-top: 0px;
}

#navigation_bar li.hilighted {
  border: 1px solid red;
  position: relative;
  z-index: 100;
}
<ul id="navigation_bar">
  <li class="first">Projection</li>
  <li class="hilighted">Real-Time</li>
  <li style="display:none;">Invisible</li>
  <li>Cleanup</li>
</ul>
英文:

To have the full highlighted border visible you need to elevate that element with z-index. However to use z-index you need a non-static positioning. You can use position: relative for it:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

#navigation_bar {
  overflow: hidden;
  list-style-type: none;
  width: 100%;
}

#navigation_bar li {
  text-align: center;
  width: 32%;
  border: 1px solid black;
  margin-top: -1px;
}

#navigation_bar li.first {
  margin-top: 0px;
}

#navigation_bar li.hilighted {
  border: 1px solid red;
  position: relative;
  z-index: 100;
}

<!-- language: lang-html -->

&lt;ul id=&quot;navigation_bar&quot;&gt;
  &lt;li class=&quot;first&quot;&gt;Projection&lt;/li&gt;
  &lt;li class=&quot;hilighted&quot;&gt;Real-Time&lt;/li&gt;
  &lt;li style=&quot;display:none;&quot;&gt;Invisible&lt;/li&gt;
  &lt;li&gt;Cleanup&lt;/li&gt;
&lt;/ul&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月29日 20:46:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76581204.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定