当前位置: 首页 > ops >正文

【CSS,DaisyUI】自定义选取内容的颜色主题

当你的网站使用了多个主题,比如 light/dark。当用户选择一段文本,常常会出现一个主题没问题,另一个主题颜色怪怪的。是因为选择区域的主题通常没有变化,导致与当前主题不配合。

下面四种方法可解决这个问题:

The problem occurs because most browsers use default system colors for text selection, which might not match your theme’s color scheme.

Here are a few ways to fix this in DaisyUI:

Method 1: CSS Custom Properties (Recommended)

Add custom CSS for text selection in your global styles:

/* Custom text selection colors */
::selection {background-color: hsl(var(--p) / 0.3); /* Using primary color with opacity */color: hsl(var(--pc)); /* Primary content color */
}::-moz-selection {background-color: hsl(var(--p) / 0.3);color: hsl(var(--pc));
}/* Dark theme specific selection */
[data-theme="dark"] ::selection {background-color: hsl(var(--s) / 0.5); /* Secondary color for dark theme */color: hsl(var(--sc));
}[data-theme="dark"] ::-moz-selection {background-color: hsl(var(--s) / 0.5);color: hsl(var(--sc));
}

Method 2: Using DaisyUI’s CSS Variables

DaisyUI provides CSS variables you can leverage:

::selection {background-color: var(--fallback-bc,oklch(var(--bc)/0.2));color: var(--fallback-b1,oklch(var(--b1)));
}[data-theme="dark"] ::selection {background-color: var(--fallback-p,oklch(var(--p)/0.3));color: var(--fallback-pc,oklch(var(--pc)));
}

Method 3: Direct Color Values

If you prefer specific colors:

::selection {background-color: #3b82f6; /* blue-500 */color: white;
}[data-theme="dark"] ::selection {background-color: #60a5fa; /* blue-400 */color: #1f2937; /* gray-800 */
}

Method 4: Using Tailwind’s @apply (if using Tailwind)

@layer utilities {.selection-light {@apply selection:bg-primary selection:bg-opacity-30 selection:text-primary-content;}.selection-dark {@apply selection:bg-secondary selection:bg-opacity-50 selection:text-secondary-content;}
}/* Apply based on theme */
[data-theme="light"] {@apply selection-light;
}[data-theme="dark"] {@apply selection-dark;
}

Complete Example with DaisyUI

<!DOCTYPE html>
<html data-theme="light">
<head><style>/* Custom text selection */::selection {background-color: hsl(var(--p) / 0.3);color: hsl(var(--pc));}::-moz-selection {background-color: hsl(var(--p) / 0.3);color: hsl(var(--pc));}[data-theme="dark"] ::selection {background-color: hsl(var(--s) / 0.5);color: hsl(var(--sc));}[data-theme="dark"] ::-moz-selection {background-color: hsl(var(--s) / 0.5);color: hsl(var(--sc));}</style>
</head>
<body><!-- Your content -->
</body>
</html>

The first method using DaisyUI’s CSS variables is recommended because it maintains consistency with your theme’s color scheme and automatically adapts if you change your theme’s primary/secondary colors.

Choose the method that best fits your project’s setup and preferences!

http://www.xdnf.cn/news/20213.html

相关文章:

  • Android开发——初步了解AndroidManifest.xml
  • 零基础入门深度学习:从理论到实战,GitHub+开源资源全指南(2025最新版)
  • C++ 条件变量 通知 cv.notify_all() 先释放锁再通知
  • [光学原理与应用-428]:非线性光学 - 为什么要改变光的波长/频率,获得特点波长/频率的光?
  • RocketMQ如何处理消息堆积
  • 云某惠旧案再审可能性与商业创新实践:积分运营的边界与实体商家机遇
  • 【设计模式】 工厂方法模式
  • 【YOLOv11】2.安装Anaconda3
  • 机器人控制器开发(定位算法——map、odom、baselink关联与差异)
  • JavaScript的库简介
  • 离散数学学习指导与习题解析
  • react生命周期,详细版本
  • 运筹学——求解线性规划的单纯形法
  • solidity的高阶语法2
  • AI工程师对于AI的突发奇想
  • Docker Desktop 安装 Linux(告别传统的虚拟机VMware)
  • Date、BigDecimal类型值转换
  • 残差去噪扩散模型
  • 字节跳动OmniHuman-1.5发布:单图+音频秒变超真实视频,AI数字人技术再升级
  • HOT100--Day13--104. 二叉树的最大深度,226. 翻转二叉树,101. 对称二叉树
  • Docker入门到精通:从零基础到生产部署
  • 如何在路由器上配置DHCP服务器?
  • 本体论中的公理与规则——从经典逻辑到神经符号融合的演进
  • Hive on Tez/Spark 执行引擎对比与优化
  • AI浪潮下,人类创造力的“危”与“机”
  • 2026届大数据毕业设计选题推荐-基于大数据旅游数据分析与推荐系统 爬虫数据可视化分析
  • JAVA基本文件操作
  • 【74页PPT】MES简介(附下载方式)
  • TensorFlow 面试题及详细答案 120道(101-110)-- 底层原理与扩展
  • C++笔记之软件设计原则总结