css后代选择器:nth

CSS writers are the blind swordsmen of web development: we pen declarations and throw them at HTML documents hoping that something might stick, but rarely are we aware of the specific content of a selected element: the question of how many words are in a paragraph, or if it contains any words at all, has traditionally been the responsibility of JavaScript, PHP, and other languages.

CSS编写者是Web开发的盲目剑客:我们写一些声明,然后将它们扔到HTML文档上,希望可能会留下一些痕迹,但我们很少意识到所选元素的具体内容 :一个段落中有多少个单词的问题,或者如果它根本不包含任何单词,传统上就是JavaScript , PHP和其他语言的责任。

Very often server-side languages will be employed to fire dynamic content into HTML containers on a page. When such an operation fails, front-end developers deserve equal time to address the issue: rather than trying to code around a load problem using only JavaScript and PHP, we can add CSS to style the empty containers.

通常,将使用服务器端语言将动态内容发射到页面上HTML容器中。 当这样的操作失败时,前端开发人员应有同等的时间来解决该问题:我们可以添加CSS来为空容器设置样式,而不是尝试仅使用JavaScript和PHP来解决负载问题。

处理空细胞的奥秘 (Treating the Mystery of the Empty Cell)

Frequently tables are filled with dynamic data, but some cells in the table may have missing information. Usually, these cells are just left blank, but you might want to emphasize their lack of content in other ways.

通常, 表中填充有动态数据,但是表中的某些单元格可能缺少信息。 通常,这些单元格只是空白,但您可能想以其他方式强调它们缺少内容。

A good example might be a table that shows the distances between cities. Naturally, there will be no mileage information between a city and itself, creating a series of empty cells.

一个很好的例子可能是一张显示城市之间距离的表格。 自然,城市与城市之间将不会有里程信息,从而会创建一系列的空单元。

Distances Between Cities On The Pacific Rim (miles)
Auckland Papeete Los Angeles
Auckland 2542 6518
Papeete 2542 4114
Los Angeles 6518 4114
环太平洋城市之间的距离(英里)
奥克兰市 帕皮提 洛杉矶
奥克兰市 2542 6518
帕皮提 2542 4114
洛杉矶 6518 4114

The markup for the table is as follows (I’m using HTML5 shortcuts for speed):

该表的标记如下(我使用HTML5快捷键来提高速度):

<table>
<caption>Distances Between Cities On The Pacific Rim (miles)</caption>
<col><col><col><col>
<tr><th><th scope=col>Auckland<th scope=col>Papeete<th scope=col>Los Angeles
<tr><th scope=row>Auckland<td><td>2542<td>6518
<tr><th scope=row>Papeete<td>2542<td><td>4114
<tr><th scope=row>Los Angeles<td>6518<td>4114<td>
</table>

This is a good use-case for :empty as the vacant cells must be included in order for the table to be valid and present well.  With :empty, targeting the cells that lack content is easy:

这对于:empty是一个很好的用例,因为必须包含空单元格才能使表有效并正确显示。 使用:empty ,轻松定位缺少内容的单元格:

