【问题标题】:Overwrite custom default color settings of browser覆盖浏览器的自定义默认颜色设置
【发布时间】:2021-03-25 06:21:11
【问题描述】:

我想通过考虑具有高对比度模式(或者可能只是暗模式)的用户来使网站更易于访问。 现在我遇到了自定义单选按钮的问题,这些按钮只是具有圆形边框的 HTML 元素:当用户将浏览器的默认背景颜色设置为黑色时,我的元素的背景颜色也是黑色,看起来像一个选中的选项,即使我将此元素的背景颜色设置为白色。

为了防止这种情况,我把白色边框加粗了,所以它看起来像一个白色背景。但这也产生了问题,有时在某些缩放级别或分辨率下,我的按钮中间会有一个小黑点。

有什么方法可以防止浏览器覆盖我在 CSS 中声明的背景色? 有没有什么我错过的东西,比如阻止这种行为的属性?

我也可以替换这些元素或以不同的方式实现它们,但此时我只是好奇是否有任何简单的解决方案。

我的单选按钮的样式如下所示:

input.check + label:before {
    display: inline-block;
    content: "";
    width: 1rem;
    height: 1rem;
    background: #fefefe;
    border: 0.2rem solid #fefefe;
    border-radius: 50%;
    position: absolute;
    left: -1rem;
}

input.check:checked + label:before {
    background: #000000;
}

Here is a link to a picture of my radio when checked / or default black background-color

我通过将背景颜色设置为黑色来制作单选按钮的“选中”外观 - 就像浏览器在这种情况下所做的那样。

编辑

到目前为止,对于改进自定义单选按钮的可访问性,已经有了很好的解决方案。 对我的问题更具体一点:

我在 Firefox 中使用以下颜色设置测试了该页面

但是无论您为每个元素指定什么,这似乎都会覆盖每个背景颜色。有没有办法让这种覆盖异常?

【问题讨论】:

  • 您能否添加一些代码或图片来显示您的问题,以便我们提供帮助?仅根据您的描述很难理解。
  • @anpel 我添加了 css 和元素的图像。
  • 您正在为哪些浏览器和操作系统开发?
  • 尝试使用javascript检测浏览器的类型,并在body元素中添加一个类。这样你就可以知道浏览器的类型,并且可以通过 css 定位它。
  • 请注意,有一些方法可以通过媒体查询检测 CSS 中的高对比度模式和暗模式,例如 prefers-contrastprefers-color-scheme

标签: html css high-contrast


【解决方案1】:

使用已在问题中提出的伪元素 - 可以完全按照您的意愿设置按钮样式。

这个 sn-p 和问题中的 CSS 的主要区别在于,我们将实际输入元素设置为不透明度 0。这意味着它仍然“存在”用于可访问性目的,但实际上无法看到。

此外,使用径向渐变为按钮着色,我们可以使用不同颜色的环,或者只使用一种最适合所选背景的颜色。正如问题中的代码所做的那样,我们还根据 rems 调整所有内容,以便为此更改浏览器设置的用户受益。

body {
  background-color: black;
}

input {
  opacity: 0;/* keep the input element in the DOM for accessibility it's just not seen*/ 
}

/* a pseudo element - this is what the user actually sees and we can style it */

label > input[type="radio"] + *::before {
  content: "";
  display: inline-block;
  width: 2rem;
  height: 2rem;
  margin-right: 1rem;
  border-radius: 50%;
  border-style: solid;
  border-width: 0.2rem;
  border-color: white;
  background: radial-gradient(white 0%, white 100%);/* choose what you want - one color or rings */
  border-color: white;
}

label > input[type="radio"]:checked + *::before {
  background: radial-gradient(red 0%, red 50%, white 50%, white 100%);/* choose what you want here too */
  border-color: red;
}

label {
  font-size: 2rem;
  color: white;
}
  <label>
    <input type="radio" name="mybutton" value="A" checked />
    <span>A</span>
  </label>
  <label>
    <input type="radio" name="mybutton" value="B" />
    <span>B</span>
  </label>
  <label>
    <input type="radio" name="mybutton" value="C" />
    <span>C</span>
  </label>

【讨论】:

  • 在我的例子中,每个背景颜色都被高对比度主题覆盖,当我使用径向渐变着色时也是如此。我主要担心的是,它保持白色。您有什么其他想法,或者 Firefox 颜色设置的工作方式与其他高对比度工具/扩展不同吗?
  • 这是否意味着您的高对比度主题是在您自己的 css 之后加载的?我不知道这样的浏览器设置流程。
  • 我根本不知道(没有在我的 css jet 上应用任何观察者),但无论哪种方式,它都会被浏览器覆盖/你不能用自定义 css 覆盖它——甚至在浏览器检查。那里说白色背景是活动的。
【解决方案2】:

要删除单选按钮的默认浏览器样式,您需要appearance: none;

您将在 here 上找到更多信息,您可以查看浏览器支持 here

从那里,您可以使用background 属性来创建检查效果,您甚至不需要使用::before

我创建了this fiddle 来演示。

代码如下所示:

:root {
  --background-color: white;
  --text-color: black;
  --fill-color: black;
  --border-color: black;
}

@media (prefers-color-scheme: dark) {
   :root {
    --background-color: black;
    --text-color: white;
    --fill-color: white;
    --border-color: white;
  }
}

body {
  background: var(--background-color);
  color: var(--text-color);
}

div {
  display: flex;
  align-items: center;
}

input[type=radio] {
  appearance: none;
  margin: 0 0.5rem 0 0;
  border: 1px solid var(--border-color);
  border-radius: 50%;
  width: 1rem;
  height: 1rem;
}

input[type=radio]:checked {
  background: radial-gradient(var(--fill-color) 0%, var(--fill-color) 30%, var(--background-color) 31%);
}
<p>Select an option:</p>

<div>
  <input class="check" type="radio" id="huey" name="duck" value="huey" checked>
  <label for="huey">Huey</label>
</div>

<div>
  <input class="check" type="radio" id="dewey" name="duck" value="dewey">
  <label for="dewey">Dewey</label>
</div>

<div>
  <input class="check" type="radio" id="louie" name="duck" value="louie">
  <label for="louie">Louie</label>
</div>

【讨论】:

  • 媒体查询是一个很好的提示,我以前没有遇到过。但遗憾的是,它仍然不适用于我在帖子中编辑的颜色设置。因为我想在深色主题上使用白色背景色。
  • @CountDocu 你有你想要实现的截图吗?
  • 我想要实现的基本上只是帖子中的链接图片。未选中时,整个内部按钮应为白色 -> 当背景颜色为白色时应为白色。
猜你喜欢
  • 2018-02-21
  • 1970-01-01
  • 2022-12-05
  • 1970-01-01
  • 2014-02-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多