如何在GtkButton获得焦点时修改其边框颜色?
你的CSS代码未生效主要有两个核心原因:
- 只设置了
outline-width和border-color,但缺少轮廓样式声明——默认outline-style为none,即使设置了宽度和颜色也不会显示。 - Gtk控件的聚焦边框通常由
outline属性控制,而非border,所以设置border-color不会影响聚焦时的边框效果。
正确的CSS写法
针对GtkToggleButton的聚焦状态,你需要完整设置outline的三个属性:
.toggle-button:focus { outline-style: solid; /* 必须声明样式,否则轮廓不显示 */ outline-width: 2px; outline-color: #000000; }
额外注意事项
- 如果你实际要设置的是普通GtkButton(而非ToggleButton),请将选择器改为
.button:focus。 - 若仍不生效,可能是主题样式优先级更高,可以尝试添加
!important强制覆盖(仅在必要时使用):
.toggle-button:focus { outline-style: solid !important; outline-width: 2px !important; outline-color: #000000 !important; }
- 部分GTK主题会区分键盘聚焦和鼠标聚焦,如果需要针对键盘触发的聚焦设置样式,可以使用
:focus-visible选择器替代:focus。
内容的提问来源于stack exchange,提问作者anon
相关产品推荐
相关产品推荐

