css3失去焦点伪类_CSS:伪类内的焦点

news/2024/6/30 9:57:04

css3失去焦点伪类

介绍 (Introduction)

Selecting a parent element has long been impossible to do using just CSS, but a pseudo-class, :focus-within, changes that story somewhat. It allows to style an element when it has focus, but also when any of its inner elements (descendants) have focus. A prime example is with a form where you’d want a container element to be styled a certain way when the user focuses into one of the input elements.

长期以来,仅使用CSS来选择父元素是不可能的,但是伪类:focus-within会稍微改变这个故事。 它允许在元素具有焦点时设置样式,也可以在其任何内部元素(后代)具有焦点时设置样式。 一个很好的例子是一种表单,当用户将焦点放在输入元素之一时,您希望容器元素以某种方式设置样式。

HTML和CSS代码 (HTML and CSS Code)

Here’s an example of using :focus-within with a form. Let’s start with this markup:

这是在表单中使用:focus-within的示例。 让我们从这个标记开始:

<form tabindex="0" class="myForm">
  <h3>What's your favorite color?</h3>
  <input type="text">
</form>

And our CSS rules are the following:

我们CSS规则如下:

.myForm:focus-within {
  background: #f8f8f8
    repeating-linear-gradient(
      45deg,
      transparent,
      transparent 35px,
      rgba(255, 255, 255, 0.5) 35px,
      rgba(255, 255, 255, 0.5) 70px
    );
}
.myForm:focus-within::before {
  content: "I'm a happy selected form!";
  color: rgb(239, 187, 53);
}

工作演示 (Working Demo)

Below you can see the result if you’re using a supporting browser. Notice how the different background is applied to the containing form element when the form itself is focused or when either of the inputs are focused.

如果您使用的是支持的浏览器,则可以在下面看到结果。 请注意,当表单本身被聚焦或两个输入中的任何一个被聚焦时,如何将不同的背景应用于包含的表单元素。

If we had used the good old :focus pseudo-class instead of :focus-within, our container form would be styled only when the focus is on the form itself, but not when the inputs are focused:

如果我们使用旧的:focus伪类而不是:focus-within ,则仅当焦点位于表单本身时才设置容器表单的样式,而对输入进行聚焦时则不会:

你最喜欢什么颜色? (What’s your favorite color?)

Thanks to Lea Verou for the CSS background pattern.

感谢Lea Verou提供CSS背景图案。

结论 (Conclusion)

In order for :focus-within to work as expected, you’ll have to make sure that the inner elements of the container are focusable. Input elements are focusable by default, but div, img or p elements, for example, are not. The tabindex attribute can be used to make an element focusable. The container element should also be focusable in order to receive the styling when focusing from just the container.

为了使:focus-within能够按预期工作,您必须确保容器的内部元素是可聚焦的。 输入元素默认情况下是可聚焦的,但是div , img或p元素则不能。 tabindex属性可用于使元素具有焦点。 容器元素也应该是可聚焦的,以便在仅从容器聚焦时接收样式。

Browser Compatibility Check: As of 2020, all modern browsers support focus-within, but check Can I Use for detailed, version-specific browser support.

浏览器兼容性检查:自2020年起,所有现代浏览器都支持focus-within ,但请选中“我可以使用”以获取详细的特定于版本的浏览器支持。

翻译自: https://www.digitalocean.com/community/tutorials/css-focus-within-pseudo-class

css3失去焦点伪类


http://www.niftyadmin.cn/n/3647801.html

相关文章

绘制恒线速度的参数曲线

假设一条参数曲线和某个参数 t 相关。L: x f(t) y g(t)如果我们绘制这条参数曲线的时候的&#xff0c;t是按比例增加的话。可能点的分布会不均匀。那么按照什么公式来决定t的步长能让曲线的点分布均匀呢&#xff1f;首先我们对参数曲线公式进行微分。dx df(t)dy dg…

css遮罩mask_使用mask-image属性在CSS中遮罩图像

css遮罩maskWe covered the use of the clip-path property for clipping using CSS, so it’s only natural that we now go over masking. Contrary to clipping, where a part of an image or element is either completely invisible or completely visible, with masking …

css中让图片剪切居中_CSS中使用剪切路径进行剪切的简介

css中让图片剪切居中介绍 (Introduction) clip-path is a very interesting property that allows to clip the visible portion of SVG elements, images or any HTML element really. clip-path是一个非常有趣的属性&#xff0c;它允许剪切SVG元素&#xff0c;图像或任何HTML…

脚本与渲染器 .

一直以来都想做一个脚本驱动的渲染器.就是说可以用脚本定制渲染器的行为,比如创建多少个渲染队列,如何渲染.多少RenderTarget, 每个物体的材质也是一样. 要生成多少个Pass,每个Pass是立即渲染呢还是放到那个队列里 . 其实我是个很懒的人 ,这个想法早在去年就有了.一直拖到…

laravel入门_Laravel入门

laravel入门视频 (Video) 关于谈话 (About the Talk) Laravel — a free, open-source PHP web application framework — is one of the best ways to build web applications. It is a strong framework that gives many niceties out of the box, freeing you to create wit…

项目开发中源代码树的组织

很多人多很重视自己代码的可读性,重用性等,尽量让自己的代码看上去更加的雅观,因为很多人都认为这是代码优劣的门面光. 不过,我却认为,代码的门面光应该是源代码树的组织. 因为,别人看你的代码首先看到的目录结构.一个良好的目录结构,能很方便的让你定位到你需要的组…

css 垂直对齐_CSS垂直对齐属性

css 垂直对齐介绍 (Introduction) vertical-align defines the vertical alignment for the content of a table cell or for an inline element against the rest of the inline flow. vertical-align定义表格单元格的内容或内联元素相对于其余内联流的垂直对齐方式。 vertic…

一道智力题的数学解

最近看数学。都看的有点锈逗了。看到一道智力题&#xff0c;情不自禁的用数学来解了。。只是不知道结果对不对。题目&#xff1a; 已知&#xff1a;每个飞机只有一个油箱&#xff0c; 一箱油可供一架飞机绕地球飞180度。飞机之间可以相互加油&#xff08;注意是相互&…