去提问»
提问背景 Question:
帮我看看,为什么 {{.Article.Question}} 前面加了空格,可能是css输出原因,我如何去掉空格,但是vscode编辑器点保存就缩回了。怎么办 以下是代码。 <!doctype html> <html lang="zh-CN"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>{{.Article.Title}} - AI 百科 - bbsgood.com</title> <style> /* ================= 现代系统变量与重置 ================= */ :root { --primary: #4f46e5; --primary-hover: #4338ca; --primary-light: #e0e7ff; --bg-canvas: #f8fafc; --bg-card: #ffffff; --border-color: #e2e8f0; --text-main: #334155; --text-dark: #0f172a; --text-muted: #64748b; --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.05), 0 1px 2px rgba(0, 0, 0, 0.02); --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.05), 0 2px 4px -1px rgba(0, 0, 0, 0.03); } * { box-sizing: border-box; margin: 0; padding: 0; } body { font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif; background-color: var(--bg-canvas); color: var(--text-main); line-height: 1.6; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; } a { color: inherit; text-decoration: none; transition: all 0.2s ease; } /* ================= 头部导航 (毛玻璃) ================= */ header { position: sticky; top: 0; z-index: 50; background: rgba(255, 255, 255, 0.8); backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px); border-b: 1px solid rgba(226, 232, 240, 0.8); box-shadow: var(--shadow-sm); } .header-container { max-width: 52rem; margin: 0 auto; padding: 0 1.25rem; height: 4rem; display: flex; align-items: center; justify-content: space-between; } .logo-area { display: flex; align-items: center; gap: 0.5rem; font-size: 1.15rem; font-weight: 800; background: linear-gradient(to right, #4f46e5, #7c3aed); -webkit-background-clip: text; -webkit-text-fill-color: transparent; } .back-btn { font-size: 0.875rem; color: var(--primary); font-weight: 500; display: flex; align-items: center; gap: 0.25rem; } .back-btn:hover { color: var(--primary-hover); text-decoration: underline; } /* ================= 主体内容布局 ================= */ main { max-width: 52rem; margin: 2rem auto 4rem auto; padding: 0 1.25rem; } article { background-color: var(--bg-card); border: 1px solid var(--border-color); border-radius: 1.25rem; padding: 2rem; box-shadow: var(--shadow-sm); } @media (min-width: 768px) { article { padding: 3rem; } } /* 面包屑 */ .breadcrumbs { font-size: 0.75rem; color: var(--text-muted); margin-bottom: 1.5rem; display: flex; align-items: center; gap: 0.5rem; } .breadcrumbs a:hover { color: var(--primary); text-decoration: underline; } /* ================= 提问背景模块 (折叠卡片) ================= */ .question-card { background-color: #f8fafc; border-left: 4px solid var(--primary); padding: 1.25rem; border-top-right-radius: 0.75rem; border-bottom-right-radius: 0.75rem; margin-bottom: 2rem; box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.02); } .question-title-badge { font-size: 0.75rem; font-weight: 700; color: var(--primary); text-transform: uppercase; letter-spacing: 0.05em; margin-bottom: 0.5rem; } /* 限制容器 */ .content-outer-wrapper { max-height: 160px; overflow: hidden; transition: max-height 0.4s cubic-bezier(0.4, 0, 0.2, 1); } .content-outer-wrapper.expanded { max-height: none; } .msg-content-body { white-space: pre-wrap; word-break: break-all; color: #475569; font-size: 0.875rem; line-height: 1.625; } @media (min-width: 768px) { .msg-content-body { font-size: 0.9375rem; } } /* 展开折叠按钮 */ .toggle-btn { display: none; /* 默认不显示,由JS高度判定后决定 */ margin: 1rem auto 0 auto; padding: 0.375rem 1rem; background-color: #ffffff; border: 1px solid #cbd5e1; border-radius: 9999px; color: var(--primary); font-size: 0.8125rem; font-weight: 600; cursor: pointer; box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05); transition: all 0.2s ease; } .toggle-btn:hover { background-color: #f1f5f9; border-color: #94a3b8; transform: translateY(-1px); } /* ================= 文章大标题及元数据 ================= */ .article-title { font-size: 1.5rem; font-weight: 800; color: var(--text-dark); line-height: 1.3; margin-bottom: 1rem; } @media (min-width: 768px) { .article-title { font-size: 2.25rem; } } .meta-bar { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; border-bottom: 1px solid var(--border-color); padding-bottom: 1rem; margin-bottom: 2rem; font-size: 0.75rem; color: var(--text-muted); gap: 0.75rem; } .meta-left { display: flex; gap: 1rem; } /* ================= 核心正文富文本排版 (Prose) ================= */ .prose { color: var(--text-main); font-size: 0.9375rem; line-height: 1.75; } @media (min-width: 768px) { .prose { font-size: 1.0625rem; } } .prose p { margin-bottom: 1.5rem; } .prose h1, .prose h2, .prose h3, .prose h4 { color: var(--text-dark); font-weight: 700; margin-top: 2rem; margin-bottom: 0.75rem; line-height: 1.4; } .prose h2 { font-size: 1.4rem; border-bottom: 1px solid var(--border-color); padding-bottom: 0.375rem; } .prose code { background-color: #f1f5f9; color: #ef4444; padding: 0.2rem 0.4rem; border-radius: 0.25rem; font-size: 0.85em; font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace; } .prose pre { background-color: #0f172a; color: #f8fafc; padding: 1.25rem; border-radius: 0.75rem; overflow-x: auto; margin-bottom: 1.5rem; font-size: 0.875rem; line-height: 1.5; } .prose pre code { background-color: transparent; color: inherit; padding: 0; font-size: inherit; } .prose blockquote { border-left: 4px solid #cbd5e1; padding-left: 1rem; color: var(--text-muted); font-style: italic; margin: 1.5rem 0; } .prose img { max-width: 100%; border-radius: 0.5rem; height: auto; margin: 1.5rem 0; } /* ================= 标签模块 ================= */ .tags-section { margin-top: 2.5rem; padding-top: 1.5rem; border-top: 1px solid var(--border-color); } .tags-label { font-size: 0.75rem; color: var(--text-muted); margin-bottom: 0.5rem; font-weight: 600; } .tags-list { display: flex; flex-wrap: wrap; gap: 0.5rem; } .tag-badge { font-size: 0.75rem; background-color: #f1f5f9; color: #475569; padding: 0.375rem 0.75rem; border-radius: 9999px; font-weight: 500; } .tag-badge:hover { background-color: var(--primary-light); color: var(--primary); transform: translateY(-1px); } /* ================= 页脚 ================= */ footer { background-color: #ffffff; border-top: 1px solid var(--border-color); padding: 2rem 0; text-align: center; font-size: 0.75rem; color: var(--text-muted); } </style> <script> function toggleContent() { const outer = document.getElementById("content-outer"); const btn = document.getElementById("toggle-btn"); const anchor = document.getElementById("question-top"); const isExpanded = outer.classList.contains("expanded"); if (isExpanded) { // 收起 outer.classList.remove("expanded"); btn.textContent = "展开全部"; anchor.scrollIntoView({ behavior: "smooth", block: "start" }); } else { // 展开 outer.classList.add("expanded"); btn.textContent = "收起内容"; } } document.addEventListener("DOMContentLoaded", () => { const outer = document.getElementById("content-outer"); const body = document.getElementById("content-body"); const btn = document.getElementById("toggle-btn"); // 检查内容实际高度是否超过阀值限制(160px) if (body && body.scrollHeight > 160) { btn.style.display = "block"; } else if (outer) { outer.style.maxHeight = "none"; if (btn) btn.style.display = "none"; } }); </script> </head> <body> <!-- 顶部导航栏 --> <header> <div class="header-container"> <a href="/baike" class="logo-area"> <span>🧠</span> <span>AI 智库百科</span> </a> <a href="/baike" class="back-btn"> &larr; 返回首页 </a> </div> </header> <!-- 主体区域 --> <main> <article> <!-- 面包屑导航 --> <div class="breadcrumbs"> <a href="/baike">百科首页</a> <span>/</span> <span style="color: var(--text-dark); font-weight: 500" >文章正文</span > </div> <!-- 原始提问锚点与模块 --> <div id="question-top" style="scroll-margin-top: 5.5rem"></div> <div class="question-card"> <div class="question-title-badge">提问背景 Question:</div> <!-- 内容包裹层 --> <div id="content-outer" class="content-outer-wrapper"> <div id="content-body" class="msg-content-body"> {{.Article.Question}} </div> </div> <!-- 展开/收起按钮:默认隐藏,由JS动态决定是否启用 --> <button id="toggle-btn" class="toggle-btn" onclick="toggleContent()"> 展开全部 </button> </div> <!-- 文章大标题 --> <h1 class="article-title">{{.Article.Title}}</h1> <!-- 信息元数据条 --> <div class="meta-bar"> <div class="meta-left"> <span>发布时间:{{.Article.CreatedAt}}</span> <span>👁️ 阅读:{{.Article.ClickCount}}</span> <span>👍 有用:{{.Article.LikeCount}}</span> </div> </div> <!-- 核心正文 (富文本) --> <div class="prose">{{.Article.Content}}</div> <!-- 文章关联标签 --> {{if .Tags}} <div class="tags-section"> <div class="tags-label">相关标签:</div> <div class="tags-list"> {{range .Tags}} <a href="/baike?tag={{.Name}}" class="tag-badge"> # {{.Name}} </a> {{end}} </div> </div> {{end}} </article> </main> <!-- 全站页脚 --> <footer> <p> &copy; 2024 bbsgood.com All Rights Reserved. 沉淀高价值AI时代知识百科 </p> </footer> </body> </html>

解决 Go 模板 pre-wrap 渲染多余空格问题

发布时间:2026-08-19 18:11 👁️ 阅读:1

为什么会出现空格?

问题的原因在于你的 CSS 里为 .msg-content-body 设置了:

.msg-content-body {
  white-space: pre-wrap; /* 保护所有空格和换行 */
  ...
}

而在 HTML 中,你的代码被 VS Code 格式化成了这样:

<div id="content-body" class="msg-content-body">
  {{.Article.Question}}
</div>

由于 white-space: pre-wrap 的存在,浏览器在渲染时会严格保留 <div> 标签和 {{.Article.Question}} 之间的换行符和缩进空格。这就是为什么保存代码后,页面输出内容开头会多出一段空白。


解决方案

你有两种最完美的解决办法:

方法一:使用 Go 模板的“去除空白符”语法(最推荐 🌟)

Go 语言的 text/template(或 html/template)提供了一个极其强大的语法:在双大括号内部的开头或结尾加上减号 -,可以自动消除模板标签外侧的换行符和空格

你只需要把代码改成这样(即使 VS Code 自动换行保存也不怕):

<div id="content-body" class="msg-content-body">
  {{- .Article.Question -}}
</div>

原理解释

  • {{- 会自动删掉该标签左边的所有空白字符(包括上一步的换行和缩进)。
  • -}} 会自动删掉该标签右边的所有空白字符(包括下一步的换行和缩进)。
  • 最终在浏览器渲染出的 HTML 将会是完美的:<div id="content-body" class="msg-content-body">实际内容</div>,没有一点多余的空格。

方法二:利用注释禁用 VS Code / Prettier 的自动格式化

如果你使用的是 Prettier 格式化插件,它会无视单行强制换行。你可以在该 HTML 块上方加入一条忽略指令注释,这样 VS Code 保存时就不会再去格式化这一行了。

请修改为:

<!-- prettier-ignore -->
<div id="content-body" class="msg-content-body">{{.Article.Question}}</div>

只要保持在一行内,且有了 <!-- prettier-ignore -->,编辑器保存时就会保持单行不缩回,也就不会再产生多余的换行和缩进空格了。