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.pswp.cn/news/392797.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/392797.shtml
英文地址,請注明出處:http://en.pswp.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…