dropbox/css-style-guide — 1.2k★ trên GitHub. Dropbox’s (S)CSS authoring style guide
Tóm tắt dựng từ metadata GitHub của chính dự án — chưa có bài review TopGit. Trang sẽ tự động cập nhật khi bài review đầy đủ được xuất bản.
VÌ SAO CHƯA CÓ REVIEW
TopGit viết bài đầy đủ cho repo có nhiều sao nhất và được yêu cầu nhiều nhất. Trang này là snapshot trong thời gian chờ — xem README gốc ở tab READ ME.
“Every line of code should appear to be written by a single person, no matter the number of contributors.” —@mdo
General
Don’ts
Avoid using HTML tags in CSS selectors
E.g. Prefer .o-modal {} over div.o-modal {}
Always prefer using a class over HTML tags (with some exceptions like CSS resets)
Don't use ids in selectors
#header is overly specific compared to, for example .header and is much harder to override
Read more about the headaches associated with IDs in CSS here.
Don’t nest more than 3 levels deep
Nesting selectors increases specificity, meaning that overriding any CSS set therein needs to be targeted with an even more specific selector. This quickly becomes a significant maintenance issue.
Avoid using nesting for anything other than pseudo selectors and state selectors.
E.g. nesting :hover, :focus, ::before, etc. is OK, but nesting selectors inside selectors should be avoided.
Don't !important
Ever.
If you must, leave a comment, and prioritise resolving specificity issues before resorting to !important.
!important greatly increases the power of a CSS declaration, making it extremely tough to override in the future. It’s only possible to override with another !important declaration later in the cascade.
Don’t use margin-top.
Vertical margins collapse. Always prefer padding-top ormargin-bottom on preceding elements
Avoid shorthand properties (unless you really need them)
It can be tempting to use, for instance, background: #fff instead of background-color: #fff, but doing so overrides other values encapsulated by the shorthand property. (In this case, background-image and its associative properties are set to “none.”
This applies to all properties with a shorthand: border, margin, padding, font, etc.
Spacing
Four spaces for indenting code
Put spaces after : in property declarations
E.g. color: red; instead of color:red;
Put spaces before { in rule declarations
E.g. .o-modal { instead of .o-modal{
Write your CSS one line per property
Add a line break after } closing rule declarations
When grouping selectors, keep individual selectors on a single line
Place closing braces } on a new line
Add a new line at the end of .scss files
Trim excess whitespace
Formatting
All selectors are lower case, hyphen separated aka “spinal case” eg. .my-class-name
Always prefer Sass’s double-slash // commenting, even for block comments
Avoid specifying units for zero values, e.g. margin: 0; instead of margin: 0px;
Always add a semicolon to the end of a property/value declaration
Use leading zeros for decimal values opacity: 0.4; instead of opacity: .4;
Put spaces before and after child selector div > span instead of div>span
Nesting can be really easily avoided by smart class naming (with the help of BEM) and avoiding bare tag selectors.
BEM
Block: Unique, meaningful names for a logical unit of style. Avoid excessive shorthand.
Good: .alert-box or .recents-intro or .button
Bad: .feature or .content or .btn
Element: styles that only apply to children of a block. Elements can also be blocks themselves. Class name is a concatenation of the block name, two underscores and the element name. Examples:
.alert-box__close
.expanding-section__section
Modifier: override or extend the base styles of a block or element with modifier styles. Class name is a concatenation of the block (or element) name, two hyphens and the modifier name. Examples:
.alert-box--success
.expanding-section--expanded
BEM Best practices
Don't @extend block modifiers with the block base.
Good: <div class="my-block my-block--modifier">
Bad: <div class="my-block--modifier">
Don't create elements inside elements. If you find yourself needing this, consider converting your element into a block.
Bad: .alert-box__close__button
Choose your modifiers wisely. These two rules have very different meaning:
Try to use BEM-based naming for your class selectors
When using modifier classes, always require the base/unmodified class is present
Use Sass’s nesting to manage BEM selectors like so:
.block {
&--modifier { // compiles to .block--modifier
text-align: center;
}
&__element { // compiles to .block__element
color: red;
&--modifier { // compiles to .block__element--modifier
color: blue;
}
}
}
Namespaced Classes
There are a few reserved namespaces for classes to provide common and globally-available abstractions.
.o- for CSS objects. Objects are usually common design patterns (like the Flag object). Modifying these classes could have severe knock-on effects.
.c- for CSS components. Components are designed pieces of UI—think buttons, inputs, modals, and banners.
.u- for helpers and utilities. Utility classes are usually single-purpose and have high priority. Things like floating elements, trimming margins, etc.
.is-, .has- for stateful classes, a la SMACSS. Use these classes for temporary, optional, or short-lived states and styles.
._ for hacks. Classes with a hack namespace should be used when you need to force a style with !important or increasing specificity, should be temporary, and should not be bound onto.
.t- for theme classes. Pages with unique styles or overrides for any objects or components should make use of theme classes.
Separation of Concerns (One Thing Well™)
You should always try to spot common code—padding, font sizes, layout patterns—and abstract them to reusable, namespaced classes that can be chained to elements and have a single responsibility. Doing so helps prevent overrides and duplicated rules, and encourages a separation of concerns.
Trang TopGit này là một snapshot — tab "Readme" hiển thị nguyên văn README của repo (đã bỏ link, giữ ảnh). Repo GitHub ở github.com/dropbox/css-style-guide là nguồn chính thức.
dropbox/css-style-guide có bao nhiêu sao?
dropbox/css-style-guide có 1.2k sao GitHub — tải lại trang để xem số mới nhất, hoặc xem trực tiếp github.com/dropbox/css-style-guide. TopGit phản chiếu số sao của GitHub nhưng không cam kết đến từng phút.
dropbox/css-style-guide có tag gì không?
Bản đồng bộ chưa ghi nhận topic GitHub nào cho dropbox/css-style-guide. GitHub topics hiển thị ở thanh bên phải trang repo — đó là nơi đáng kiểm tra nhất.
dropbox/css-style-guide có website riêng không?
TopGit chưa ghi nhận URL trang chủ cho dropbox/css-style-guide. Phần README ở tab phía trên thường có link demo, hoặc xem mô tả GitHub của repo.
dropbox/css-style-guide còn đang phát triển không?
Commit gần nhất trên dropbox/css-style-guide là 7.0 năm trước (theo timestamp GitHub). Repo có 82 fork — một chỉ báo về mức độ quan tâm của cộng đồng.
dropbox/css-style-guide viết bằng ngôn ngữ gì?
Dữ liệu TopGit chưa ghi nhận ngôn ngữ chính cho dropbox/css-style-guide. Xem danh sách file trên GitHub để biết chi tiết.
Đọc đầy đủ README ở tab phía trên.
Muốn nghe thêm một ý kiến về css-style-guide?
Hỏi một AI đọc được trang này — một cú bấm là có ngay nhận định về css-style-guide.