<select>在chrome浏览器下背景透明问题

本文探讨了在Chrome浏览器中使select元素背景透明的解决方案,通过使用-webkit-appearance:none属性来覆盖默认样式,并指出这种方法可能导致下拉箭头消失的问题。文章提供了在线效果查看链接,并建议使用CSS来实现自定义select框以达到美观效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

        在上篇文章《只用CSS美化选择框》运用了背景透明的技巧来美化选择框,但在chrome浏览器下遇到了跟ie、ff不一样的透明效果,下面重现一下:

在一个大的div(背景红色)内放置一个select元素(背景透明)。在线查看(可在不同浏览器中查看效果)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>chrome浏览器下select元素透明问题</title>
</head>
<body>
<div style="width: 200px; height: 200px; background: red">
   <select style="background: transparent;">
       <option>Here is the first option</option>
       <option>The second option</option>
   </select>
</div>
</body>
</html>

我们期望的效果如图:

2012-03-02_1517472012-03-02_151924

但chrome浏览器却是这样子的

2012-03-02_152143

问题不言而喻了,chrome居然没有按照bacground:tansparent的指令行事,好吧,我认为它是另类,给它一个偏方-webkit-appearance: none;(取消webkit默认的样式)试试!修改后代码在线效果 查看

2012-03-02_153719

确实透明了,但是好像默认的下拉小箭头也透明了(严格来说应该是消失了),stackoverflow上也有相关问题

http://stackoverflow.com/questions/2226666/background-image-for-select-dropdown-does-not-work-in-chrome

http://stackoverflow.com/questions/4142619/how-to-make-select-element-be-transparent-in-chrome

 

总结:webkit内核浏览器(chrome、safari)有自己的默认样式,针对webkit核心的浏览器使用-webkit-appearance: none让select背景透明;这好像不完美,下拉箭头没了,我也没找到完美的方法——既能背景透明又能不失去那个下拉箭头。推荐《只用CSS美化选择框》来实现自定义的select框吧!

有完美的方法,请告知一声,谢谢!

转载于:https://www.cnblogs.com/shishm/archive/2012/03/02/2377354.html

<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>基于STM32的环境监控系统框图</title> <script src="https://d3js.org/d3.v7.min.js"></script> <style> .system-container { font-family: 'Arial', sans-serif; margin: 20px; } .layer-box { stroke: #3498db; stroke-width: 2; fill: #ecf0f1; rx: 5; ry: 5; } .module-box { stroke: #2c3e50; stroke-width: 1; fill: white; cursor: pointer; rx: 3; ry: 3; } .connection { stroke: #7f8c8d; stroke-width: 1.5; marker-end: url(#arrowhead); } .title { font-size: 24px; font-weight: bold; fill: #2c3e50; text-anchor: middle; } .layer-title { font-size: 18px; font-weight: bold; fill: #e74c3c; } .module-title { font-size: 14px; font-weight: bold; fill: #34495e; } .module-detail { font-size: 12px; fill: #7f8c8d; } .hover-effect { filter: url(#glow); } .tooltip { position: absolute; padding: 10px; background: rgba(255, 255, 255, 0.9); border: 1px solid #3498db; border-radius: 5px; pointer-events: none; max-width: 300px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); } </style> </head> <body> <div class="system-container" id="systemDiagram"></div> <script> // 系统架构数据定义 const systemData = { title: "基于STM32的环境监控系统架构", layers: [ { name: "感知层", modules: [ { name: "温湿度传感器(DHT11)", detail: "温度:-20℃~60℃\n湿度:20%~90% RH" }, { name: "空气质量传感器(MQ135)", detail: "检测有害气体浓度" }, { name: "火焰传感器(H01)", detail: "响应时间<0.5s" }, { name: "水位传感器(M04)", detail: "检测范围 0~10cm" }, { name: "红外入侵传感器(E18-D80NK)", detail: "检测距离 3~80cm" } ] }, { name: "控制层", modules: [ { name: "数据融合与处理", detail: "多传感器数据滤波/整合" }, { name: "阈值判断与逻辑控制", detail: "参数超标判断/异常优先级" }, { name: "执行器控制策略", detail: "风扇启停/报警触发" }, { name: "通信协议解析(MQTT)", detail: "数据封装/指令解析" }, { name: "模式管理", detail: "自动/手动模式切换" } ] }, { name: "执行层", modules: [ { name: "通风风扇", detail: "DC风扇 5V/12V\nPWM调速" }, { name: "声光报警模块", detail: "蜂鸣器+语音播报" }, { name: "无线通信模块(ESP8266-01S)", detail: "UART串口通信" } ] }, { name: "交互层", modules: [ { name: "OLED显示屏", detail: "实时参数显示/状态提示" }, { name: "物理按键", detail: "模式切换/参数设置" }, { name: "移动端APP", detail: "远程监控与控制" } ] }, { name: "远程平台", modules: [ { name: "物联网云平台", detail: "数据存储/报警推送/远程控制" } ] } ], connections: [ { source: "感知层", target: "控制层" }, { source: "控制层", target: "执行层" }, { source: "执行层", target: "交互层" }, { source: "执行层", target: "远程平台" }, { source: "交互层", target: "远程平台" }, { source: "远程平台", target: "控制层", label: "远程指令反馈" } ] }; // 创建SVG容器 const svg = d3.select("#systemDiagram") .append("svg") .attr("width", "100%") .attr("height", "700") .append("g") .attr("transform", "translate(50, 50)"); // 创建箭头标记 svg.append("defs").append("marker") .attr("id", "arrowhead") .attr("viewBox", "-0 -5 10 10") .attr("refX", 18) .attr("refY", 0) .attr("orient", "auto") .attr("markerWidth", 10) .attr("markerHeight", 10) .append("path") .attr("d", "M0,-5 L10,0 L0,5") .attr("fill", "#7f8c8d"); // 创建发光效果滤镜 const defs = svg.append("defs"); const filter = defs.append("filter") .attr("id", "glow") .attr("height", "130%") .attr("width", "130%"); filter.append("feGaussianBlur") .attr("in", "SourceAlpha") .attr("stdDeviation", "4") .attr("result", "blur"); filter.append("feFlood") .attr("flood-color", "#3498db") .attr("flood-opacity", "0.5") .attr("result", "glowColor"); filter.append("feComposite") .attr("in", "glowColor") .attr("in2", "blur") .attr("operator", "in") .attr("result", "glowAlpha"); filter.append("feMerge") .selectAll("feMergeNode") .data(["SourceGraphic", "glowAlpha"]) .enter().append("feMergeNode") .attr("in", function(d) { return d; }); // 添加系统标题 svg.append("text") .attr("class", "title") .attr("x", 500) .attr("y", 20) .text(systemData.title); // 工具提示创建 const tooltip = d3.select("body").append("div") .attr("class", "tooltip") .style("opacity", 0); // 创建各层容器 const layers = svg.selectAll(".layer") .data(systemData.layers) .enter().append("g") .attr("class", "layer-group") .attr("transform", (d, i) => `translate(0, ${i * 130 + 40})`); // 添加层标题 layers.append("text") .attr("class", "layer-title") .attr("x", 500) .attr("y", -10) .text(d => d.name + " (点击展开/折叠)"); // 添加层矩形 const layerBoxes = layers.append("rect") .attr("class", "layer-box") .attr("width", 800) .attr("height", 110) .attr("x", 100); // 添加模块 const moduleGroups = layers.selectAll(".module-group") .data(d => d.modules) .enter().append("g") .attr("class", "module-group") .attr("transform", function(将这个程序转换成图片
最新发布
07-16
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值