css擴展語言_如何決定是否應該鏈接或擴展CSS類

css擴展語言

by Sarah Dayan

通過莎拉·達揚

如何決定是否應該鏈接或擴展CSS類 (How to decide whether you should chain or extend CSS classes)

If you’re building an app or a website that changes often, modular CSS methods solve many issues. Instead of copying your HTML structure in CSS and decorating it, you create consumable libraries of components. This makes projects more scalable and keeps the CSS codebase under control.

如果您要構建經常更改的應用程序或網站,則模塊化CSS方法可以解決許多問題。 您無需創建CSSHTML結構并對其進行修飾,而是創建可使用的組件庫。 這使項目更具可伸縮性,并使CSS代碼庫處于受控狀態。

CSS modularity relies on composition, which inevitably fattens the HTML. This collateral effect can be a significant deterrent for many people because of the “bloat” it creates.

CSS模塊化依賴于組合,這不可避免地會使HTML泛濫。 由于這種附帶效應會造成“膨脹”,因此對許多人來說可能是很大的威懾力。

In this article, we’ll compare two techniques: chaining and extending. We’ll see what they provide and what their shortcomings are so that you can make more informed choices.

在本文中,我們將比較兩種技術: 鏈接擴展 。 我們將看到它們提供的功能以及它們的缺點是什么,以便您可以做出更明智的選擇。

鏈式 (Chaining)

Chaining CSS classes means composing the desired look by adding granular modifiers together onto an HTML selector. The composite styles create the final visual outcome. This is the default behavior with most modular CSS methodologies.

鏈接CSS類意味著通過將粒度修飾符一起添加到HTML選擇器上來組成所需的外觀 。 復合樣式創建最終的視覺效果。 這是大多數模塊化CSS方法的默認行為。

Let’s take the following OOCSS code for a button:

讓我們為按鈕使用以下OOCSS代碼:

.btn {  display: block;  box-shadow: 0 0 5px 0 rgba(0, 0, 0, .2);}.btn-default {  border: 3px solid grey;}.btn-primary {  background: purple; color: white;}

If you were to chain modifiers, your HTML would look like this:

如果要鏈接修飾符,則HTML如下所示:

<button class="btn btn-primary">Primary button</button><button class="btn btn-default">Default button</button>

Now let’s do something a bit more complex, this time with BEM (block, element, modifier):

現在,讓我們用BEM(塊,元素,修飾符)做一些更復雜的事情:

<div class="media-object media-object--reverse media-object--outlined">  <div class="media-object__media">    <img class="media-object__img media-object__img--faded img img--square" src="..." alt="...">  </div>  <div class="media-object__body">...</div></div>

Here we have a lot more interacting classes:

在這里,我們還有更多的交互類:

  • The .media-object block has several modifiers (.media-object--reverse and .media-object--outlined).

    .media-object塊具有多個修飾符( .media-object--reverse.media-object--outlined )。

  • The .media-object__img element has one modifier (.media-object__img--faded).

    .media-object__img元素具有一個修飾符( .media-object__img--faded )。

  • The .media-object__img element is also an .img block with its own modifier (.img--square).

    .media-object__img元素也是具有自己的修飾符( .img--square )的.img塊。

優點 (Pros)

The top highlight of chaining classes is separate responsibility. It keeps your CSS codebase clean, light, comfortable to read, and non-repetitive. What each class does is crystal clear, and you immediately know what you should use and what you shouldn’t.

鏈接類的最高亮點是單獨的責任 。 它可使您CSS代碼庫保持整潔,輕巧,易于閱讀且不重復。 每個班級的工作都非常清楚,您立即知道應該使用什么和不應該使用什么。

It also prevents dead code: since you’re dealing with building blocks, everything is potentially useful. When you remove a component, you only need to remove the HTML.

它還可以防止代碼失效:由于您正在處理構建基塊,因此一切都可能有用。 刪除組件時,只需刪除HTML。

