惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
V
Visual Studio Blog
P
Privacy International News Feed
月光博客
月光博客
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
L
Lohrmann on Cybersecurity
N
News and Events Feed by Topic
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Apple Machine Learning Research
Apple Machine Learning Research
阮一峰的网络日志
阮一峰的网络日志
Webroot Blog
Webroot Blog
T
Threatpost
宝玉的分享
宝玉的分享
The Last Watchdog
The Last Watchdog
小众软件
小众软件
L
LINUX DO - 最新话题
C
Cisco Blogs
T
Troy Hunt's Blog
Schneier on Security
Schneier on Security
酷 壳 – CoolShell
酷 壳 – CoolShell
www.infosecurity-magazine.com
www.infosecurity-magazine.com
雷峰网
雷峰网
G
GRAHAM CLULEY
有赞技术团队
有赞技术团队
Know Your Adversary
Know Your Adversary
博客园 - 叶小钗
罗磊的独立博客
V
V2EX
博客园 - Franky
P
Proofpoint News Feed
SecWiki News
SecWiki News
腾讯CDC
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Jina AI
Jina AI
博客园 - 三生石上(FineUI控件)
S
Secure Thoughts
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Google DeepMind News
Google DeepMind News
Attack and Defense Labs
Attack and Defense Labs
人人都是产品经理
人人都是产品经理
The Cloudflare Blog
PCI Perspectives
PCI Perspectives
V2EX - 技术
V2EX - 技术
Google DeepMind News
Google DeepMind News
Last Week in AI
Last Week in AI
aimingoo的专栏
aimingoo的专栏
Cisco Talos Blog
Cisco Talos Blog
N
News and Events Feed by Topic
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
S
SegmentFault 最新的问题

小记 – QingZhao 青找生活

今年辞掉了稳定的工作 | QingZhao 青找生活 新版本任务计划管理来啦 | QingZhao 青找生活 2025年,我与九岁的“合伙人”的一年 | QingZhao 青找生活 家庭成长伴侣 | QingZhao 青找生活 对目前的页面留一个档 | QingZhao 青找生活 置顶记录:千万不要忘记啦! | QingZhao 青找生活 WordPress美化 评论输入打字礼花及震动特效 | QingZhao 青找生活 添加站点统计(小工具版) | QingZhao 青找生活 添加友链自动获取RSS(小工具版) | QingZhao 青找生活 给博客添加随机一言(小工具版) | QingZhao 青找生活
给博文代码块添加高度限制 | QingZhao 青找生活
By qingzhao · 2025-12-04 · via 小记 – QingZhao 青找生活

为代码块设置固定高度并允许滚动

方法一:使用自定义CSS(推荐)

1. 添加自定义CSS

在主题自定义器或子主题的style.css中添加:

/* 为所有代码块设置固定高度 */
.wp-block-code pre,
pre.wp-block-code {
    max-height: 300px; /* 设置最大高度 */
    overflow-y: auto; /* 垂直方向超出时显示滚动条 */
    overflow-x: auto; /* 水平方向超出时显示滚动条 */
    padding: 15px;
    background-color: #f5f5f5;
    border-radius: 5px;
    line-height: 1.5;
}

/* 或者只为特定的代码块设置 */
.code-scrollable pre {
    max-height: 300px;
    overflow-y: auto;
    overflow-x: auto;
}

/* 优化滚动条样式(可选) */
.wp-block-code pre::-webkit-scrollbar {
    width: 8px;
    height: 8px;
}

.wp-block-code pre::-webkit-scrollbar-thumb {
    background: #ccc;
    border-radius: 4px;
}

.wp-block-code pre::-webkit-scrollbar-track {
    background: #f1f1f1;
}

2. 针对特定高度的设置

/* 小代码块 */
.small-code pre {
    max-height: 200px;
    overflow: auto;
}

/* 中代码块 */
.medium-code pre {
    max-height: 400px;
    overflow: auto;
}

/* 大代码块 */
.large-code pre {
    max-height: 600px;
    overflow: auto;
}

方法二:使用插件

1. Simple Custom CSS插件

安装后直接在插件中添加CSS代码。

2. Code Snippets插件

创建新的代码片段,选择只在前端加载:

add_action('wp_head', function() {
    echo '<style>
        .wp-block-code pre {
            max-height: 300px;
            overflow: auto;
            padding: 15px;
        }
    </style>';
});

方法三:使用主题的functions.php

// 添加自定义CSS
function custom_code_block_styles() {
    echo '<style>
        /* 固定高度并允许滚动 */
        .wp-block-code pre {
            max-height: 300px;
            overflow-y: auto;
            overflow-x: auto;
            padding: 1em;
            background: #f5f5f5;
            border: 1px solid #ddd;
            border-radius: 4px;
        }
        
        /* 调整行号和代码显示 */
        .wp-block-code code {
            display: block;
            white-space: pre;
        }
    </style>';
}
add_action('wp_head', 'custom_code_block_styles');

方法四:只针对特定页面/帖子

/* 通过页面ID或类名指定 */
.page-id-123 .wp-block-code pre,
.post-id-456 .wp-block-code pre {
    max-height: 400px;
    overflow: auto;
}

/* 或使用文章类型 */
.single-post .wp-block-code pre {
    max-height: 350px;
    overflow: auto;
}

最佳实践建议:

  1. 响应式设计考虑
.wp-block-code pre {
    max-height: 300px;
    overflow: auto;
}

@media (max-width: 768px) {
    .wp-block-code pre {
        max-height: 250px;
    }
}

@media (max-width: 480px) {
    .wp-block-code pre {
        max-height: 200px;
    }
}
  1. 添加滚动提示
.wp-block-code {
    position: relative;
}

.wp-block-code::after {
    content: "↕ 可滚动";
    position: absolute;
    top: 5px;
    right: 10px;
    font-size: 12px;
    color: #999;
    opacity: 0.7;
}.wp-block-code {
    position: relative;
}

.wp-block-code::after {
    content: "↕ 可滚动";
    position: absolute;
    top: 5px;
    right: 10px;
    font-size: 12px;
    color: #999;
    opacity: 0.7;
}
  1. 考虑代码高亮插件
    如果你使用了SyntaxHighlighter或Prism.js等插件,可能需要调整选择器:
/* 针对SyntaxHighlighter */
.syntaxhighlighter {
    max-height: 300px !important;
    overflow-y: auto !important;
}

/* 针对Prism.js */
pre[class*="language-"] {
    max-height: 300px;
    overflow: auto;
}

一般来说方法一(自定义CSS)是最简单直接的解决方案。