You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何调整rmarkdown/bookdown生成的HTML网页的页面左边距

解决方案

以下是面向新手、可以直接复制使用的修改方案,不需要额外学习CSS知识,调整参数就能自定义边距:


操作步骤

  1. 先修正原有代码的YAML缩进错误,否则目录、编号等配置会失效
  2. 在头部YAML结束后的位置直接插入样式代码,即可控制页面全局边距
  3. 优化双栏布局的适配逻辑,避免拉伸页面时右侧内容贴边

修改后完整可运行代码

---
title: "Chapter 3 of ISLR"
author: "Me"
date: "`r Sys.Date()`"
output: 
  bookdown::html_document2:
    number_sections: false
    toc: true
---

<!-- 下面这段代码控制全局边距,直接改px前面的数值就能调整边距宽度 -->
<style>
body {
  margin-left: 30px !important;
  margin-right: 30px !important;
}
</style>

# Libraries
<!-- 双栏布局改成了响应式适配,不会因为页面拉伸出现内容溢出 -->
:::: {style="display: grid; grid-template-columns: 1fr 1fr; grid-column-gap: 20px;"}

::: {}
original version of code

```{r libraries-book}
library(MASS)
library(ISLR2)

:::

::: {}
tidymodels version of code

library(MASS) 
library(ISLR)
library(tidymodels)

:::

::::

---

## 自定义调整说明
*如果需要缩小左侧边距,只要把`<style>`块里`margin-left`对应的30px改成更小的数值即可,比如改成15px,边距就会缩窄一半;同理调整`margin-right`的数值可以修改右侧边距宽度*

内容的提问来源于stack exchange,提问作者itsMeInMiami
相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 13:15:04