Separate modifiers are great to represent state. Thus it makes life easier for JavaScript engineers. All they have to do is add and remove classes.

單獨的修飾符可以很好地表示狀態。 因此,它使JavaScript工程師的工作變得更加輕松。 他們要做的就是添加和刪除類。

On large projects, this method can save you a lot of time.

在大型項目中, 此方法可以節省大量時間

缺點 (Cons)

One of the most common issues people have with modular CSS is that it creates “class madness” in the HTML. Strictly speaking, this is true.

人們使用模塊化CSS時最常見的問題之一是,它在HTML中造成了“類瘋狂”。 嚴格來說,這是真的。

Design patterns that split responsibilities almost always result in more files and verbose code. CSS is no exception: if you pick a method that’s supposed to make your codebase more maintainable, the counterpart is lengthy HTML files.

劃分職責的設計模式幾乎總是會導致更多文件和冗長的代碼。 CSS也不例外: 如果您選擇一種應該使您的代碼庫更具可維護性的方法,則對應的方法將是冗長HTML文件

Having to type so much code is becoming less and less of a problem these days, as most editors and IDEs offer powerful autocompletion. But now, it’s still more code to write every time you make a new page or compose a new component. Over time, this can induce a feeling of clutter and redundancy that will put off some developers.

由于大多數編輯器和IDE提供了強大的自動補全功能,因此如今不得不鍵入那么多的代碼越來越成為一個問題。 但是現在,每次創建新頁面或組成新組件時,仍然需要編寫更多代碼。 隨著時間的流逝,這可能會導致混亂和冗余的感覺,從而使某些開發人員望而卻步。

延伸 (Extending)

If you don’t want to chain classes, you can extend them. We still have the same separate blocks, but instead of chaining them in the HTML, we inherit the properties of the base class to its modifiers. This way, we can use them all at once.

如果您不想鏈接類,則可以擴展它們。 我們仍然具有相同的獨立塊,但是我們沒有將基本塊的屬性鏈接到HTML中, 而是將基類的屬性繼承為其修飾符 。 這樣,我們可以一次全部使用它們。

Let’s use the @extend function in Sass to do so:

讓我們在Sass中使用@extend函數:

.btn {  display: block;  box-shadow: 0 0 5px 0 rgba(0, 0, 0, .2);  &-default {    @extend .btn;    border: 3px solid grey;  }  &-primary {    @extend .btn;    background: purple;    color: white;  }}

This will turn into the following CSS snippet:

這將變成以下CSS代碼段:

.btn,.btn-default,.btn-primary {  display: block;  box-shadow: 0 0 5px 0 rgba(0, 0, 0, .2);}.btn-default {  border: 3px solid grey;}.btn-primary {  background: purple; color: white;}

With the above CSS, our HTML would look like this:

使用上述CSS,我們HTML看起來像這樣:

<button class="btn-primary">Primary button</button><button class="btn-default">Default button</button>

Instead of having a slew of seemingly repetitive classes, we only have one. It has an explicit name and keeps the code readable. We can still use .btn alone, but if we need a variation of it, we only need to append the modifier part on it instead of chaining a new class.

我們沒有一堆看似重復的類,而只有一類。 它有一個明確的名稱,并使代碼可讀。 我們仍然可以單獨使用.btn ,但是如果我們需要它的一個變體,我們只需要在其上附加修飾符部分,而不是鏈接一個新類即可。

優點 (Pros)

The highlight of this method is a clutter-free, more readable, and lighter HTML. When you go for modular CSS, you also decide to do more HTML and less CSS. The CSS becomes a library instead of a list of instructions. Thus, you spend more time in the HTML, which is why you may want to keep it light and easy to read.

該方法的重點是無雜亂,更易讀和更輕巧HTML。 當您使用模塊化CSS時,您還決定使用更多HTML而減少使用CSS。 CSS變為庫,而不是說明列表。 因此,您在HTML上花費了更多的時間,這就是為什么您可能希望使其保持簡潔和易于閱讀的原因。

