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

Jekyll just-the-docs主题自定义Callout背景透明度设置失败求助

问题分析

你的核心问题是Liquid模板无法直接解析Sass的颜色函数(如rgba()、fade()),且你在_config.yml中使用的是颜色名称(如red),无法直接生成带透明度的RGBA值。原代码中rgba(${{ callout_color }}, {{ callout_opacity }})的写法混淆了Liquid和Sass语法,浏览器无法识别$red这类未编译的Sass变量,导致背景色不生效。


解决方案

以下提供两种可行方案,推荐第二种更优雅的样式分离写法:

方案1:Liquid手动拼接RGBA字符串(适配Hex颜色值)

步骤1:修改_config.yml的颜色定义为Hex格式

callouts:
  error:
    title: Error
    color: "#ff0000" # 替换为对应颜色的Hex值

步骤2:在custom-callout.html中处理颜色转换

{% assign callout_type = include.callout %}
{% assign callout_color = site.callouts[callout_type].color %}
{% assign callout_title = site.callouts[callout_type].title %}
{%- assign callout_opacity = 0.2 -%}

{% comment %} 将Hex颜色转换为RGB数值 {% endcomment %}
{% assign hex_color = callout_color | remove: '#' %}
{% assign r = hex_color | slice: 0,2 | hex_to_dec %}
{% assign g = hex_color | slice: 2,2 | hex_to_dec %}
{% assign b = hex_color | slice: 4,2 | hex_to_dec %}
{% assign background_color = "rgba(" | append: r | append: "," | append: g | append: "," | append: b | append: "," | append: callout_opacity | append: ")" %}

<div class="custom-callout" style="background-color: {{ background_color}}; border-left: 4px solid {{ callout_color }}; border-radius: 4px; box-shadow: 0 1px 2px rgba(0, 0, 0, 0.12), 0 3px 10px rgba(0, 0, 0, 0.08); padding: 0.8rem; margin-bottom: 1.5rem;">
  <strong class="custom-callout-title" style="color: {{ callout_color }}; display: block; font-weight: bold; margin-top: 0; margin-bottom: 0.5rem;">{{ callout_title }}</strong>
  {{ include.content | markdownify }}
  {% if include.collapsible_content %}
    <details>
      <summary>{{ include.summary }}</summary>
      {{ include.collapsible_content | markdownify }}
    </details>
  {% endif %}
</div>

方案2:用CSS类+Sass处理(推荐,样式结构分离)

步骤1:创建自定义Sass文件

在项目_sass/custom目录下新建callouts.scss(无目录则手动创建):

.custom-callout {
  border-radius: 4px;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.12), 0 3px 10px rgba(0, 0, 0, 0.08);
  padding: 0.8rem;
  margin-bottom: 1.5rem;
  border-left: 4px solid var(--callout-color);
  background-color: rgba(var(--callout-rgb), 0.2);
}

.custom-callout-title {
  color: var(--callout-color);
  display: block;
  font-weight: bold;
  margin-top: 0;
  margin-bottom: 0.5rem;
}

{% comment %} 遍历所有callout类型,生成对应CSS变量 {% endcomment %}
{% for callout_type, callout in site.callouts %}
  .custom-callout-{{ callout_type }} {
    {% if callout.color contains '#' %}
      {% assign hex = callout.color | remove: '#' %}
      {% assign r = hex | slice: 0,2 | hex_to_dec %}
      {% assign g = hex | slice: 2,2 | hex_to_dec %}
      {% assign b = hex | slice: 4,2 | hex_to_dec %}
      --callout-color: {{ callout.color }};
      --callout-rgb: {{ r }}, {{ g }}, {{ b }};
    {% elsif callout.color contains 'rgb' %}
      {% assign rgb_values = callout.color | remove: 'rgb(' | remove: ')' | split: ',' %}
      --callout-color: {{ callout.color }};
      --callout-rgb: {{ rgb_values | join: ',' }};
    {% else %}
      {% comment %} 处理颜色名称,可自行扩展更多颜色映射 {% endcomment %}
      {% case callout.color %}
        {% when 'red' %}
          --callout-color: red;
          --callout-rgb: 255, 0, 0;
        {% when 'blue' %}
          --callout-color: blue;
          --callout-rgb: 0, 0, 255;
        {% when 'green' %}
          --callout-color: green;
          --callout-rgb: 0, 128, 0;
      {% endcase %}
    {% endif %}
  }
{% endfor %}

步骤2:修改custom-callout.html模板

去掉内联样式,添加对应类名:

{% assign callout_type = include.callout %}
{% assign callout_title = site.callouts[callout_type].title %}

<div class="custom-callout custom-callout-{{ callout_type }}">
  <strong class="custom-callout-title">{{ callout_title }}</strong>
  {{ include.content | markdownify }}
  {% if include.collapsible_content %}
    <details>
      <summary>{{ include.summary }}</summary>
      {{ include.collapsible_content | markdownify }}
    </details>
  {% endif %}
</div>

步骤3:导入自定义Sass

确保assets/css/custom.css中导入了新建的Sass文件:

@import "custom/callouts";

内容的提问来源于stack exchange,提问作者Nre

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 02:17:45