td:empty { background: #777; }

Note the structure of the code at the very end of the table, with the closing </table> tag right next to the last, empty <td> element. If that was not the case, the sole <td> tag would be treated as being “open” and not empty. (An alternative approach would be an opening and closing <td></td> with no space between them).

注意表末尾的代码结构,最后一个空的<td>元素旁边是</table>标记。 如果不是这种情况,唯一的<td>标签将被视为“打开”而不是空的。 (另一种方法是打开和关闭<td></td>之间没有空格)。

While odd, it should also be noted that you can combine the :empty and :not selectors to style cells that are filled:

虽然很奇怪,但也应注意,您可以结合使用:empty:not选择器来设置填充单元格的样式:

td:not(:empty) { /* styles for filled cells */ }

This would be an unusual approach, as our assumption is that the majority of cells have content and can be addressed with a simple td selector, but it is still a valid approach in CSS.

这将是一种不寻常的方法,因为我们的假设是大多数单元格都有内容,并且可以使用简单的td选择器进行寻址,但这在CSS中仍然是有效的方法。

修复缺失的环节 (Rehabilitating The Missing Link)

Navigation for a site is often dynamically generated, sometimes incompletely. It’s possible to have space reserved for a link that never appears, or does so only fitfully. While this usually implies that your backend developers need to do more work, there’s a simple CSS solution that will make site navigation appear less like a gap-toothed smile in the interim:

网站导航通常是动态生成的,有时是不完整的。 可以为从未出现或仅适当出现的链接保留空间。 尽管这通常意味着您的后端开发人员需要做更多的工作,但是有一个简单CSS解决方案可以使网站导航在过渡期间看起来像齿隙般的微笑:

nav[role="navigation"] a:empty { display: none; pointer-events: none; }

This means that a link with an href attribute value but no content will not be rendered in the browser. So taking this output HTML:

这意味着具有href属性值但没有内容的链接将不会在浏览器中呈现。 因此,采用以下输出HTML:

<nav role="navigation">
<a href="index.html">Home</a>
<a href="contact.html">Contact</a>
<a href="tools.html">Tools</a>
<a href="classes.html"></a>
</nav>

… and adding the CSS above, will result in the last, empty tag not appearing. (In this case pointer-events is somewhat redundant, as the link won’t appear at all, and can’t be clicked in any case, but it’s a useful backup technique).

…并在上面添加CSS,将导致最后一个空标签不显示。 (在这种情况下, pointer-events有点多余,因为链接根本不会出现,在任何情况下都无法单击,但这是一种有用的备份技术)。

:empty异常 (:empty Exceptions)

Note that whitespace between an opening and closing tag counts as character information, as do any tags inside the targeted element. So the following is not considered an empty element:

请注意,开始和结束标签之间的空白与目标元素内的所有标签一样,都视为字符信息 。 因此以下内容视为空元素:

<a href="classes.html"> </a>

Neither is this:

这也不是:

<a href="classes.html"><span></span></a>

As mentioned above, tags that are not closed, even if doing so is optional in HTML, do not count as empty, even if they have no content. A single paragraph is “open”, and therefore not empty:

如上所述,即使未关闭的标记在HTML中是可选的,即使它们没有内容,也不会算作空。 单个段落是“开放”的,因此不能为空

<p>

Although such a tag with no carriage return between it and the next one would be:

尽管这样的标签与下一个标签之间没有回车符,但它可能是:

<p><p>

In the example above, the first paragraph would be empty, but the second (assuming nothing else came immediately after it) would not be.

在上面的示例中,第一段为空,但是第二段(假定紧随其后没有其他内容)将为空。

“Self-closed” elements are counted as being empty: <br>, horizontal rules, <img>, etc will respond to :empty.

“自动关闭”元素认为是空的: <br> , 水平尺 , <img>等将响应:empty

Elements are counted as being empty if comments are their sole content:

元素算作是空的,如果评论是他们唯一的内容:

<p><!--this paragraph is empty --></p>

使用:empty作为代码的可视化质量检查测试 (Using :empty as a visual QA test for code)

Inevitably, some developers get lazy. “Hey, I need more space under this element. I know, I’ll just add an empty paragraph".

不可避免地,一些开发人员会变得懒惰。 “嘿,我需要在此元素下留出更多空间。 我知道,我将添加一个空的段落”。

<p>Some actual content…
<p></p>

Or, even worse, a <br> tag. This empty, “filler" markup gets in the way of well-written CSS, while being notoriously difficult to track down. We can use :empty as a quick visual check of pages for vestigial markup:

甚至更糟的是<br>标签。 这个空的,“填充”的标记妨碍了编写良好CSS的方式,但是众所周知,要追踪它非常困难,我们可以使用:empty来快速直观地检查页面的残留标记:

*:empty, br { border: 2px solid red; }

浏览器支持和结论 (Browser support & conclusion)

:empty has very good support: every modern browser, including IE9+, recognizes the selector.

:empty有很好的支持:每个现代的浏览器,包括IE9 +,都可以识别选择器。

There are many more possibilities for :empty: you’ll see one next month, in the redesign of this blog.

:empty还有更多可能性:empty下个月,在重新设计此博客时,您将看到一个。

翻译自: https://thenewcode.com/692/Vanishing-Acts-The-CSS-empty-Selector

css后代选择器:nth

css后代选择器:nth_消失的行为:CSS:空选择器相关推荐

  1. CSS基础知识汇总{主要内容: CSS语法结构,类选择器,后代选择器,交集选择器等}

    1.CSS的作用: 通过css达到美化网页的效果 2.选择器: ✔ 基础选择器(重点) ◆标签选择器(重点学习) ◆类选择器(重点学习) ◆ID选择器 ◆通配符选择器 ✔ 复合选择器(重点) ◆后代选 ...

  2. css后代选择器:nth_CSS选择器:特异性

    css后代选择器:nth The following is an extract from our book, CSS Master, written by Tiffany B. Brown. Cop ...

  3. html连在一起的选择器,请问关于css选择器的问题:两个类选择器连在一起是什么意思?...

    CSS 规则由两个主要的部分构成:选择器,以及一条或多条声明. 选择器包括单独的选择和分组选择. 如:h1 { color:red;} 单独选择器 定义h1标签的文字颜色 h1, h2, h3, h4 ...

  4. apache伪静态把css 排除掉_(02)CSS 选择器详解 | CSS

    原创:itsOli @前端一万小时 本文版权归作者所有,未经授权,请勿转载! 本文节选自"语雀"私有付费专栏「前端一万小时 | 从零基础到轻松就业」 1. 伪类选择器有哪些?2. ...

  5. 0基础快速入门CSS技术栈(4)—图解详细阐述CSS的复合选择器、标签显示模式、行高、CSS背景,及最为重要的CSS三大特性附带权重计算笔试题(附详细案例源码解析过程)

    文章目录 1. 0基础快速入门CSS技术栈(4) 2. 重点提炼 3. CSS复合选择器 3.1 后代选择器(重点) 3.1.1 example01 3.2 子元素选择器 3.2.1 exmaple0 ...

  6. CSS3 属性选择器 伪类选择器 盒模型 圆角 阴影 CSS定位和浮动

    HTML是网页的裸框架,但是现在已经是2020年了,你再做出一个80年代的网页来,恐怕是没HR要你了.所以学习CSS的重要性可想而知,CSS用途很广,是一门独立的编程语言,能美化网页,也能进一步提高自 ...

  7. css 选择器 伪元素_CSS伪元素-解释选择器之前和之后

    css 选择器 伪元素 选择器之前 (Before Selector) The CSS ::before selector can be used to insert content before t ...

  8. html之CSS设计(四种引入方式、各种选择器)

    文章目录 一.CSS简介 二.四种引入方式 三.CSS选择器 四.练习代码 本文主要介绍一下CSS的基本操作,四中引入方式和选择器.属性选择器的使用 一.CSS简介 1.介绍: 也叫层叠样式表,用来控 ...

  9. CSS/HTML 5简洁带图标的input日期选择器

    CSS/HTML 5简洁带图标的input日期选择器 说明 css代码 html代码 演示效果图 说明 演示效果分辨率为1980x1080分辨率效果 高度可定制修改 纯css代码控制 使用的input ...

最新文章

  1. tensorboard merge报错_什么是TensorBoard?
  2. XWiki 4.3 正式版发布
  3. C语言高级编程:深入理解const
  4. cvCalcBackProjectPatch() 基于块的反向投影
  5. 计算机485通讯原理,用RS-485设计的多机通信接口电路
  6. java在线播放_Java实现视频在线播放flv视频
  7. c语言笔试题 选择题,C语言笔试选择题
  8. MySQL基础命令汇总
  9. 1人30天44587行代码,分享舍得网开发经验
  10. 2022网易校招易计划在线课程
  11. 湖南大学ACM程序设计新生杯大赛(同步赛)L-Liao Han【打表规律+二分】
  12. 质因子分解 Python
  13. postgresSQL的FDE加密
  14. Qiime2+Origin绘制稀释曲线
  15. 小米电脑重装系统后亮度无法调节的解决办法
  16. 用图灵机器人2.0实现聊天机器人
  17. 企业无线局域网,买AP一定需要买AC控制器吗?还有一定要买AP授权吗?
  18. 基于ADI-DSP-1452外挂flash模拟声浪功能调试
  19. java截取字符串中间一部分内容
  20. 学习委员(班长、团支书)查作业软件,查人+重命名

热门文章

  1. Nginx配置upstream
  2. 取其精华,弃其糟粕---拿来主义
  3. 启用win10自动登录
  4. excel中同一单元格内容分隔到不同的单元格
  5. 据说可以让男人看一遍就哭的文章
  6. 微信需要什么软件打开html,微信调试模式怎么打开?在线调试微信打开的HTML5页面...
  7. 关于msvcp71.dll文件出错的解决办法
  8. 投稿外文期刊,如何引用中文文献?
  9. 行为树(Behavior Tree)实践(2)– 进一步的讨论
  10. C# Microsoft.Office库播放PPT实时获取当前页号