缺點 (Cons)

Your CSS may look DRY, especially if you’re using a pre-processor, but extending classes results in a much heavier CSS file. Plus, you don’t have much control over what happens: every time you use @extend, the class definition is moved to the top and added to a list of selectors sharing the same ruleset. This process can result in weird style overrides and a lot more generated code.

您CSS 看起來很干,尤其是在使用預處理器的情況下,但擴展類會導致CSS文件重得多 。 另外,您對發生的事情沒有太多控制:每次使用@extend ,類定義都移到頂部,并添加到共享同一規則集的選擇器列表中。 此過程可能導致怪異的樣式覆蓋和更多的生成代碼。

There’s also the case of wanting to use several modifiers together. With the extend method, you don’t compose in the HTML anymore. You’re left with one solution if you’re going to create new combinations: create even more classes by extending modifiers. This is hard to maintain and results in more code. Every time you need to blend classes, you’ll need to edit the CSS and create a potentially non-reusable new rule. If you ever remove the HTML that uses it, you’ll also have to delete the CSS class.

也有需要一起使用多個修飾符的情況。 使用extend方法,您不再需要在HTML中編寫代碼。 如果要創建新的組合,則剩下一個解決方案:通過擴展修飾符創建更多類。 這很難維護,并導致更多代碼。 每次需要混合類時,都需要編輯CSS并創建可能不可重用的新規則。 如果刪除了使用HTMLHTML,則還必須刪除CSS類。

事后思考 (Afterthoughts)

Modular CSS comes at the price of more verbose HTML, but it’s not much to pay for all the benefits it provides. If you’ve already determined you need modularity, don’t shoot yourself in the foot by using incompatible practices. It will result in more work for half the benefits. Inheritance is tempting, but composition has more than once been recognized as a far better strategy.

模塊化CSS的代價是更冗長HTML,但要付出其提供的所有好處并不多。 如果您已經確定需要模塊化,請不要通過使用不兼容的做法而陷入困境。 這將導致工作量增加,但收益卻減半。 繼承是很誘人的,但是不止一次地將組合視為一種更好的策略 。

HTML “bloat” is not that big of a deal when you look at its actual impact. Modularity inevitably creates more code — the method you pick only determines where it goes. From a performance standpoint, more HTML is far better than more CSS.

當您查看HTML的實際影響時,HTML的“膨脹”并不是那么重要。 模塊化必然造成更多的代碼-你只挑方法確定哪里就有奇跡。 從性能的角度來看, 更多HTML比更多CSS更好 。

Don’t focus on small things that don’t matter. Instead, leverage tools that help you write and navigate code more efficiently. Try to look at the big picture and make choices based on facts, not personal preferences.

不要專注于無關緊要的小事情。 相反,請利用可幫助您更有效地編寫和導航代碼的工具。 嘗試著眼全局,根據事實而不是個人喜好做出選擇。

Originally published at frontstuff.io.

最初發布在frontstuff.io上 。

翻譯自: https://www.freecodecamp.org/news/how-to-decide-whether-you-should-chain-or-extend-css-classes-a8e17d7a7b0b/

css擴展語言

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/394075.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/394075.shtml
英文地址,請注明出處:http://en.pswp.cn/news/394075.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

vue 是否有word編輯控件_GitHub - C84882428/editor-ui: vue集成 tinymce 富文本編輯器,增加導入 word 模板...

editor-uivue 集成 tinymce 富文本編輯器自定義 tinymce 富文本編輯器,在原來的編輯器中增加上傳 word 模板最終展示效果:主要代碼:整體思路:1,在編輯器原來的基礎上增加上傳模板按鈕2, 前端上傳 word 模板3, 服務端接收將 word 轉換為html 返回前端4, 前端拿到服務端返回的值,…

Android開發系列之屏幕密度和單位轉換

