简体   繁体   English

无法为Select元素设置样式,以使其具有透明背景

[英]Trouble Styling a Select element to have a transparent background

I'm trying to style a Select element to have a transparent background, and during troubleshooting the logic has run me in a circle. 我正在尝试设置Select元素的样式以使其具有透明背景,并且在进行故障排除期间,逻辑使我陷入了困境。

We use Divi as our WP theme. 我们使用Divi作为我们的WP主题。 We are using Ninja Forms as our forms plugin. 我们正在使用Ninja Forms作为我们的表单插件。

Two fields in this mix at this point, a regular text input and the select field. 此时,这两个字段混合在一起,即常规文本输入和选择字段。

We applied a custom class to each of those fields. 我们对每个字段都应用了一个自定义类。

We applied the following directive to that class: 我们将以下指令应用于该类:

.header_form_select {
    background: transparent !important;
  }

Here's where the troubleshooting went in circles. 这是排查问题的地方。 When we test this arrangement we find that the text input accepts the styling and is indeed transparent. 当我们测试这种安排时,我们发现文本输入接受样式并且确实是透明的。 The select, not so much. 选择,没有那么多。 If we move the select field element outside of it's parent element the styling DOES affect it. 如果将选择字段元素移到其父元素之外,则样式会影响它。 So something must be overwriting it, right? 所以一定要覆盖它,对吧?

However, if we keep it within the parent element (as designed), and style the background to be red instead of transparent it works as expected. 但是,如果我们将其保留在父元素中(如设计的那样),并且将背景设置为红色而不是透明,则可以按预期工作。 It seems the trouble is specifically with transparency AND the select element (since it works fine with the text input)... 看来麻烦特别是与透明度和select元素有关(因为它在文本输入中可以正常工作)...

I can't seem to find any information on why it would be a quirk around select elements, and at the same time it's hard to believe that's the root of the problem since if we just move it outside of that parent element it DOES work... 我似乎找不到任何信息,为什么它会围绕选择元素变得怪异,同时,我们很难相信这是问题的根源,因为如果我们将其移出该父元素之外,它将起作用。 ..

Any help untangling this mess would be super great. 弄清这种混乱的任何帮助都将非常棒。 :) Thanks everyone. :) 感谢大家。

In CSS, a property set to its default value will be overridden if the parent element has the same property set to an explicit value. 在CSS中,如果父元素具有设置为显式值的相同属性,则将覆盖设置为其默认值的属性。

This is called inheritance . 这称为继承

In your case, I suspect the select element is inheriting styles from the parent when you use transparent because that is the default value of background-color . 在您的情况下,我怀疑当您使用transparent时, select元素是从父级继承样式,因为这是background-color的默认值。

.parent {
  background-color: blue; //explicitly-set value on parent
}

.child {
.background-color: transparent //default value; will be overridden by parent
.background-color: red //explicitly-set value, will NOT be overidden by parent
}

Unless the browser support is a deal-breaker, you can use the native CSS rgba() function to explicitly set the child to be transparent. 除非浏览器支持令人难以置信,否则您可以使用本机CSS rgba()函数将子级明确设置为透明。

.child {
  background-color: rgba(0, 0, 0, 0);
}

EDIT: edited for clarity (I hope) 编辑:为清楚起见进行编辑(我希望)

!important is a pretty aggressive way to override styles, but if even that's not enough to override then it's typically because the theme/plugin is using an even more specific selector to change the select background with the !important tag. !important是一种非常激进的方式来覆盖样式,但是即使这还不足以覆盖样式,通常是因为主题/插件正在使用更具体的选择器来更改!important标签的选择背景。

It might look something like this; 它可能看起来像这样;

.parent select.elementclass {
  background: #ffffff !important;
}

You'll have to use an equally or more specific selector than the overriding style. 与覆盖样式相比,您必须使用同等或更具体的选择器。 If you run 'inspect element' on the select element, you can find what's overriding the background property and copy that selector into your stylesheet. 如果在select元素上运行“ inspect element”,则可以查找覆盖background属性的内容并将该选择器复制到样式表中。 That should be enough to override as custom stylesheets are loaded after theme styles. 这应该足以覆盖,因为在主题样式之后加载了自定义样式表。 If not just tweak it to be more specific, eg use an ID for the select element. 如果不只是进行调整,使其更加具体,例如,为选择元素使用ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM