当前位置: 首页 > 编程日记 > 正文

css 倒三角_倒三角结构:如何管理大型CSS项目

css 倒三角

by Luuk Gruijs

Luuk Gruijs着

倒三角结构:如何管理大型CSS项目 (The Inverted Triangle Architecture: how to manage large CSS Projects)

You’re assigned a small task to fix some little styling issues here and there. You’ve found the correct CSS rules to apply the fix, so you quickly drop those rules at the bottom of your CSS file, push your changes, and then move on with more important stuff.

您被分配了一个小任务来解决此处和此处的一些小样式问题。 您已经找到了正确CSS规则来应用此修复程序,因此您可以将这些规则快速放在CSS文件的底部,推送更改,然后继续进行其他重要操作。

Over time this happens a couple of times and before you know it “the bottom” of your CSS file consists of a few hundred lines of code that nobody understands and nobody dares to remove as it will inevitably break stuff.

随着时间的流逝,这种情况会发生几次,并且在您不知道CSS文件的“底部”之前,没有几百行代码,没人理解,也没人敢删除,因为它不可避免地会破坏内容。

Do you recognize this scenario because you’ve either done this yourself or you’ve seen colleagues do this? Well, read on and promise yourself that you will never do this again, because here’s an easier way to manage your CSS files.

您是否认识到这种情况是因为您自己完成了此操作,还是您看到同事都这样做了? 好吧,请继续阅读并向自己保证,您将永远不会再这样做,因为这是一种管理CSS文件的简便方法。

倒三角架构介绍 (Introducing the Inverted Triangle architecture)

The Inverted Triangle architecture, also know as ITCSS, is a methodology for structuring your CSS in the most effective and least wasteful way.

倒三角形架构,也称为ITCSS,是一种以最有效,最不浪费的方式构造CSS的方法。

ITCSS was first introduced by Harry Roberts and can be best visualized by an upside-down, layered triangle. ITCSS defines the shared CSS-rules of a project in a logical and sane manner, whilst also providing a solid level of encapsulation and decoupling that which prevents non-shared CSS-rules from interfering with one another.

ITCSS最初是由哈里·罗伯茨(Harry Roberts)提出的,可以通过上下颠倒的分层三角形来形象地看到。 ITCSS以逻辑和理智的方式定义了项目的共享CSS规则,同时还提供了可靠的封装和解耦级别,从而防止了非共享CSS规则之间的相互干扰。

ITCSS is very flexible as it does not force you to use any specific naming convention methodologies like SMACCS, OOCSS or BEM.

ITCSS非常灵活,因为它不会强迫您使用任何特定的命名约定方法,例如SMACCS,OOCSS或BEM。

原则 (The principles)

ITCSS works by structuring your entire CSS project according to these 3 principles:

ITCSS通过以下三个原则来构建整个CSS项目:

  1. Generic to explicit

    泛型到显式

    We start with the most generic, low-level, catch-all styles. This could be font settings or, for example, color variables if you’re using SCSS.

    我们从最通用的,低级的,万能的样式开始。 这可能是字体设置,例如,如果使用的是SCSS,则可能是颜色变量。

  2. Low to high specificity

    从低到高的特异性

    The lowest specificity selectors appear at the start of your project. The specificity steadily increases. This way we avoid specificity conflicts and specificity overrides using

    最低的特异性选择器出现在项目的开始。 特异性稳步提高。 这样我们可以避免使用特异性冲突和使用特异性覆盖

    !important.

    !important

  3. Far-reaching to localized

    深入到本地化

    Selectors at the start of our project affect a lot of DOM-elements, for example your browser reset styles, while selectors later in our project become very localized, for example specific styles for one component.

    项目开始时的选择器会影响许多DOM元素,例如您的浏览器重置样式,而项目后面的选择器则变得非常本地化,例如某个组件的特定样式。

三角形层 (The triangle layers)

Sticking to the above principles means we have to break up our CSS into layers. Each layer must be introduced in a location that honors each of the criteria.

坚持以上原则意味着我们必须将CSS分成多个层。 必须在符合每个标准的位置引入每个层。

It happens often that CSS is grouped by, for example, typographic styles, form styles, and styles for a specific component. These groups are often not imported in the most efficient order and this creates some inheritance or specificity problems.

CSS通常按印刷样式,表单样式和特定组件的样式进行分组。 这些组通常不会以最有效的顺序导入,这会带来一些继承或特异性问题。

In ITCSS, each layer is a logical progression from the last. It increases in specificity, narrows in reach and gets more explicit. This means our CSS is easier to scale, as we’re only adding to what is already there and not overriding what was previously written.

在ITCSS中,每一层都是从最后一层开始的逻辑发展。 它增加了特异性,缩小了范围,变得更加明确。 这意味着我们CSS易于扩展,因为我们仅添加现有内容,而不覆盖先前编写的内容。

Another big advantage of following this structure is that everybody always knows where to find certain CSS rules as they’re logically placed. This avoids the issue of people dropping their CSS rules at the bottom of the file.

遵循这种结构的另一个大优点是,每个人总是知道在逻辑上放置某些CSS规则的位置。 这样可以避免人们将CSS规则放在文件底部的问题。

Harry Roberts, the creator of ITCSS, laid out seven layers. He ordered them as follows:

ITCSS的创建者哈里·罗伯茨(Harry Roberts)布置了七个层。 他命令他们如下:

  1. Settings

    设定值

    If you’re using a preprocessor like SCSS, this is your starting point. In this layer you define your variables.

    如果您使用的是SCSS之类的预处理器,那么这就是您的起点。 在这一层中,定义变量。

  2. Tools

    工具类

    This layer can be used for your tooling. Think about mixins and functions that need to be globally available. If they don’t need to be, just place them in the layer where they’re needed.

    该层可用于您的工具。 考虑需要全局可用的混合器和功能。 如果不需要,只需将它们放在需要的图层中即可。

  3. Generic

    泛型

    In this layer, we house all the very high-level, far reaching styles. This layer is often the same across all your projects as it contains things like Normalize.css, CSS resets, and for example box-sizing rules.

    在这一层中,我们包含所有非常高级的,深远的样式。 在您的所有项目中,此层通常都是相同的,因为它包含Normalize.css,CSS重置以及框大小调整规则等内容。

  4. Elements

    元素

    In this layer we put styles for bare, unclassed HTML elements. You could, for example, think about underlines for anchors on hover or font-sizes for the different headings.

    在这一层中,我们为裸露的未分类HTML元素添加样式。 例如,您可以考虑悬停时锚点的下划线或不同标题的字体大小。

  5. Objects

    对象

    In the object layer we style the first elements that have classes. Think about your containers, wrappers or rows. You can also define your grid here.

    在对象层中,我们设置具有类的第一个元素的样式。 考虑一下您的容器,包装器或行。 您也可以在此处定义网格。

  6. Components

    组件

    The component layer is the place where most of the styling magic happens as you will be styling your UI elements here. In component based frameworks like Angular, Vue or React this is the layer where you import your styling for each component if you don’t include them directly in your component.

    组件层是大多数样式化魔术发生的地方,因为您将在此处对UI元素进行样式化。 在基于组件的框架(如Angular,Vue或React)中,如果不将其直接包含在组件中,则可以在该层中导入每个组件的样式。

  7. Trumps

    特朗普

    The trumps layer is the dirty layer. Even after structuring your styling according to the ITCSS principles it can happen that you have to use

    王牌层是脏层。 即使按照ITCSS原则构造了样式,也可能需要使用

    !important to override some third-party styling, for example. Do that in this layer as this is the most specific, local and explicit layer.

    !important可以覆盖某些第三方样式。 在这一层中执行此操作,因为这是最具体,本地和显式的层。

最终结果 (The end result)

Now that I’ve explained the layers, it’s time to look at how a simple end result could potentially look.

现在,我已经解释了各层,现在该看看简单的最终结果可能看起来如何了。

// settings@import "globals";@import "branding";
// tools@import "mixins";
// generic@import "normalize";
// elements@import "fonts";@import "form";
// objects@import "grid";@import "wrappers";
// components@import "header";@import "sidebar";@import "carousel";@import "card";
// trumps@import "overrides";

结论 (Conclusion)

Just as ITCSS doesn’t force you to use a certain naming conventions, it doesn’t force you to use all layers. Use a layer structure that works best for you while maintaining the ITCSS principles of generic to explicit, low to high specificity and far-reaching to localized.

正如ITCSS不会强迫您使用某些命名约定一样,它也不会强迫您使用所有图层。 使用最适合您的层结构,同时保持ITCSS泛型到显式,低到高的特异性以及对本地化的影响深远的原则。

If you notice that you have to override styles, it almost always means your structure is inefficient. If you feel like learning more about this, I recommend watching this video:

如果您发现必须覆盖样式,则几乎总是意味着您的结构效率低下。 如果您想了解更多有关此的内容,建议您观看以下视频:

在阿姆斯特丹找工作? (Looking for a job in Amsterdam?)

I work for Sytac as a Senior front-end developer and we are looking for medior/senior developers that specialise in Angular, React, Java or Scala. Sytac is an ambitious consultancy company in the Netherlands that works for a lot of renowned companies in the banking, airline, government and retail sectors.

我在Sytac担任高级前端开发人员,我们正在寻找专门从事Angular,React,Java或Scala的中/高级开发人员。 Sytac是荷兰一家雄心勃勃的咨询公司,为银行,航空,政府和零售领域的许多知名公司工作。

If you think you have what it takes to work with the best, send me an email at luuk[dot]gruijs[at]sytac[dot]io and I’d be happy to tell you more.

如果您认为自己有最好的工作条件,请给我发电子邮件至luuk [gruijs] [sytac] io ,我很乐意告诉您更多信息。

翻译自: https://www.freecodecamp.org/news/managing-large-s-css-projects-using-the-inverted-triangle-architecture-3c03e4b1e6df/

css 倒三角

相关文章:

列举一些常见的系统系能瓶颈 Common Bottlenecks

http://www.nowamagic.net/librarys/veda/detail/2408在 Zen And The Art Of Scaling - A Koan And Epigram Approach 一文中, Russell Sullivan 提出一个很有趣的设想:一共有20种经典的瓶颈。这听起来就像只有20种基本的故事情节(20 basic s…

Zeal 离线API文档浏览器

zeal是一个windows上的开源的离线文档浏览工具,基于docset格式,可以兼容全部dash的文档。zeal没有代码片段管理的功能,只提供文档浏览功能,不过windows下的用户可算是有的用了。dash目前只提供mac上的版本,作者说有往w…

iOS scrollToItemAtIndexPath 无效的解决方案