由于Android的開源性&#xff0c;所以目前市面上面Android手機的分辨率特別多&#xff0c;這樣的話就給我適配帶來了一定的難度。要想做好適配&#xff0c;我們首先應該明白什么是分辨率、PPI、屏幕大小等概念&#xff0c;還有在不同的屏幕密度下&#xff0c;各個單位之間的轉換…

java集合AbstractMap_Java 集合中的 AbstractMap 抽象類

Java 集合中的 AbstractMap 抽象類jdk1.8.0_144AbstractMap 抽象類實現了一些簡單且通用的方法, 本身并不難但在這個抽象類中有兩個方法非常值得關注, keySet 和 values 方法源碼的實現可以說是教科書式的典范抽象類通常作為一種骨架實現, 為各自子類實現公共的方法上一篇我們講…

leetcode392. 判斷子序列(動態規劃)

給定字符串 s 和 t &#xff0c;判斷 s 是否為 t 的子序列。 你可以認為 s 和 t 中僅包含英文小寫字母。字符串 t 可能會很長&#xff08;長度 ~ 500,000&#xff09;&#xff0c;而 s 是個短字符串&#xff08;長度 <100&#xff09;。 字符串的一個子序列是原始字符串刪…

讓機器讀懂用戶——大數據中的用戶畫像

讓機器讀懂用戶——大數據中的用戶畫像 摘要&#xff1a; 用戶畫像(persona)的概念最早由交互設計之父Alan Cooper提出:“Personas are a concrete representation of target users.” 是指真實用戶的虛擬代表&#xff0c;是建立在一系列屬性數據之上的目標用戶模型。隨著互聯…

asp.net應用程序_如何在ASP.NET中為聊天應用程序構建鍵入指示器

asp.net應用程序by Neo Ighodaro由新Ighodaro 如何在ASP.NET中為聊天應用程序構建鍵入指示器 (How to build a typing indicator for your chat app in ASP.NET) A basic understanding of ASP.NET and jQuery is needed to follow this tutorial.要學習本教程&#xff0c;需要…

activeMQ在文件上傳的應用

本次試驗主要用到了activeMq和上傳插件uploadify的知識&#xff0c;感謝以下兩篇文章的作者。 1.http://itindex.net/detail/47160-java-jquery-%E4%B8%8A%E4%BC%A0 2.http://blog.csdn.net/jiuqiyuliang/article/details/47160259 本文中不再提供activeMq和uploadify的介紹。 …

java nginx 例子_Java及nginx實現文件權限控制代碼實例

我們知道&#xff0c;使用nginx作為文件下載服務器&#xff0c;可以極大地降低對后端Java服務器的負載沖擊&#xff0c;但是nginx本身并不提供授權控制&#xff0c;因此好的方案是由后端服務器實現權限控制&#xff0c;最好的方式是直接復用應用的認證體系&#xff0c;最大化的…

leetcode934. 最短的橋(dfs+bfs)

在給定的二維二進制數組 A 中&#xff0c;存在兩座島。&#xff08;島是由四面相連的 1 形成的一個最大組。&#xff09; 現在&#xff0c;我們可以將 0 變為 1&#xff0c;以使兩座島連接起來&#xff0c;變成一座島。 返回必須翻轉的 0 的最小數目。&#xff08;可以保證答…

謝煙客---------Linux之DNS服務系統的基礎知識

DNS Domain Name Server1)C/S架構&#xff1a;SOCKET通信IP PORT2&#xff09;應用層協議&#xff1a;資源子網BIND Berkerley Information Name DomainDNS由來1&#xff09;統一名字&#xff0c;自己維護 <自己查詢>解析: 基于key查找value: 查詢數據庫(二維關系的表: …

Java實現點擊導出excel頁面遮罩屏蔽,下載完成后解除遮罩

一、問題場景 最近在做數據統計功能&#xff0c;需求是導出大數據量的excel&#xff0c;時間間隔較長&#xff0c;大概需要十秒左右&#xff0c;點擊導出后&#xff0c;頁面沒有做任何處理&#xff0c;用戶也不知道是否正在導出&#xff1b;如果沒有做交互上的限制&#xff0c;…

