css empty_何时使用:empty和:blank CSS伪选择器

css empty

I made a terrible mistake when I tweeted about :empty and :blank a while ago. I said that :empty wasn’t useful, and :blank is much more useful than :empty.

不久前我在Twitter上发布:empty:blank时,我犯了一个严重的错误。 我说过:empty没用,而:blank:empty有用得多。

I was wrong!

我错了!

:empty is actually good enough. We don’t even need :blank!

:empty实际上足够好。 我们甚至不需要:blank

快速介绍 (A quick introduction)

Okay, first off, what is :empty and what is :blank?

好吧,首先,什么是:empty ?什么是:blank

:empty is a pseudo selector. It lets you select elements that are empty.

:empty是伪选择器。 它使您可以选择空元素。

/* This is CSS */
:empty { /* do something */}

Empty elements are elements that have nothing in them. It cannot even have a whitespace.

空元素是其中没有任何元素的元素。 它甚至不能有空格。

<!-- This is html -->
<!-- Example of an empty element --><div></div>

Empty elements can have comments though, as long as the comments fill up the entire element.

但是,空元素可以有注释,只要注释填满了整个元素。

<!-- This is html -->
<!-- Empty elements can have comments --><div><!-- this is a comment --></div>

:blank is a powered-up form of :empty. It lets you select elements that have whitespaces in them:

:blank:empty的加电形式。 它使您可以选择其中包含空格的元素:

<!-- This is html -->
<!-- Matched with :blank but not with :empty --><div> </div>

Both :empty and :blank are useful if need to:

如果需要:empty:blank都是有用的:

  1. Style an empty element

    设置空元素的样式
  2. Create empty states

    创建空状态

一个例子 (An example)

Let’s say you have a <div>. You will only fill up this <div> with content when an error occurs.

假设您有一个<d iv>。 仅在发生错误时用内容填充this <div>。

<!-- This is html -->
<!-- Without errors --><div class="error"></div>
<!-- With errors --><div class="error">Whoops! Something went wrong!</div>

Here, you need to style the .error div. If you don’t use :empty, you need to rely on a class or attribute. This feels redundant.

在这里,您需要设置.error div的样式。 如果不使用:empty ,则需要依赖一个类或属性。 感觉很多余。

<!-- This is html -->
<!-- With errors --><div class="error" data-state="error">Whoops! Something went wrong!</div>
/* This is CSS */
.error { display: none; background-color: hsl(0, 20%, 50%); padding: 0.5em 0.75em;}
.error[data-state="error"] { display: block;}

But if you use :empty, you don’t need the extra class or attribute. You can style the .error class directly. You don’t even need display: none;!

但是,如果使用:empty ,则不需要额外的类或属性。 您可以直接设置.error类的样式。 您甚至不需要display: none;

/* This is CSS */
.error { background-color: hsl(0, 20%, 50%); padding: 0.5em 0.75em;}
.error:empty { padding: 0;}

Here’s a codepen Empty Demo I made for you to play with (try removing the padding: 0; from .error:empty, you’ll see a red background ?).

这是我为您制作的一个CodepenEmpty Demo (尝试删除padding: 0;.error:empty删除,您会看到红色背景吗?)。

Let’s say you want to create a todo-list. When your users see the todo-list for the first time, they will probably see zero todo items.

假设您要创建一个待办事项列表。 当您的用户第一次看到待办事项列表时,他们可能会看到零个待办事项。

What do you show when there are zero todos?

当待办事项为零时,您会看到什么?

This zero todo state is what we call an empty state.

零待办状态就是所谓的空状态。

If you want to create an empty state for your todo-list, you can add an extra <div> after your <ul>. When you do so, you can use a combination of :empty and the + (adjacent sibling) or ~ (subsequent sibling) selector to style the empty state.

