搜尋
首頁web前端css教學How I write CSS selectors

How I write CSS selectors

There's many CSS methodologies out there, and I hate them all. Some more (tailwind & al.) and some less (BEM, OOCSS, etc.). But at the end of the day, they all have flaws.

People use these approaches for good reasons, of course, and many of the problems addressed are ones I have also encountered. So in this post, I'd like to write down my own guidelines for how I keep CSS organised.

This isn't a fully described CSS methodology that anyone could just start using. Maybe it could be turned into one with some extra work, but the purpose of this post is simply to show how I make these decisions when writing CSS.

Builtin Elements

As a rule of thumb, I try to use builtin element types as much as possible, and with as little extra fluff as possible.

Having a need for a thousand different types of button is a sign that there might be something wrong with the design on a deeper level, so while in some cases I get the appeal of CSS being inert until framework-specific classes are used, in most cases I see it as ideal when a button is just and looks like a button without any further magic.

div.btton should turn into button

Custom Elements

Not all design elements have a semantically fitting HTML equivalent, and for these cases, I usually resort to custom elements.

I haven't seen many instances of custom element names being used without any accompanying javascript, but it has proven to be a surprisingly solid choice for writing clear HTML that also looks the way I want.

Entirely separate elements in terms of design are also more likely to, over time, develop requirements that can only be implemented with JavaScript, which leaves you with a clear path to implementing those that doesn't need any changes in the HTML nor the CSS.

div.vsep should turn into vertical-separator

Classes

Classes should work as modifiers of the existing node name, rather than entirely new element types, and often have similar but different effects on different element types.

A dangerous button is a button.danger

Attributes

Some ways of modifying elements aren't simple on/off switches that classes are useful for, but behave more like key-value pairs.

In these cases, custom attributes with matching selectors have proven to be the best option almost every time I've used them. Unlike hyphenated classes, they show on a syntax-level which is the attribute and which is the value, making it easier for editors to highlight them, easier for the human eye to quickly parse, and easier to interface using JavaScript.

For those of us who still have hope that the attr() function might one day make its way into CSS for more than just content, this is also an extra layer of future-proofing.

IDs

IDs are, by definition, unique inside the document. As such, any rule targeting a particular ID will be limited and may require refactoring if it later turns out there should be more than one element of this type on the lage after all.

As such, IDs should be used sparingly and only when it makes no sense to ever have more than one element in one document.

The benefits over classes in both a practical and readability sense are rather small, so erring on the side of classes is usually the best idea when no clear 1-to-1 relation between element and styling can be identified.

Inline CSS

Any real-world application will at some point have elements that simply require individual tweaking in order to make them more aesthetically pleasing in the context they appear in.

In these cases, the style attribute is the way to go. Any reason why using it is considered bad practice applies to any sort of inline-styling, including utility classes. The problem isn't the attribute, it's mixing styling and markup.

The one difference between style and class for inlining styles is that one indicates purpose, allows using plain CSS and is mostly universal, while the other isn't.

Put simply, width: 100px has a universally defined meaning, whille .width-100 could mean anything.

Utility Classes

In very rare occasions, element-specific styles get so complex that inlining them explicitly would harm readability, or is even impossible (for example if it requires media queries).

In these cases, utility classes are basically the only option, even if they're ugly.

In an ideal world, these could be handled separately from specific mixin classes, and I have even considered using prefixing to more easily tell them apart, but ultimately haven't found a good way to make these not be ugly.

I like the idea of prefixing utility classes with a + to represent that they add some sort of functionality to the element, in contrast to normal classes, that specify what type of an element is.


And that was about it. Of course, no two projects are the same, and sometimes rules have to be bent a little to remain practical, but overall, that's my framework for deciding how to make a thing on the screen look a certain way.

What are your thoughts? Do you hate it? Do you think it makes sense? Let me know in a comment ?

以上是How I write CSS selectors的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
將框陰影添加到WordPress塊和元素將框陰影添加到WordPress塊和元素Mar 09, 2025 pm 12:53 PM

CSS盒子陰影和輪廓屬性獲得了主題。讓我們查看一些在真實主題中起作用的示例,以及我們必須將這些樣式應用於WordPress塊和元素的選項。

使用GraphQL緩存使用GraphQL緩存Mar 19, 2025 am 09:36 AM

如果您最近開始使用GraphQL或審查了其優點和缺點,那麼您毫無疑問聽到了諸如“ GraphQl不支持緩存”或

使您的第一個自定義苗條過渡使您的第一個自定義苗條過渡Mar 15, 2025 am 11:08 AM

Svelte Transition API提供了一種使組件輸入或離開文檔(包括自定義Svelte Transitions)時動畫組件的方法。

優雅且酷的自定義CSS捲軸:展示櫃優雅且酷的自定義CSS捲軸:展示櫃Mar 10, 2025 am 11:37 AM

在本文中,我們將深入研究滾動條。我知道,這聽起來並不魅力,但請相信我,一個精心設計的頁面是齊頭並進的

展示,不要說展示,不要說Mar 16, 2025 am 11:49 AM

您花多少時間為網站設計內容演示文稿?當您撰寫新的博客文章或創建新頁面時,您是在考慮

使用Redwood.js和Fauna構建以太坊應用使用Redwood.js和Fauna構建以太坊應用Mar 28, 2025 am 09:18 AM

隨著最近比特幣價格超過20k美元的攀升,最近打破了3萬美元,我認為值得深入研究創建以太坊

NPM命令是什麼?NPM命令是什麼?Mar 15, 2025 am 11:36 AM

NPM命令為您運行各種任務,無論是一次性或連續運行的過程,例如啟動服務器或編譯代碼。

讓我們使用(x,x,x,x)來談論特殊性讓我們使用(x,x,x,x)來談論特殊性Mar 24, 2025 am 10:37 AM

前幾天我只是和埃里克·邁耶(Eric Meyer)聊天,我想起了我成長時代的埃里克·邁耶(Eric Meyer)的故事。我寫了一篇有關CSS特異性的博客文章,以及

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器

MantisBT

MantisBT

Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

mPDF

mPDF

mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)