pbs 支持 java_Linux下Java安裝與配置

安裝以JDK1.6.0_43為例下載jdk-6u43-linux-x64.bin&#xff0c;http://www.oracle.com/technetwork/java/javase/downloads/index.html增加可執行權限 chmod x jdk-6u43-linux-x64.bin&#xff0c;執行 ./jdk-6u43-linux-x64.bin 生成目錄jdk1.6.0_43拷貝到/usr/share下&#x…

使用Chatkit構建Node.js命令行聊天應用程序

by Hugo雨果 使用Chatkit構建Node.js命令行聊天應用程序 (Build a Node.js command-line chat application with Chatkit) Building chat in your app can be pretty complex. Yet, with Chatkit, implementing fully-featured chat is nothing but a few lines of code.在您的…

java 毫秒轉分鐘和秒_Java程序將毫秒轉換為分鐘和秒

Java程序將毫秒轉換為分鐘和秒在上面的程序中&#xff0c;您將學習如何在Java中將毫秒分別轉換為分鐘和秒。示例1&#xff1a;將毫秒分別轉換為分鐘和秒import java.util.concurrent.TimeUnit;public class Milliseconds {public static void main(String[] args) {long millis…

Andrew Ng機器學習之一 導論

監督學習與無監督學習 監督學習&#xff08;Supervised Learning) Ng的原文是&#xff1a; We gave the algorithm a data set that the "right answers" were given. 即給定了一個正確結果的集合供算法學習&#xff0c;強調了需要實現準備好正負樣本喂給機器。 無監…

leetcode994. 腐爛的橘子(bfs)

在給定的網格中&#xff0c;每個單元格可以有以下三個值之一&#xff1a; 值 0 代表空單元格&#xff1b; 值 1 代表新鮮橘子&#xff1b; 值 2 代表腐爛的橘子。 每分鐘&#xff0c;任何與腐爛的橘子&#xff08;在 4 個正方向上&#xff09;相鄰的新鮮橘子都會腐爛。 返回直…

ES6對象的擴展

1.屬性簡寫表示 2.方法簡寫表示 屬性與方法簡寫&#xff1a; 3.屬性名表達式 ES6允許字面量定義對象時&#xff0c;用方法二&#xff08;表達式&#xff09;作為對象的屬性名&#xff0c;即把表達式放在方括號內。 4.Object.is()比較兩個值是否嚴格相等 轉載于:https://www.cnb…

Spring Cloud項目MVN編譯 -- Non-resolvable import POM

最近利用閑余時間&#xff0c;打算搭建一套基于Spring Cloud G版的微服務架構(Spring boot 2.1.0)&#xff0c;一頓操作之后,IDEA也沒有提示什么錯誤,自認為微服務搭建完畢。啟動項目前&#xff0c;習慣性的Maven -clean了一下&#xff0c;我去&#xff0c;IDEA里面的Maven Pro…

datax底層原理_Datax 插件加載原理

Datax 插件加載原理插件類型Datax有好幾種類型的插件&#xff0c;每個插件都有不同的作用。reader&#xff0c; 讀插件。Reader就是屬于這種類型的writer&#xff0c; 寫插件。Writer就是屬于這種類型的transformer&#xff0c; 目前還未知handler&#xff0c; 主要用于任務執行…

mysql windows身份驗證_SQL Server 2005 怎么就不能用Windows身份驗證方式登錄呢?

SQL Server 2005 自從裝到我的電腦上始終無法使用Windows身份驗證的方式登錄,由于使用用戶名和密碼登錄還算順暢,所以一直忽略了這SQL Server 2005 自從裝到我的電腦上始終無法使用Windows身份驗證的方式登錄,由于使用用戶名和密碼登錄還算順暢,所以一直忽略了這個問題,直到又有…