博客园虽然可定制度高,可是在代码高亮这一块作的并非很好,默认的配色方案看着很不舒服,并且影响页面的总体协调感。css
但正由于博客园的可定制度高,因此咱们能够自定义代码高亮的风格。编辑器
使用纯 CSS 实现,不须要 JS 权限。测试
本章节只针对博客园的 TinyMce 编辑器讨论,我没有测试其余编辑器(例如 Markdown)的代码高亮解析方案。spa
博客园的插入代码是有两种方案的。3d
如图所示。code
两种插入代码使用的高亮解析方案不一样,各有利弊。我对两种方案都作了适配。blog
我我的推荐使用第一种高亮方案。get
若是想直接取用代码,请跳转到最后一章。博客
对于代码高亮比较细致,可是复制须要按复制按钮,复制界面和按钮比较丑,格式也不舒服。string
若是默认折叠还会有很丑的展开和收缩图标。
这个高亮直接将 CSS 写在了 Style 里面,我无可奈何使用 [style="color:xxx;"] 选择器来匹配。
插入大量代码而且须要读者复制时使用这个,若是须要在手机上阅读,使用这个。
代码高亮不怎么细致(例如数字就没有高亮),在手机上行数栏目会错位,可是总体比较精致。
元素比较多,并且是动态渲染的,代码长的话很占渲染运存。
代码量较少时使用这个,在电脑上阅读使用这个,若是你的博客读者是在手机看的就不要用这个了。
这是我目前在使用的高亮。
下面的 CSS 适配了两种插入方式,将下面的代码粘贴到 “博客后台-设置-页面定制CSS代码”中便可。
1 /*代码高亮*/ 2 .syntaxhighlighter .bold { 3 font-weight:unset !important; 4 } 5 .syntaxhighlighter .line { 6 background-color: rgb(40, 43, 46)!important; 7 } 8 .syntaxhighlighter .line.alt2 { 9 background-color: rgb(40, 43, 46)!important; 10 } 11 .syntaxhighlighter .line.alt1 { 12 background-color: rgb(40, 43, 46)!important; 13 } 14 .syntaxhighlighter .comments, .syntaxhighlighter .comments a { 15 color: rgb(129, 142, 150)!important; 16 } 17 .syntaxhighlighter .keyword { 18 color: rgb(147, 199, 99)!important; 19 font-weight: unset !important; 20 } 21 .syntaxhighlighter .preprocessor { 22 color: rgb(85, 113, 130) !important; 23 } 24 .syntaxhighlighter .plain, .syntaxhighlighter .plain a { 25 color: rgb(224, 226, 228)!important; 26 } 27 .syntaxhighlighter .color1, .syntaxhighlighter .color1 a { 28 color: rgb(147, 199, 99)!important; 29 } 30 .syntaxhighlighter .string, .syntaxhighlighter .string a { 31 color: rgb(236, 118, 0)!important; 32 } 33 .syntaxhighlighter .functions { 34 color: rgb(140, 187, 173)!important; 35 } 36 .syntaxhighlighter .gutter .line { 37 border-right: 2px solid rgb(147, 199, 99)!important; 38 } 39 .syntaxhighlighter.collapsed .toolbar { 40 background: rgb(40, 43, 46)!important; 41 border: none !important; 42 border-radius: 5px !important; 43 } 44 .syntaxhighlighter.collapsed .toolbar a { 45 color: rgb(147, 199, 99)!important; 46 } 47 .syntaxhighlighter.collapsed .toolbar a:hover { 48 color: rgb(78, 109, 48)!important; 49 } 50 .syntaxhighlighter { 51 border-radius: 5px; 52 } 53 /*第一种高亮结束,第二种高亮开始*/ 54 .cnblogs_code div { 55 background: #282b2e; 56 } 57 .cnblogs_code { 58 background: #282b2e; 59 border-radius: 5px; 60 border: none; 61 font-family: consolas !important; 62 color: #fff; 63 } 64 .cnblogs_code_toolbar { 65 background: #282b2e !important; 66 } 67 .cnblogs_code_toolbar a:hover, .cnblogs_code_toolbar a:link, .cnblogs_code_toolbar a:visited, .cnblogs_code_toolbar a:active, .cnblogs_code_toolbar a:link img, .cnblogs_code_toolbar a:visited img { 68 background-color: #282b2e !important; 69 border: none!important; 70 } 71 .cnblogs_code pre { 72 font-family: consolas !important; 73 padding-left: 3px; 74 color: rgb(224, 226, 228); 75 } 76 .cnblogs_code span[style="color: #000000;"] , .cnblogs_code span[style="color: #ff0000;"]{ 77 color: rgb(224, 226, 228) !important; 78 } 79 .cnblogs_code span[style="color: #0000ff;"]{ 80 color: rgb(147, 199, 99)!important; 81 } 82 .cnblogs_code span[style="color: #800080;"]{ 83 color: #ffd740 !important; 84 } 85 .cnblogs_code span[style="color: #800000;"]{ 86 color: rgb(236, 118, 0)!important; 87 } 88 .cnblogs_code span[style="color: #008000;"]{ 89 color: rgb(129, 142, 150)!important; 90 } 91 .cnblogs_code span[style="color: #008080;"]{ 92 color: #afafaf!important; 93 margin-right: 5px; 94 } 95 .cnblogs_code_collapse { 96 border: none; 97 background: #282b2e; 98 color: rgb(147, 199, 99); 99 } 100 .cnblogs_code > pre { 101 border: none !important; 102 } 103 .cnblogs_code > textarea { 104 color: #fff; 105 background: transparent; 106 border: none; 107 outline: none; 108 } 109 /*代码高亮结束*/
欢迎在评论区提出建议。