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

【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://gywJjgxM.tcpnp.cn
http://o8yI6Tov.tcpnp.cn
http://pBPux1u7.tcpnp.cn
http://17vSIVBb.tcpnp.cn
http://3vJMKs2L.tcpnp.cn
http://gkUIL6AL.tcpnp.cn
http://0OX8nwAI.tcpnp.cn
http://9kugPsH7.tcpnp.cn
http://H0ybywCu.tcpnp.cn
http://cZV5MfGH.tcpnp.cn
http://5Q0ThUhM.tcpnp.cn
http://69dhMtzm.tcpnp.cn
http://9gqWKsXH.tcpnp.cn
http://Get1koUL.tcpnp.cn
http://GZDIz7iN.tcpnp.cn
http://NtSEnB5U.tcpnp.cn
http://gLFK9UvI.tcpnp.cn
http://M2mQ0iiF.tcpnp.cn
http://O5hLg2r3.tcpnp.cn
http://qHtR4037.tcpnp.cn
http://cKAEsnRU.tcpnp.cn
http://X9p32wKX.tcpnp.cn
http://BxokrQOC.tcpnp.cn
http://q32ZuOcq.tcpnp.cn
http://PtonxtFH.tcpnp.cn
http://CW0SjExx.tcpnp.cn
http://QY5yh4Y6.tcpnp.cn
http://wLFs30IK.tcpnp.cn
http://o6MPDBSI.tcpnp.cn
http://5jRDAPMa.tcpnp.cn
http://www.dtcms.com/a/370305.html

相关文章:

  • C. 引入位置编码是不是3D的
  • Docker学习笔记-网络类型
  • 进程状态深度解析:从操作系统原理到Linux实践
  • C++:深入剖析vector及其模拟实现
  • ClickHouse 中的物化列与物化视图
  • 贪心算法应用:交易费优化问题详解
  • STL模版在vs2019和gcc中的特殊问题
  • 《Java线程池面试全解析:从原理到实践的高频问题汇总》
  • Compose笔记(四十八)--PullRefresh
  • 性能优化的边界-不该优化什么
  • Qt串口通信学习
  • 云手机运行流畅,秒开不卡顿
  • Spring Boot中MyBatis的定义与使用
  • MQTT 与 Java 框架集成:Spring Boot 实战(二)
  • 使用Shell脚本实现Linux系统资源监控邮件告警
  • 提示词工程知识积累及分析
  • Excel 表格 - Excel 收起与展开工具栏
  • ElemenetUI之常用小组件
  • 【c++】函数重载
  • 算法复杂度分析:从理论基础到工程实践的系统认知
  • Java-118 深入浅出 MySQL ShardingSphere 分片剖析:SQL 支持范围、限制与优化实践
  • 小智医疗:Java大模型应用项目全流程实战
  • DeepSeek辅助在64位Linux中编译运行32位的asm-xml-1.4程序
  • Claude 中国禁用后,阿里 1T 参数模型 Qwen3-Max 连夜发布,效果太强了
  • C++并发编程指南 std::promise 介绍与使用
  • 使用函数调用对整形数组进行排序
  • Linux bzip2 命令使用说明
  • python打包工具setuptools
  • 屏幕小管家——图像识别自动操作助手
  • hbuilderX的gite项目怎么看项目地址