【问题标题】:How can I reverse the order of a list in CSS, remove the last value, and keep centered?如何反转 CSS 中列表的顺序,删除最后一个值并保持居中?
【发布时间】:2018-04-20 06:19:43
【问题描述】:

我正在整理一个[网站模板][1]。

编辑器中的代码是:

<div class="post-meta vcard"><p>
<a href="http://wordpress-114402-466332.cloudwaysapps.com/france/" rel="tag">France</a>, 
<a href="http://wordpress-114402-466332.cloudwaysapps.com/france/paris/" rel="tag">Paris</a>, 
<a href="http://wordpress-114402-466332.cloudwaysapps.com/france/paris/things-to-do/" rel="tag">Things to Do in Paris</a>
</p></div>

我希望输出只是法国巴黎。我将如何使用 CSS 实现这一目标?假设步骤将是相反的顺序并删除列表中的第一个变量。

【问题讨论】:

  • 嗨,请在您的问题中包含您正在使用的代码,最好是制作一个 sn-p。谢谢
  • 我刚刚从编辑器中添加了代码。目前输出的是法国,巴黎,巴黎的事情。我希望它只输出法国巴黎。 (这些是文章所在类别的列表)
  • 你试过display: noneflex-direction吗?昏迷在这里会弄得一团糟,也许你可以在::afterpseudo-elements中设置它们。

标签: css frontend reverse web-frontend


【解决方案1】:

棘手的部分是逗号的使用。您可以看到悬停在标签上的效果。

我使用 flexbox 来显示标签并在 hover 上使用 row-reverse。请记住,项目的实际顺序不会改变,因此当悬停在屏幕上的最后一个孩子时,实际上是弹性框中的第一个孩子。

希望这会有所帮助。

.container {
  max-width: 400px;
  text-align: center;
}

.tags {
  display: flex;
  justify-content: center;
}

.tags:hover {
  flex-direction: row-reverse;
}

.tags:not(:hover)>span:not(:last-child)::after,
.tags:hover>span:not(:first-child)::after {
  content: ", ";
}

.tags:hover>span:last-child {
  display: none;
}
<div class="container">
  <div><img src="http://via.placeholder.com/350x150"></div>
  <div class="tags">
    <span>France</span><span>Paris</span><span>Things to do in Paris</span>
  </div>
</div>

【讨论】:

  • 不考虑逗号后的空格字符。也许这可以用一些margin-right 或固定宽度的空间来解决
【解决方案2】:

我敢这样做:

p {
  direction: rtl;
  color: transparent;
}

p a {
  display: inline-block;
}

p a:nth-child(3) {
  display: none;
}
<div class="post-meta vcard">
  <p>
    <a href="http://wordpress-114402-466332.cloudwaysapps.com/france/" rel="tag">France</a>,
    <a href="http://wordpress-114402-466332.cloudwaysapps.com/france/paris/" rel="tag">Paris</a>,
    <a href="http://wordpress-114402-466332.cloudwaysapps.com/france/paris/things-to-do/" rel="tag">Things to Do in Paris</a>
  </p>
</div>

这是codepen

【讨论】:

  • 我冒昧地从你的 Codepen 制作了一个 sn-p。您的解决方案存在一些问题,例如由于 RTL,文本现在右对齐,并且逗号未显示(尽管存在)
  • @JeremyThille 这是简单的代价,我的朋友 :)。现在他可以选择使用哪一个了。感谢您的编辑。
  • @jeremythrille,我在哪里可以看到你的版本?
【解决方案3】:
  1. 颠倒链接的顺序并使用 flexbox 将所有内容居中
  2. 提供所有文字font-size: 0 - 这会隐藏所有内容
  3. 提供链接以显示font-size: 1rem
  4. 将逗号重新引入为pseudoelement

p {
  display: flex;
  flex-direction: row-reverse;
  justify-content: center;
  font-size: 0;
}

a:not(:last-child) {
  font-size: 1rem;
}

/* reintroduce comma */
a:first-child:before {
  content: ',';
  margin-right: .5ch;
  float: left;
  color: initial;
}
<div class="post-meta vcard">
  <p>
    <a href="http://wordpress-114402-466332.cloudwaysapps.com/france/" rel="tag">France</a>,
    <a href="http://wordpress-114402-466332.cloudwaysapps.com/france/paris/" rel="tag">Paris</a>,
    <a href="http://wordpress-114402-466332.cloudwaysapps.com/france/paris/things-to-do/" rel="tag">Things to Do in Paris</a>
  </p>
</div>

【讨论】:

  • 谢谢索尔!碰巧,知道如何保留 1 个逗号,使其显示为“法国巴黎”吗?
  • 非常感谢!完美运行!我做了一个小改动来添加一个空格,并且能够稍微减少最后一部分的代码。它居中并在逗号后添加一个空格: .post-meta a:first-child:before { content: ',\00a0';空白:pre;颜色:初始}
【解决方案4】:

您需要了解这不是最好的方法。如果您采用这些黑客手段,您的网站将难以维护。

无论如何,这是我使用flexbox 的方法。它与其他答案类似,但是:

  1. 我不会更改 HTML(我假设你不能)。
  2. 我试图让它看起来,就好像你只是 HTML(而不是 hack)。

我只是将逗号隐藏在父 div 后面,所以如果你使用不同的字体大小,你需要调整它(或者只使用 em)。

/* Hide the comma */
.post-meta {
  overflow: hidden;
}

.post-meta p {
  display: flex;
  flex-direction: row-reverse; /* Reverse order of elements... */
  justify-content: flex-end; /* ...and move them to the left */
  position: relative;
  left: -3px; /* Move a notch to the left so we can hide the comma */
}

/* Make up for the removed space */
p a:nth-child(1) {
  margin-left: 3px;
}

/* Hide 'Things to Do' */
p a:nth-child(3) {
  display: none;
}
<div class="post-meta vcard"><p>
<a href="http://wordpress-114402-466332.cloudwaysapps.com/france/" rel="tag">France</a>, 
<a href="http://wordpress-114402-466332.cloudwaysapps.com/france/paris/" rel="tag">Paris</a>, 
<a href="http://wordpress-114402-466332.cloudwaysapps.com/france/paris/things-to-do/" rel="tag">Things to Do in Paris</a>
</p></div>

【讨论】:

  • 这很棒!我唯一的问题是居中,但不确定是否可能。目前,它被推到左边。当我将 justify-content 更改为“中间”时,逗号会重新出现。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-24
  • 1970-01-01
相关资源
最近更新 更多