
EnlighterJS——WordPress代码高亮插件的安装与自定义CSS(美化)
如果要在WordPress博客中放置大块的代码的话,最佳的解决方案是使用代码高亮插件。因为代码高亮插件不仅能根据语言来对代码进行高亮显示,而且可以提供生肉显示、复制、小窗打开等功能,这是HTML中标签无法比拟的。EnlighterJS是一款WordPress上的代码高亮插件,它可以直接在WordPress插件页面中安装,是WordPress上目前功能较为齐全的代码高亮插件,称得上是代码高亮界的“瑞士军刀”。
但是EnlighterJS在它的设置中仅提供了几款主题,风格都比较老旧。借助WordPress的自定义CSS功能,我们可以在EnlighterJS的主题上额外施加CSS代码,达到美化的效果。本人参考了当今网络上流行的对EnlighterJS的美化方案,并且进行了进一步的优化与增强,包括使用浮动布局重写,调整间距,增加阴影等,效果参考下方代码块。
请打开WordPress仪表盘,点击Enlighter设置,然后在主题(Theme)一栏选择莫奈彩(Monokai)。我们将基于这个主题进行美化。
然后,打开WordPress仪表盘,点击外观中的自定义选项,选择额外CSS,加入以下代码:
.enlighter-t-monokai .enlighter-toolbar {
top: 6px;
}
.enlighter-btn {
border-radius: 8px;
}
.enlighter::-webkit-scrollbar {
width: 8px;
height: 6px;
}
.enlighter::-webkit-scrollbar-thumb {
background-color: #1e90ff;
background-image: -webkit-linear-gradient(
45deg,
rgba(50, 205, 50) 25%,
transparent 25%,
transparent 50%,
rgba(50, 205, 50) 50%,
rgba(50, 205, 50) 75%,
transparent 75%,
transparent
);
}
.enlighter::-webkit-scrollbar-track-piece {
background: #444;
}
.enlighter-default {
border-radius: 15px;
padding-top: 34px !important;
margin-top: 30px;
margin-bottom: 30px !important;
background: #333;
box-shadow: 2px 2px 12px #444;
}
.enlighter-default .enlighter {
font-family: "JetBrains Mono", "Consolas", monospace, "Open Sans Pro",
"Courier New", "Arial";
overflow: auto;
display: block;
background: #444;
border-radius: 15px
}
.enlighter-default::after {
content: " ";
position: absolute;
-webkit-border-radius: 50%;
border-radius: 40%;
background: #c7c7c7;
width: 12px;
height: 12px;
top: 0;
left: 15px;
margin-top: 11px;
-webkit-box-shadow: 20px 0 #c7c7c7, 40px 0 #c7c7c7;
box-shadow: 20px 0 #c7c7c7, 40px 0 #c7c7c7;
transition-duration: 0.3s;
}
.enlighter-default:hover::after {
background: #fc625d;
-webkit-box-shadow: 20px 0 #fdbc40, 40px 0 #35cd4b;
box-shadow: 20px 0 #fdbc40, 40px 0 #35cd4b;
}
.enlighter-t-monokai.enlighter-hover div.enlighter > div:hover {
background-color: #607d8b;
}
/* All line numbers */
.enlighter-t-monokai.enlighter-linenumbers div.enlighter > div::before {
color: #ccc;
background: #555;
position: absolute;
}
.enlighter-default.enlighter-hover div.enlighter > div:hover:before {
color: #fff;
}
/* 代码整行,连同行号 容器 */
.enlighter-t-monokai div.enlighter > div {
padding: 0;
line-height: 1.7em;
display: block;
position: relative;
}
.enlighter-t-monokai div.enlighter > div:after {
content: "";
/* 清除浮动 */
display: block;
height: 0;
clear: both;
visibility: hidden;
}
/* Only Code */
div.enlighter > div > div {
display: block;
width: calc(100% - 60px);
margin: 0;
padding: 0;
float: right;
}
/* 调整最上、最下一行的行高 */
.enlighter-t-monokai div.enlighter > div:first-child,
.enlighter-t-monokai div.enlighter > div:last-child {
padding-top: 0;
padding-bottom: 0;
}
/* First line container */
.enlighter-t-monokai div.enlighter > div:first-child {
padding-top: 5px;
}
/* First line code */
.enlighter-t-monokai div.enlighter > div:first-child > * {
}
/* Firse line pseudo class */
.enlighter-t-monokai div.enlighter > div:first-child::before {
top: 0;
padding-top: 5px;
}
/* Last line container */
.enlighter-t-monokai div.enlighter > div:last-child {
padding-bottom: 5px;
}
/* Last line code */
.enlighter-t-monokai div.enlighter > div:last-child > * {
}
/* Last line pseudo class */
.enlighter-t-monokai div.enlighter > div:last-child::before {
}
/* All pseudo class */
.enlighter-t-monokai div.enlighter > div::before {
height: 100%;
}
您不必在意是否要去除空格、缩进和注释,WordPress会帮您自动压缩这些CSS。
请注意,这些代码在EnlighterJS 4.6.1
的版本中测试无虞。
本文是原创文章,采用 CC BY-NC-ND 4.0 协议,完整转载请注明来自 ipangbo.cn
评论
匿名评论
隐私政策
你无需删除空行,直接评论以获取最佳展示效果