给文章底部的标签换个彩色药丸样式:红橙绿蓝紫五色自动循环,鼠标悬停变深灰。纯 CSS,不用改主题模板文件。下面每块都能点「预览效果」直接看,复制走改个类名就能用。
原版 · 五色循环
HTML彩色标签 · 五色循环原版
<style>
/** 文章底部彩色标签CSS */
.post-tags{margin-bottom: 10px}
.post-tags a{padding: 4px 10px;background-color: #19B5FE;color: white;font-size: 12px;font-weight:bold;line-height: 16px;font-weight: 400;margin: 0 5px 5px 0;border-radius: 10px;display: inline-block}
.post-tags a:nth-child(5n){background-color: #6022c3;color: #FFF}
.post-tags a:nth-child(5n+1){background-color: #ff5e5c;color: #FFF}
.post-tags a:nth-child(5n+2){background-color: #ffbb50;color: #FFF}
.post-tags a:nth-child(5n+3){background-color: #1ac756;color: #FFF}
.post-tags a:nth-child(5n+4){background-color: #19B5FE;color: #FFF}
.post-tags a:hover{background-color: #1B1B1B;color: #FFF}
</style>
<div class="post-tags">
<a href="#">WordPress</a><a href="#">CSS</a><a href="#">美化</a><a href="#">标签</a><a href="#">前端</a><a href="#">配色</a>
</div>原版有三处小毛病,实测确认过:font-weight:bold 写完后紧接着又写了 font-weight: 400,后者把前者覆盖了,实际字重是 400 不是粗体;没写 text-decoration:none,标签会带下划线;hover 没有过渡,颜色是瞬变的。
整理版 · 修掉上面三个问题
HTML彩色标签 · 整理版
<style>
/** 文章底部彩色标签CSS · 整理版 */
.post-tags{margin-bottom:10px}
.post-tags a{padding:4px 10px;background-color:#19B5FE;color:#fff;font-size:12px;font-weight:700;line-height:16px;margin:0 5px 5px 0;border-radius:10px;display:inline-block;text-decoration:none;transition:background-color .22s, transform .22s}
.post-tags a:nth-child(5n){background-color:#6022c3}
.post-tags a:nth-child(5n+1){background-color:#ff5e5c}
.post-tags a:nth-child(5n+2){background-color:#ffbb50}
.post-tags a:nth-child(5n+3){background-color:#1ac756}
.post-tags a:nth-child(5n+4){background-color:#19B5FE}
.post-tags a:hover{background-color:#1B1B1B;color:#fff;transform:translateY(-2px)}
</style>
<div class="post-tags">
<a href="#">WordPress</a><a href="#">CSS</a><a href="#">美化</a><a href="#">标签</a><a href="#">前端</a><a href="#">配色</a>
</div>顺手去掉了每条里重复的 color:#FFF(继承基础样式即可),hover 加了一点上浮,鼠标划过更明显。
清淡描边款 · 适合极简主题
底色换成透明,只留彩色边框和彩色文字,hover 时反色填充。浅色背景或扁平化主题上比实心款更协调。
HTML彩色标签 · 清淡描边款
<style>
/** 彩色标签 · 清淡描边款(适合极简/浅色主题) */
.post-tags{margin-bottom:12px}
.post-tags a{padding:4px 11px;background-color:transparent;color:#5b6480;font-size:12px;font-weight:500;line-height:16px;margin:0 6px 6px 0;border-radius:999px;border:1px solid currentColor;display:inline-block;text-decoration:none;transition:all .22s}
.post-tags a:nth-child(5n){color:#6022c3}
.post-tags a:nth-child(5n+1){color:#ff5e5c}
.post-tags a:nth-child(5n+2){color:#e0912a}
.post-tags a:nth-child(5n+3){color:#1ac756}
.post-tags a:nth-child(5n+4){color:#19B5FE}
.post-tags a:hover{color:#fff;transform:translateY(-2px)}
.post-tags a:nth-child(5n):hover{background-color:#6022c3}
.post-tags a:nth-child(5n+1):hover{background-color:#ff5e5c}
.post-tags a:nth-child(5n+2):hover{background-color:#e0912a}
.post-tags a:nth-child(5n+3):hover{background-color:#1ac756}
.post-tags a:nth-child(5n+4):hover{background-color:#19B5FE}
</style>
<div class="post-tags">
<a href="#">WordPress</a><a href="#">CSS</a><a href="#">美化</a><a href="#">标签</a><a href="#">前端</a><a href="#">配色</a>
</div>改的时候注意这几点
- 类名要对上你的主题:代码里写的是
.post-tags,实际常见的还有.entry-tags、.tag-links、.postmeta。在前台右键检查标签,看外层class叫什么,把代码里的类名换掉。 nth-child数的是容器里所有子元素,不只是 a 标签。如果容器里还包了个「标签:」之类的span,颜色就会整体错位。要么保证容器里只有 a,要么改成nth-of-type。- 换配色改 5 个色值:
5n+1到5n+4加5n各对应一个色,顺序就是红橙绿蓝紫。想少几色就删掉对应的行。 - 想更圆:
border-radius:10px改成999px就是全圆角药丸;改成3px是方角。 - 去掉上浮效果:删掉
transition里的transform .22s和 hover 里的transform:translateY(-2px)。
怎么用
复制代码块 → WordPress 后台「外观 → 自定义 → 额外 CSS」粘贴(推荐,不用动主题文件);或者放进子主题的 style.css。
只加 CSS 不改结构的话,确保你主题的标签本身是 <a> 标签,且外层有上面那个类名。如果主题压根没有标签容器,就按代码块里的 HTML 结构手动加。
代码仅供学习交流,禁止商用。更多网站美化代码,前往 维形筑网 www.wxzcw.com。
作者:Flechazo
如若转载,请注明出处:维形筑网 https://wxzcw.com