如果你想为你的待办事项列表为空状态,你可以添加一个额外的<d静脉>之后you [R <ul>。 当你这样做,你可以使用一个COM binati EM:关于p TY和+(相邻s ibling)或〜(后续的兄弟姐妹)选择款式空状态。

<!-- This is html -->
<ul> <li>Item 1</li> <li>Item 2</li> <li>Item 3</li></ul><div class="empty-state"></div>
/* This is CSS */
.empty-state { display: none;}
ul:empty + .empty-state { display: block;}

I learned how to use :empty this way from Heydon Pickering. Check out Heydon’s article on Inclusive Components if you want to see the todo-list example at work.

我从Heydon Pickering学习了如何使用:empty 。 如果您想查看工作中的待办事项列表示例,请查看Heydon关于“ 包含组件” 的文章 。

Note: empty states are important. If you need some convincing, check out this article on Invision.

注意:空状态很重要。 如果您需要说服力,请查看有关Invision的文章 。

分解我的推理 (Taking apart my reasoning)

:empty is often enough in practice. I felt :empty isn’t good enough because of two reasons:

实际上, :empty通常就足够了。 我觉得:empty不够好有两个原因:

  1. Poor developer experience

    糟糕的开发人员经验
  2. I’ll need to trim whitespaces manually with JavaScript

    我需要使用JavaScript手动修剪空格

The first reason is valid, but it isn’t a big deal.

第一个原因是有效的,但这并不重要。

The second reason is not valid. I assumed I had to trim whitespaces, but I don’t need to.

第二个原因无效 。 我以为我必须修剪空白,但是我不需要。

I’ll walk you through both of them.

我将带您浏览他们两个。

Let’s go back to the todo-list example. Say we created a todo-list and we have this markup.

让我们回到待办事项列表示例。 假设我们创建了一个待办事项清单,并且有了这个标记。

<!-- This is html -->
<ul> <li>Item 1</li> <li>Item 2</li> <li>Item 3</li></ul><div class="empty-state"></div>

How would you check if :empty was working?

您将如何检查:empty是否正常工作?

Well, I would remove each <li> with cmd + x. This command cuts the entire line. When I removed all three <li>, I’ll end up with this markup:

好,我将消除每< LI> wic MD + X。 此命令剪切整行。 当我删除所有thre <li>时,我将得到以下标记:

<!-- This is html -->
<ul></ul>

By now, you’ll know that the HTML above won’t trigger :empty. :empty only works when there are no whitespaces in the element.

现在,您将知道上面HTML不会触发:empty:empty仅在元素中没有空格时才有效。

I had to remove the whitespaces for :empty to work, which means a few more keystrokes. This was a chore I hoped I didn’t have to go through.

我必须删除:empty的空白,这意味着需要更多击键。 我希望我不必经历那件事。

But then again, it’s a small problem for a big benefit.

但是话又说回来,这是一个小问题,却带来了很大的好处。

I say it again. You don’t need to trim whitespaces manually in JavaScript if you use :empty. I made a wrong assumption.

我再说一遍。 如果使用:empty无需在JavaScript中手动修剪空格 。 我做错了一个假设。

Let’s go through an example and you’ll see what I mean. We’ll use the todo-list example one more time.

让我们看一个例子,您会明白我的意思。 我们将再使用一次todo-list示例。

Say we have this HTML right now:

假设我们现在有以下HTML:

<!-- This is html -->
<ul> <li>Item 1</li></ul><div class="empty-state"></div>

For the empty state to work, we need to remove the final <li> item from <ul>. If you use plain JavaScript, you can do this with removeChild.

为了使空状态正常工作,我们需要fro <ul>删除最后一个< li>项目。 如果您使用纯JavaScript,则可以o this with removeChild进行此操作。

// This is JavaScript
const ul = document.querySelector('ul')const li = ul.children[0]
ul.removeChild(li)

I believed (erroneously) that removeChild will produce this HTML:

我相信(错误地) removeChild将产生以下HTML:

<!-- This is html -->
<ul></ul>

If it produces this HTML, I’ll have to trim any whitespace remaining in the list (which is extra JavaScript).

如果产生此HTML,则必须修剪列表中剩余的所有空白(这是额外JavaScript)。

// This is JavaScript
const ul = document.querySelector('ul')const li = ul.children[0]
ul.removeChild(li)
if (ul.children.length === 0) { ul.innerHTML = ''}

Like I said, I was wrong. It didn’t produce the above HTML. Instead, this is what it produced:

就像我说的,我错了。 它没有产生上述HTML。 相反,这是它产生的:

<!-- This is html -->
<ul></ul>

Which means we didn’t need the extra JavaScript to trim whitespace!

这意味着我们不需要额外JavaScript来修剪空格!

Disclaimer: I checked the output on Safari, Chrome, and Firefox. I haven’t checked Edge yet though. I’ll be super grateful if you can help me test it out!
免责声明:我检查了Safari,Chrome和Firefox的输出。 我还没有检查Edge。 如果您能帮助我进行测试,我将不胜感激!

Here’s the code for this example:

这是此示例的代码:

See the Pen Empty demo with todolist I made (@zellwk) on CodePen.

请参阅我在CodePen上创建的 todolist ( @zellwk )的PenEmp演示 。

:empty is supported on all browsers, and :blank has poor browser support. This gives you plenty of reason to use :empty over :blank today!

:empty在所有浏览器上都受支持,而:blank对浏览器的支持不佳。 这给了您充足的理由使用:empty over :blank

I hope that browser support for :blank improves one day though.

我希望有一天,浏览器对:blank支持会有所改善。

结语 (Wrapping up)

:empty and :blank let you style empty elements and produce empty states easily.

:empty:blank允许您设置空元素的样式并轻松产生空状态。

:blank is better than :empty because it provides us with a better developer experience. But we can’t use :blank because :blank doesn’t have enough browser support.

:blank:empty更好,因为它为我们提供了更好的开发人员体验。 但我们不能使用:blank因为:blank没有足够的浏览器支持。

:empty is often good enough. You can use it already. Use it all you want! ?

:empty通常足够好。 您已经可以使用它了。 随便使用它吧! ?

Give :empty a go and let me know what you think!

放开:empty走,让我知道您的想法!

Thanks for reading. Did this article help you in any way? If you did, I hope you consider sharing it. You might help someone out. Thank you!

谢谢阅读。 本文对您有任何帮助吗? 如果这样做, 希望您考虑共享它 。 您可能会帮助某人。 谢谢!

This article was originally posted at my blog.

本文最初发布在我的博客上

Sign up for my newsletter if you want more articles to help you become a better frontend developer.

如果您想获得更多文章来帮助您成为更好的前端开发人员,请注册我的时事通讯 。

翻译自: https://www.freecodecamp.org/news/empty-and-blank-53b9e96151cd/

css empty

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/392797.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

浙江大学计算机系统结构,高级计算机体系结构-浙江大学计算机系统结构室.pdf...

高级计算机体系结构-浙江大学计算机系统结构室高级计算机体系结构陈文智 浙江大学计算机学院chenwzzju.edu.cn2014年9月11.1 计算机技术发展综述(1)1946年: 在二次世界大战期间研制成功的世界上第一台电子计算机ENIAC(Electronic Numerical Intergrator andCalculator)正式对…

PVS 6.1 Configuring Services Failed

好久没有更新了&#xff0c;嘿嘿&#xff0c;更新一个。 项目中遇到一个问题&#xff0c;PVS安装到最后一步报错&#xff0c;如下图&#xff1a; 环境&#xff1a;PVS 6.1&#xff0c;数据库是SQL Server 2005 SP4 查了一下文档&#xff0c;PVS 6.1支持SQL Server 2005 SP4 排查…

javascript动态创建table

function createTable(parentNode,headres,datas){//创建表格var table document.createElement("table");//将表格追加到父容器中parentNode.appendChild(table);//设置table的样式table.cellSpacing 0;table.cellPadding 0;table.border "1px";//创建…

leetcode 234. 回文链表(快慢指针+链表倒置)

请判断一个链表是否为回文链表。 示例 1: 输入: 1->2 输出: false 示例 2: 输入: 1->2->2->1 输出: true 代码 /*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode(int x) { val x; }* }*/…

面试小问题——Object中有哪些常用方法?

一、equals方法 Object类中的equals方法用于检测一个对象是否等于另外一个对象。Java语言规范要求equals方法具有下面的特性&#xff1a; &#xff08;1&#xff09;自反性&#xff1a;对于任何非空引用x&#xff0c;x.equals(x)应该返回true &#xff08;2&#xff09;对称性&…

职称计算机证书 评中级职称,软考证书如何申请评职称及职称申请流程的详细介绍...

我们很多考友参加软考。比如信息系统项目管理师和系统集成项目管理工程师考试&#xff0c;目的都是为了评职称&#xff0c;那么在拿到软考证书后&#xff0c;很多人最关心的一个问题就是关于职称评聘问题&#xff0c;今天就以软考证书如何申请评职称及职称申请流程的详细介绍&a…

播客51:妈妈可以编码的创始人埃里卡·彼得森(Erica Peterson)

On todays episode of the freeCodeCamp.org podcast, Abbey Rennemeyer chats with Erica Peterson, a founder, entrepreneur, and mother of two who lives and works in Pittsburg, Pennsylvania.在freeCodeCamp.org播客的今天节目中&#xff0c;Abbey Rennemeyer与Erica P…

leetcode 1024. 视频拼接(dp/贪心)

你将会获得一系列视频片段&#xff0c;这些片段来自于一项持续时长为 T 秒的体育赛事。这些片段可能有所重叠&#xff0c;也可能长度不一。 视频片段 clips[i] 都用区间进行表示&#xff1a;开始于 clips[i][0] 并于 clips[i][1] 结束。我们甚至可以对这些片段自由地再剪辑&am…

java实现时钟方法汇总

import java.awt.Dimension; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.Timer; import java.util.TimerTask;import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; //第一种比较…

js中注册标识符流程

注册分为三个阶段&#xff1a;分别是注册阶段&#xff0c;函数处理阶段&#xff0c;变量处理阶段&#xff1b;这三个阶段有先后顺序的。&#xff08;注&#xff1a;这三个阶段的名字没有权威性&#xff0c;是作者为了方便记忆自己起的名字&#xff09; 注册阶段的特征 1.此时不…

jsp论坛网站模版_网站关键词优化怎么做

说到网站关键词优化&#xff0c;大多企业都很陌生&#xff0c;建站公司说的关键词优化头头是道。跟听天书似的&#xff0c;51商务网小编为大家总结的网站优化方法希望可以帮到大家&#xff0c;首先要说的是做网站优化第一点就是要有耐心&#xff0c;如果很长时间没有收录的话&a…

feature功能_利用feature-u V1释放基于功能的JS开发的强大功能

feature功能This article is an introduction to a new JS library called feature-u, that facilitates feature-based development in your React project.本文是对新的JS库(称为feature-u )的介绍&#xff0c;该库促进了React项目中基于功能的开发 。 Note: On 8/14/2018 f…

虚拟实验工场大学计算机实验报告答案,虚拟实验实验报告 - 实验报告 - 书业网.doc...

虚拟实验实验报告 - 实验报告 - 书业网虚拟实验实验报告 - 实验报告 - 书业网篇一&#xff1a;虚拟实验报告第一章 文献综述1.1 丙酮酸脱氢酶概述丙酮酸脱氢酶复合体(Pyruvate Dehydrogenase Complex)催化丙酮酸不可逆的氧化脱羧转化成乙酰辅酶A。该复合体是糖酵解的关键限速酶…

【笔记】一些linux实用函数技巧【原创】

函数返回的是函数的地址 kallsyms_lookup_name() 本文转自张昺华-sky博客园博客&#xff0c;原文链接&#xff1a;http://www.cnblogs.com/sky-heaven/p/5191491.html&#xff0c;如需转载请自行联系原作者

leetcode 845. 数组中的最长山脉

我们把数组 A 中符合下列属性的任意连续子数组 B 称为 “山脉”&#xff1a; B.length > 3 存在 0 < i < B.length - 1 使得 B[0] < B[1] < … B[i-1] < B[i] > B[i1] > … > B[B.length - 1] &#xff08;注意&#xff1a;B 可以是 A 的任意子数组…

【Lintcode】018.Subsets II

题目&#xff1a; Given a list of numbers that may has duplicate numbers, return all possible subsets Notice Each element in a subset must be in non-descending order.The ordering between two subsets is free.The solution set must not contain duplicate subset…

多线程1

1-1 进程 程序是静止的&#xff0c;运行中的程序就是进程。进程的三个特征&#xff1a; 动态性 &#xff1a; 进程是运行中的程序&#xff0c;要动态的占用内存&#xff0c;CPU和网络等资源。独立性 &#xff1a; 进程与进程之间是相关独立的&#xff0c;彼此有自己的独立内存区…

go 列出已经安装的包_Go 安装教程

一、在 Windows 上安装 Go 环境首先在 Go 官网 下载 Windows 系统下的一键安装包。然后双击打开该文件&#xff0c;一直点 Next 就行。注意这里默认是安装到 C 盘&#xff0c;建议不要修改&#xff0c;因为环境变量会自动设置&#xff0c;如果安装到其他盘&#xff0c;那么可能…

【转】spin_lock、spin_lock_irq、spin_lock_irqsave区别

为什么80%的码农都做不了架构师&#xff1f;>>> 转自&#xff1a;http://blog.csdn.net/luckywang1103/article/details/42083613 void spin_lock(spinlock_t *lock);void spin_lock_irq(spinlock_t *lock);void spin_lock_irqsave(spinlock_t *lock, unsigned lon…

七年级计算机上教学计划,初一教学计划模板锦集5篇

初一教学计划模板锦集5篇时光在流逝&#xff0c;从不停歇&#xff0c;我们又将迎来新的教学工作&#xff0c;我们要好好计划今后的教育教学方法。那么一份同事都拍手称赞的教学计划是什么样的呢&#xff1f;以下是小编为大家整理的初一教学计划5篇&#xff0c;仅供参考&#xf…