


(Recommended tutorial: CSS Tutorial)
Attribute selectors are amazing. They can get you out of sticky situations, help you avoid adding classes, and point out problems in your code. But don't worry, although attribute selectors are very complex and powerful, they are easy to learn and use. In this article, we'll discuss how they work and give some ideas on how to use them.
Usually put HTML
attributes in square brackets, called attribute selectors, as follows:
[href] { color: red; }
In this way, any attribute with href
and The text color of elements without a more specific selector will be red
. Attribute selectors have the same properties as classes.
NOTE: For more on the CSS specificity of cage matching, you can read CSS Properties: Things You Should Know or if you like Star Wars: CSS Properties Wars.
But you can do more with attribute selectors. Like your DNA, they have internal logic to help you choose various attribute combinations and values. They can match any attribute within an attribute, even a string value, rather than an exact match like tag, class, or id selectors.
Attribute selector
The attribute selector can exist independently. More specifically, if you need to select all p tags with the title
attribute, you can do this:
p[title]
But you can also select the child elements of p
with the title attribute through the following operations
p [title]
It should be noted that there is no space between them means that the attribute is on the same element (like there is no space between the element and the class), and the space between them means that the descendant selector, i.e. selects the child elements of the element with that attribute.
You can more granularly select attributes with specific attribute values.
p[title="dna"]
The above selects all p's with the exact name dna
, although there are selector algorithms to handle every case (and more), "dna" will not be selected here is awesome" or "dnamutation" 's title.
Note: In most cases, quotes are not required in attribute selectors, but I use them because I believe it improves code readability and ensures that edge use cases will work correctly.
If you want to select title elements containing dna
, such as "my beautiful dna" or "mutating dna is fun!", you can use the tilde (~).
p[title~="dna"]
If you want to match title
ending with dna, such as "dontblamemeblamemydna" or "his-stupidity-is-from-upbringing-not-dna", Just use the $
identifier:
[title$="dna"]
if you want to match title
starting with dna, such as "dnamutants" or "dna- splicing-for-all", just use the ^
identifier:
[title^="dna"]
While exact matching is helpful, it may be too tight, and ^
Symbol matching may be too broad for your needs. For example, you may not want to select the title "genealogy" but still select "gene" and "gene-data". Pipe Characteristics (|) That's it, the attribute must be a complete and unique word, or separated by -
.
[title|="gene"]
Finally, there is a fuzzy search attribute operator that matches any substring. String splitting is done in the attribute, as long as the word dna
can be separated:
[title*="dna"]
What makes these attribute selectors even more powerful is that they are stackable, allowing you to select elements with multiple matching factors.
If you need to find a a
tag that has a title
and a class ending with "genes", you can use the following method:
a[title][class$="genes"]
Not only can you select attributes of HTML elements, you can also use pseudo-type elements to print out text:
<span class="joke" title="Gene Editing!"> What’s the first thing a biotech journalist does after finishing the first draft of an article? </span>
.joke:hover:after { content: "Answer:" attr(title); display: block; }
The above code will display a custom string when the mouse is hovered.
The last thing to know is that you can add a flag to make attribute searches case-insensitive. Add i
before the closing square bracket:
[title*="DNA" i]
so it will match dna
, DNA
, dnA
, etc.
Now that we have seen how to make selections using attribute selectors, let's look at some use cases. I divide them into two categories: General purpose and Diagnostic.
General purpose
Settings for input type styles
You can use different styles for input types, such as email and phone.
input[type="email"] { color: papayawhip; } input[type="tel"] { color: thistle; }
Show phone link
You can hide phone numbers in specific sizes and show phone link to make calls easily on your phone.
span.phone { display: none; } a[href^="tel"] { display: block; }
内部链接 vs 外部链接,安全链接 vs 非安全链接
你可以区别对待内部和外部链接,并将安全链接设置为与不安全链接不同:
a[href^="http"]{ color: bisque; } a:not([href^="http"]) { color: darksalmon; } a[href^="http://"]:after { content: url(unlock-icon.svg); } a[href^="https://"]:after { content: url(lock-icon.svg); }
下载图标
HTML5 给我们的一个属性是“下载”,它告诉浏览器,你猜对了,下载该文件而不是试图打开它。这对于你希望人们访问但不希望它们立即打开的 PDF
和 DOC
非常有用。它还使得连续下载大量文件的工作流程更加容易。下载属性的缺点是没有默认的视觉效果将其与更传统的链接区分开来。通常这是你想要的,但如果不是,你可以做类似下面的事情:
a[download]:after { content: url(download-arrow.svg); }
还可以使用不同的图标(如PDF与DOCX与ODF等)来表示文件类型,等等。
a[href$="pdf"]:after { content: url(pdf-icon.svg); } a[href$="docx"]:after { content: url(docx-icon.svg); } a[href$="odf"]:after { content: url(open-office-icon.svg); }
你还可以通过叠加属性选择器来确保这些图标只出现在可下载链接上。
a[download][href$="pdf"]:after { content: url(pdf-icon.svg); }
覆盖或重新使用已废弃/弃用的代码
我们都遇到过时代码过时的旧网站,在 HTML5 之前,你可能需要覆盖甚至重新应用作为属性实现的样式。
<p bgcolor="#000000" color="#FFFFFF">Old, holey genes</p> p[bgcolor="#000000"] { /*override*/ background-color: #222222 !important; } p[color="#FFFFFF"] { /*reapply*/ color: #FFFFFF; }
重写特定的内联样式
有时候你会遇到一些内联样式,这些样式会影响布局,但这些内联样式我们又没修改。那么以下是一种方法。
如果你道要覆盖的确切属性和值,并且希望在它出现的任何地方覆盖它,那么这种方法的效果最好。
对于此示例,元素的边距以像素为单位设置,但需要在 em
中进行扩展和设置,以便在用户更改默认字体大小时可以正确地重新调整元素。
<p style="color: #222222; margin: 8px; background-color: #EFEFEF;"Teenage Mutant Ninja Myrtle</p> p[style*="margin: 8px"] { margin: 1em !important; }
显示文件类型
默认情况下,文件输入的可接受文件列表是不可见的。 通常,我们使用伪元素来暴露它们:
<input type="file" accept="pdf,doc,docx"> [accept]:after { content: "Acceptable file types: " attr(accept); }
html 手风琴菜单
details
和summary
标签是一种只用HTML做扩展/手风琴菜单的方法,details
包括了summary
标签和手风琴打开时要展示的内容。点击summary
会展开details
标签并添加open
属性,我们可以通过open
属性轻松地为打开的details
标签设置样式:
details[open] { background-color: hotpink; }
打印链接
在打印样式中显示URL使我走上了理解属性选择器的道路。 你现在应该知道如何自己构建它, 你只需选择带有href
的所有标签,添加伪元素,然后使用attr()
和content
打印它们。
a[href]:after { content: " (" attr(href) ") "; }
自定义提示
使用属性选择器创建自定义工具提示既有趣又简单:
[title] { position: relative; display: block; } [title]:hover:after { content: attr(title); color: hotpink; background-color: slateblue; display: block; padding: .225em .35em; position: absolute; right: -5px; bottom: -5px; }
便捷键
web 的一大优点是它提供了许多不同的信息访问选项。一个很少使用的属性是设置accesskey
的能力,这样就可以通过键组合和accesskey
设置的字母直接访问该项目(确切的键组合取决于浏览器)。但是要想知道网站上设置了哪些键并不是件容易的事
下面的代码将显示这些键:focus
。我不使用鼠标悬停,因为大多数时候需要accesskey
的人是那些使用鼠标有困难的人。你可以将其添加为第二个选项,但要确保它不是惟一的选项。
a[accesskey]:focus:after { content: " AccessKey: " attr(accesskey); }
诊断
这些选项用于帮助我们在构建过程中或在尝试修复问题时在本地识别问题。将这些内容放在我们的生产网站上会使用户产生错误。
没有 controls 属性的 audio
我不经常使用audio
标签,但是当我使用它时,我经常忘记包含controls
属性。 结果:没有显示任何内容。 如果你在 Firefox,如果隐藏了音频元素,或者语法或其他一些问题阻止它出现(仅适用于Firefox),此代码可以帮助你解决问题:
audio:not([controls]) { width: 100px; height: 20px; background-color: chartreuse; display: block; }
没有 alt
文本
没有 alt
文本的图像是可访问性的噩梦。只需查看页面就很难找到它们,但如果添加它们,它们就会弹出来(当页面图片加载失败时,alt文字可以更好的解释图片的作用):
img:not([alt]) { /* no alt attribute */ outline: 2em solid chartreuse; } img[alt=""] { /* alt attribute is blank */ outline: 2em solid cadetblue; }
异步 Javascript 文件
网页可以是内容管理系统和插件,框架和代码的集合,确定哪些JavaScript异步加载以及哪些不加载可以帮助你专注于提高页面性能。
script[src]:not([async]) { display: block; width: 100%; height: 1em; background-color: red; } script:after { content: attr(src); }
JavaScript 事件元素
你可以突出显示具有JavaScript
事件属性的元素,以便将它们重构到JavaScript文件中。这里我主要关注OnMouseOver
属性,但是它适用于任何JavaScript事件属性。
[OnMouseOver] { color: burlywood; } [OnMouseOver]:after { content: "JS: " attr(OnMouseOver); }
隐藏项
如果需要查看隐藏元素或隐藏输入的位置,可以使用它们来显示
[hidden], [type="hidden"] { display: block; }
原文地址:https://www.smashingmagazine.com/2018/10/attribute-selectors-splicing-html-dna-css/
The above is the detailed content of Learn about some CSS attribute selectors that improve front-end development efficiency. For more information, please follow other related articles on the PHP Chinese website!

在css中,可用list-style-type属性来去掉ul的圆点标记,语法为“ul{list-style-type:none}”;list-style-type属性可设置列表项标记的类型,当值为“none”可不定义标记,也可去除已有标记。

区别是:css是层叠样式表单,是将样式信息与网页内容分离的一种标记语言,主要用来设计网页的样式,还可以对网页各元素进行格式化;xml是可扩展标记语言,是一种数据存储语言,用于使用简单的标记描述数据,将文档分成许多部件并对这些部件加以标识。

在css中,可以利用cursor属性实现鼠标隐藏效果,该属性用于定义鼠标指针放在一个元素边界范围内时所用的光标形状,当属性值设置为none时,就可以实现鼠标隐藏效果,语法为“元素{cursor:none}”。

转换方法:1、给英文元素添加“text-transform: uppercase;”样式,可将所有的英文字母都变成大写;2、给英文元素添加“text-transform:capitalize;”样式,可将英文文本中每个单词的首字母变为大写。

在css中,rtl是“right-to-left”的缩写,是从右往左的意思,指的是内联内容从右往左依次排布,是direction属性的一个属性值;该属性规定了文本的方向和书写方向,语法为“元素{direction:rtl}”。

在css中,可以利用“font-style”属性设置i元素不是斜体样式,该属性用于指定文本的字体样式,当属性值设置为“normal”时,会显示元素的标准字体样式,语法为“i元素{font-style:normal}”。

在css3中,可以用“transform-origin”属性设置rotate的旋转中心点,该属性可更改转换元素的位置,第一个参数设置x轴的旋转位置,第二个参数设置y轴旋转位置,语法为“transform-origin:x轴位置 y轴位置”。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
