首页 » 收集 » 正文内容
美化代码块教程
寻梦xunm| 814| 收集
3年前
超过1201天 温馨提示
本文最后更新于2021年06月07日,已超过1201天没有更新,若内容或图片失效,请留言反馈。

程序员的博客里绝对不会缺少的东西一定是代码块,那么怎么把代码块打扮的美美的呢?
火狐截图_2021-06-07T05-28-49.837Z.png
介于前两天有同学问我,我的代码块是怎么美化的,那么今天我就出个我美化代码块的教程,主要分为三个步骤,分割关键字、添加额外标签和自定义美化css
加载 js 分割代码关键字

使用任意代码块分割插件自动将代码关键字加上相应标签,我这是使用的是一款名叫 prism 的插件

<script src="//cdn.jsdelivr.net/gh/cetr/cdn@master/prism.min.js"></script>

动态添加代码块头部样式

用 js 动态对每篇文章里的代码块加上顶部三个彩色圆点,用于美化ui

if (!$('pre').hasClass('line-numbers')) {
    $('pre').addClass('line-numbers').before($(
        '<figcaption class="line-numbers-head">' +
        '<div class="custom-carbon">' +
        '<div class="custom-carbon-dot custom-carbon-dot--red"></div>' +
        '<div class="custom-carbon-dot custom-carbon-dot--yellow"></div>' +
        '<div class="custom-carbon-dot custom-carbon-dot--green"></div>' +
        '</div>' +
        '</figcaption>'
    ));
}

自定义 css 样式

自定义 css 给代码块及代码块里的文字添加样式

code[class*="language-"],
pre[class*="language-"] {
    color: black;
    background: none;
    text-shadow: 0 1px white;
    font-family: Consolas, Monaco, 'Andale Mono', 'Ubuntu Mono', monospace;
    font-size: 16px;
    text-align: left;
    white-space: pre;
    word-spacing: normal;
    word-break: normal;
    word-wrap: normal;
    line-height: 1.5;

    -moz-tab-size: 4;
    -o-tab-size: 4;
    tab-size: 4;

    -webkit-hyphens: none;
    -moz-hyphens: none;
    -ms-hyphens: none;
    hyphens: none;
}

pre[class*="language-"]::-moz-selection, pre[class*="language-"] ::-moz-selection,
code[class*="language-"]::-moz-selection, code[class*="language-"] ::-moz-selection {
    text-shadow: none;
}

pre[class*="language-"]::selection, pre[class*="language-"] ::selection,
code[class*="language-"]::selection, code[class*="language-"] ::selection {
    text-shadow: none;
}

@media print {
    code[class*="language-"],
    pre[class*="language-"] {
        text-shadow: none;
    }
}

/* Code blocks */
pre[class*="language-"] {
    padding: 1em;
    margin-bottom: 20px;
    overflow: auto;
}

:not(pre) > code[class*="language-"],
pre[class*="language-"] {
    background: #f5f2f0;
}

:not(pre) > code[class*="language-"] {
    padding: .1em;
    border-radius: .3em;
    white-space: normal;
}

.token.comment,
.token.prolog,
.token.doctype,
.token.cdata {
    color: #90a4ae;
}

.token.punctuation,
.token.attr-name {
    color: #757575;
}

.token.namespace {
    opacity: .7;
}

.token.property,
.token.tag,
.token.boolean,
.token.number,
.token.constant,
.token.symbol,
.token.deleted {
    color: #f76d47;
}

.token.selector,
.token.char,
.token.builtin,
.token.inserted {
    color: #be4dbc;
}

.token.keyword {
    color: #39adb5;
}

.token.operator,
.token.entity,
.token.url,
.language-css .token.string,
.style .token.string {
    color: #9a6e3a;
}

.token.atrule,
.token.string,
.token.attr-value {
    color: #4dc14c;
}

.token.function,
.token.class-name {
    color: #6182b8;
}

.token.regex,
.token.important,
.token.variable {
    color: #eab700;
}

.token.parameter {
    color: #ee8019;
}

.token.important,
.token.bold {
    font-weight: bold;
}

.token.italic {
    font-style: italic;
}

.token.entity {
    cursor: help;
}

figcaption.line-numbers-head {
    width: 100%;
    color: #8d949e;
    background-color: #f5f6f7;
    zoom: 1;
    border-top-left-radius: 0.25rem;
    border-top-right-radius: 0.25rem;
}

figcaption.line-numbers-head .custom-carbon-dot {
    display: inline-block;
    margin: 0 4px;
    border-radius: 50%;
    width: 10px;
    height: 10px;
}

figcaption.line-numbers-head .custom-carbon-dot--red {
    background-color: #ff5f56;
}

figcaption.line-numbers-head .custom-carbon-dot--yellow {
    background-color: #ffbd2e;
}

figcaption.line-numbers-head .custom-carbon-dot--green {
    background-color: #27c93f;
}

figcaption.line-numbers-head .custom-carbon {
    padding: 2px 0 0 9px;
}

pre.line-numbers {
    margin-top: 0;
    padding: 10px 1em 1em 1em;
    background-color: #f5f6f7;
    border-bottom-left-radius: 0.25rem;
    border-bottom-right-radius: 0.25rem;
}

文章来源于:https://coor.top/archives/21013012

0 赞 or 打赏
喜欢就打赏一点
微信 支付宝
隐私
Q Q:1340326824
邮箱:vipshiyi@qq.com
QQ群:422720328

我的音乐

微博客-专为自己编写开发的源码