在UITableview中放置的UICollectionView,然后设置滚动没有效果scrollToItemAtIndexPath - (void)layoutSubviews {[self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:self.selectedIdx inSection:0] atScrollPosition:UICollectionViewScrollPositio…

机器学习编程语言_我应该选择哪种编程语言? 我应该专注于前端吗? 后端? 机器学习?...

机器学习编程语言by Preethi Kasireddy通过Preethi Kasireddy 我应该选择哪种编程语言? 我应该专注于前端吗? 后端? 机器学习? (What programming language should I pick? Should I focus on front-end? Back-end? Machine l…

spdlog源码阅读 (1): sinks

0. spdlog简单介绍 spdlog 是一个快速的 C 日志库,只包含头文件,兼容 C11。项目地址 特性: 非常快只包含头文件无需依赖第三方库支持跨平台 - Linux / Windows on 32/64 bits支持多线程可对日志文件进行循环输出可每日生成日志文件支持控制台日志输出可选…

什么样的程序员才算成熟? 让程序员认清自己的所处的阶段

http://www.nowamagic.net/librarys/veda/detail/1450程序员在经历了若干年编程工作之后,很想知道自己水平到底如何?自己是否已经成为成熟的程序员?虽然程序员会对自己有一个自我评价,但是,自己的评价和社会的评价、专…

iOS访问系统日历 添加提醒事件

1 添加隐私请求提示 Privacy - Calendars Usage Description 2 代码 #import <EventKit/EventKit.h> // 添加提醒事件 - (void)addEventWithTimeStr:(NSString *)timeStr title:(NSString *)title planId:(NSString *)planId {EKEventStore *store [[EKEventStore al…

数据分析从头学_数据新闻学入门指南:让我们从头开始构建故事

数据分析从头学by Mina Demian由Mina Demian 数据新闻学入门指南&#xff1a;让我们从头开始构建故事 (A Beginner’s Guide to Data Journalism: Let’s Build a Story From Scratch) This is an introductory guide on how to produce the beginnings of a piece of data jo…

Comparator 和 Comparable

Comparator 和 Comparable 比较 Comparable是排序接口&#xff1b;若一个类实现了Comparable接口&#xff0c;就意味着“该类支持排序”。而Comparator是比较器&#xff1b;我们若需要控制某个类的次序&#xff0c;可以建立一个“该类的比较器”来进行排序。 我们不难发现&…

朴素贝叶斯算法的python实现

朴素贝叶斯 算法优缺点 优点&#xff1a;在数据较少的情况下依然有效&#xff0c;可以处理多类别问题 缺点&#xff1a;对输入数据的准备方式敏感 适用数据类型&#xff1a;标称型数据 算法思想&#xff1a; 朴素贝叶斯比如我们想判断一个邮件是不是垃圾邮件&#xff0c;那么…

iOS 加载本地和网络gif 图片类扩展

https://github.com/AlexanderYeah/GifKuoZhan [self.meiXueImgView showGifImageWithData:[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:"美" ofType:"gif"]]];

arkit与现实世界距离比_如何使用ARKit和Pusher构建实时增强现实测量应用程序

arkit与现实世界距离比by Esteban Herrera由Esteban Herrera 如何使用ARKit和Pusher构建实时增强现实测量应用程序 (How to Build a Real-Time Augmented Reality Measuring App with ARKit and Pusher) Augmented reality (AR) is all about modifying our perception of the…

Servlet 3.0 新特性概述

Servlet 3.0 新特性概述 Servlet 3.0 作为 Java EE 6 规范体系中一员&#xff0c;随着 Java EE 6 规范一起发布。该版本在前一版本&#xff08;Servlet 2.5&#xff09;的基础上提供了若干新特性用于简化 Web 应用的开发和部署。其中有几项特性的引入让开发者感到非常兴奋&…

MVC、MVP、MVVM

MVC、MVP、MVVM这些模式是为了解决开发过程中的实际问题而提出来的&#xff0c;目前作为主流的几种架构模式而被广泛使用。 一、MVC&#xff08;Model-View-Controller&#xff09; MVC是比较直观的架构模式&#xff0c;用户操作->View&#xff08;负责接收用户的输入操作&a…

iOS 计算两个日期之间的差值

NSDateFormatter *dateFomatter [[NSDateFormatter alloc] init];dateFomatter.dateFormat "yyyy-MM-dd HH:mm";// 当前时间字符串格式NSDate *planDate [dateFomatter dateFromString:[model.PlanTime substringToIndex:16]];// 当前日历NSCalendar *calendar […

unity水管工_我是如何从30岁的管道工转变为32岁的Web开发人员的

unity水管工by Rick West由里克韦斯特(Rick West) 我是如何从30岁的管道工转变为32岁的Web开发人员的 (How I transformed from a 30-year-old plumber into a 32-year-old web developer) Friends often ask me why I decided to give up a solid, well-paying job like plum…

netty集成ssl完整参考指南(含完整源码)

虽然我们在内部rpc通信中使用的是基于认证和报文头加密的方式实现安全性&#xff0c;但是有些时候仍然需要使用SSL加密&#xff0c;可能是因为对接的三方系统需要&#xff0c;也可能是由于open的考虑。中午特地测了下netty下集成ssl的功能&#xff0c;关于ssl的握手过程以及jav…

呼叫中心的服务水平管理

对企业来讲&#xff0c;呼叫中心是企业的窗口&#xff0c;呼叫中心为客户提供服务&#xff0c;是企业提升品牌形象、建立客户忠诚度的最佳通道。因此&#xff0c;呼叫中心的服务水平对于企业来说意义重大&#xff0c;相应的服务水平管理更是企业必不可少的管理之一。 “客户至上…

iOS 数组中的模型去重

NSMutableDictionary *mutableDic [NSMutableDictionary dictionary];for (HomeRectProductModel *model in self.modelArr) {[mutableDic setValue:model forKey:model.Id];}[self.modelArr removeAllObjects];self.modelArr [[mutableDic allValues] mutableCopy];// 方法…

软件可读性和效率取舍_网络通信设计中的一些限制和取舍:摘要

软件可读性和效率取舍by Shubheksha通过Shubheksha 网络通信设计中的一些约束和折衷&#xff1a;摘要 (Some Constraints and Trade-offs In The Design of Network Communications: A Summary) This article distills the content presented in the paper “Some Constraints…

浅析对象访问属性的.和[]方法区别

原文链接&#xff1a;http://www.cnblogs.com/bigboyLin/p/4967820.html 简明版&#xff1a;请问js对象属性值为什么用数组也可以访问 在JavaScript中通常使用”."运算符来存取对象的属性的值。或者使用[]作为一个关联数组来存取对象的属性。但是这两种方式有什么区别了&a…

iOS 关闭页面侧滑手势

-(void)popGestureChange:(UIViewController *)vc enable:(BOOL)enable{if ([vc.navigationController respondsToSelector:selector(interactivePopGestureRecognizer)]) {//遍历所有的手势for (UIGestureRecognizer *popGesture in vc.navigationController.interactivePopGe…

CSS与HTML结合

CSS与HTML结合的4中方式&#xff1a; 1、每个HTML标签都有style属性 2、当页面中有多个标签具有相同样式时&#xff0c;可定义style标签封装样式以复用 <style type”text/css”>css代码</style> 3、当多个页面使用相同样式时&#xff0c;可将样式单独封装为CSS文件…

硬件断点反跳似乎_高性能应用程序:多路复用,反跳,系统字体和其他技巧

硬件断点反跳似乎by Atila Fassina通过阿蒂拉法西纳(Atila Fassina) 高性能应用程序&#xff1a;多路复用&#xff0c;反跳&#xff0c;系统字体和其他技巧 (High Performance Apps: Multiplexing, Debouncing, System Fonts, and other tricks) Here are some performance ti…

jquery仿邮箱文本输入框自动加载邮箱后缀

jquery仿邮箱文本输入框自动加载邮箱后缀 在像百度这样的网站注册时&#xff0c;你会看到输入邮箱会出现自动给用户输入补全主流邮箱。这种对于增加用户体验的小例子已司空见惯。正好看到人家写的这种js功能。还挺不错,使用起来很方便&#xff0c;几乎不用写神呢代码。"傻…

Maven最佳实践:划分模块

所有用Maven管理的真实的项目都应该是分模块的&#xff0c;每个模块都对应着一个pom.xml。它们之间通过继承和聚合&#xff08;也称作多模块&#xff0c;multi-module&#xff09;相互关联。那么&#xff0c;为什么要这么做呢&#xff1f;我们明明在开发一个项目&#xff0c;划…

facebook 直播_什么时候是在Facebook Live上直播的最佳时间? 我分析了5,000个Facebook帖子以找出答案。...

facebook 直播by Ofir Chakon由Ofir Chakon 什么时候是在Facebook Live上直播的最佳时间&#xff1f; 我分析了5,000个Facebook帖子以找出答案。 (When is the best time to stream on Facebook Live? I analyzed 5,000 Facebook posts to find out.) Streaming on Facebook …

解决keepalived脑裂问题

检测思路&#xff1a;正常情况下keepalived的VIP地址是在主节点上的&#xff0c;如果在从节点发现了VIP&#xff0c;就设置报警信息 脚本如下&#xff1a; #!/bin/bash # 检查脑裂的脚本&#xff0c;在备节点上进行部署 LB01_VIP10.10.10.229 LB01_IP10.10.10.129 LB02_IP10.10…

iOS 根据中文字符串排序出字母索引

// 传入字符串数组 返回索引字典 - (NSDictionary *)createCharacter:(NSMutableArray *)strArr {NSMutableDictionary *dict [NSMutableDictionary dictionary];for (NSString *stringdict in strArr) {NSString *string stringdict;if ([string length]) {NSMutableString …

devops开发运维训练营_嗨,网络开发人员训练营的毕业生:这是您第一份工作需要了解的内容。...

devops开发运维训练营by Rachel Bird雷切尔伯德(Rachel Bird) 嗨&#xff0c;网络开发人员训练营的毕业生&#xff1a;这是您第一份工作需要了解的内容。 (Hey web dev bootcamp grads: Here’s what you need to know for your first job.) You worked your butt off and gai…