html - 水平列表的响应式分隔符

标签 html css responsive-design html-lists

这个问题扩展了'Separators For Navigation ' 通过询问,如何删除由视口(viewport)大小引起的换行符处的分隔符。

宽视口(viewport)
->       Item 1 | Item 2 | Item 3 | Item 4 | Item 5       <-
小视口(viewport)
->  Item 1 | Item 2 | Item 3  <-
->      Item 4 | Item 5       <-

这是一个 fiddle ,显示管道如何保持在换行符处:

Fiddle .

我对纯 css 解决方案感兴趣,但如果 javascript 提供了唯一可能的解决方案,它也是可以接受的。

最佳答案

您可以利用尾随和行尾随空格自动折叠的事实:

document.write(
 'word<b style="background: red; outline: 1px solid blue;"> </b>'
 .repeat(42)
);

如您所见,单词之间有带有蓝色轮廓的红色空间,但行尾的最后一个和两个缺少红色区域,因为它的宽度折叠为零:这实际上是空白折叠。

可以使用 word-spacing 调整宽度并改用伪元素,因此设置内联 ::after { content: ' '; word-spacing: 2em; } 会为您提供宽内联矩形,该矩形可以装饰背景或边框,但在不在单词之间时会消失。

简化的用例(来自 https://codepen.io/myf/pen/dyOzpZM,仅在 2021-02 常绿 Firefox 和 Chromium 中进行了测试,将无法在 Chromium Edge 之前的版本中运行;有关更强大的示例,请参见下面的第二个片段):

ul {
  text-align: center;
  padding: 0;
}
li {
  display: inline;
}
li::after {
  /*
   This has to be space, tab or other
   breakable white-space character:
  */
  content: " ";
  word-spacing: 1em;
  background-image: linear-gradient(
    -0.2turn,
    transparent 0 calc(50% - 0.03em),
    currentcolor 0 calc(50% + 0.03em),
    transparent 0
  );
}
/*
 That's it: just inline text
 with styled ::after spaces
 that collapse at line breaks
 and at the end of the element.
 
 That's basically how spaces work in text.
*/

/*
 Unrelated whimsical effects:
*/
body { background: #456; color: #fed; min-height: 100vh; margin: 0; display: flex; align-items: center; }
ul { --dur: 3s; font-family: Georgia, serif; font-size: min(7vw, calc(100vh / 7)); margin: 0 auto; position: relative; padding: 0 1em; -webkit-text-fill-color: #999; text-transform: capitalize; animation: poing var(--dur) infinite alternate ease-in-out; }
@keyframes poing { from { max-width: 3.4em; } to { max-width: min(19em, calc(100vw - 2em)); color: lime; } }
ul::before, ul::after { -webkit-text-fill-color: currentcolor; position: absolute; top: 50%; transform: translatey(-50%); animation: calc(var(--dur) * 2) calc(var(--dur) * -1.5) infinite forwards linear; }
ul::before { content: "☜"; left: 0; animation-name: a !important; }
ul::after { content: "☞"; right: 0; animation-name: b !important; }
@keyframes a { 50% { content: "☛"; } }
@keyframes b { 50% { content: "☚"; } }
ul:hover, ul:hover::before, ul:hover::after { animation-play-state: paused; }
<ul>
 <li>foo</li>
 <li>bar</li>
 <li>baz</li>
 <li>gazonk</li>
 <li>qux</li>
 <li>quux</li>
</ul>

它使用带有单个单词项的平面列表,因此与现实世界的使用关系不大。适合导航的更现实的变体可能类似于 https://jsfiddle.net/vnudrsh6/7/ :

nav {
  text-align: center;
  padding-right: 1em; /* = li::after@word-spacing */
}
ul {
  display: inline;
  margin: 0;
  padding: 0;
}
li {
  display: inline;
  /*
   white-space: nowrap should be moved to child A
   because IE fails to wrap resulting list completely
  */
}
li::before {
  content: ' ';
  /*
   this content is important only for Chrome in case
   the HTML will be minified with *no whitespaces* between </li><li>
  */
}
li::after {
  content: ' ';
  /*
   this is actual placeholder for background-image
   and it really must be space (or tab)
  */
  white-space: normal;
  word-spacing: 1em;
  /*
   = nav@padding-right - this actually makes width
  */
  background-image: radial-gradient(circle, black, black 7%, transparent 15%, transparent 35%, black 45%, black 48%, transparent 55%);
  background-size: 1em 1em;
  background-repeat: no-repeat;
  background-position: center center;
  opacity: 0.5;
}
/*
 no need to unset content of li:last-child::after
 because last (trailing) space collapses anyway
*/
a {
  white-space: nowrap;
  display: inline-block; /* for padding */
  padding: 1em;
  text-decoration: none;
  color: black;
  transition-property: background-color;
  transition-duration: 500ms;
}
a:hover {
  background-color: #ccc;
}
/*
 For demonstrative purposes only
 Give items some content and uneven width
*/
nav:hover > ul > li {
  outline: 3px dotted rgba(0,0,255,.5);
  outline-offset: -3px;
}
nav:hover > ul > li::after {
  opacity: 1;
  background-color: rgba(255, 0, 0, .5);
}
nav:hover > ul > li:hover {
  outline-style: solid;
}
nav:hover > ul > li:hover::after  {
  background-color: cyan;
}

nav:hover > ul > li > a {
  outline: 3px solid rgba(0,255,0,.5);
  outline-offset: -3px;
}

nav > ul {
  counter-reset: c;
}
nav > ul > li {
  counter-increment: c;
}
nav > ul > li > a::before {
  content: counter(c, upper-roman) '. ';
  letter-spacing: .3em;
}
nav > ul > li > a::after {
  content: ' item ' counter(c, lower-roman);
  word-spacing: .3em;
  letter-spacing: .1em;
  transform: translatex(.1em);
  display: inline-block;
}
<nav>
  <ul><li><a href="#"></a></li><li><a href="#"></a></li><li><a href="#"></a></li><li><a href="#"></a></li><li><a href="#"></a></li><li><a href="#"></a></li><li><a href="#"></a></li><li><a href="#"></a></li><li><a href="#"></a></li><li><a href="#"></a></li><li><a href="#"></a></li><li><a href="#"></a></li><li><a href="#"></a></li><li><a href="#"></a></li><li><a href="#"></a></li><li><a href="#"></a></li><li><a href="#"></a></li><li><a href="#"></a></li><li><a href="#"></a></li><li><a href="#"></a></li><li><a href="#"></a></li>
  </ul>
</nav>
<!--  For demonstrative purposes is content of links made by CSS
-->

此概念验证在每个 <li> 之后使用“最终折叠”CSS 生成内容的背景图像空格。 2016 年在 Firefox、Chrome 和 IE11 中测试。


其他值得注意的答案:

关于html - 水平列表的响应式分隔符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37052659/

相关文章:

css - 如何停止用户代理样式表覆盖我的颜色样式以获得更长的下拉列表?

css - 图像不会为 HTML5 响应式网站调整大小

javascript - 将事件添加到 HTML 输入数组的所有元素并获取数组中的值

html - 如何在元素底部使用::before 选择器?

javascript - PHP 数组排序到 HTML 表中

html - 更改 Bootstrap 的响应能力

jquery - 可以在媒体查询中隐藏特定字符吗?

html - 元素在网络服务器上消失

使用前置触发器的 jQuery 下拉菜单

css - 为什么要使用属性选择器来匹配类?