Table of Contents
CSS分类
命名规则
属性分组
选择器性能优化
颜色声明
文件组织
其他资源
CSS Preprocessors
其他方法论
文档
更高级的规范
彩蛋
Home Web Front-end HTML Tutorial 漫谈SMACSS_html/css_WEB-ITnose

漫谈SMACSS_html/css_WEB-ITnose

Jun 24, 2016 am 11:27 AM

SMACSS (Scalable and Modular Architecture for CSS)发音同“smacks”,它不是框架,不是库。它只是一种组织CSS代码的方法论(或者说是为了让代码可扩展,易维护的一套规范,它会使团队开发更容易)。每个人看世界的角度不同,方法论因人而异,所以这套玩意儿不一定适合你。

CSS分类

按照SMACSS可以分为5类:

  1. Base—— 通常是一些默认样式,reset.css ,normalize.css 之类

    html, body, form { margin: 0;}input[type=text] { border: 1px solid #999;}a { color: #390;}
    Copy after login

  2. Layout—— Layout 则是将页面分成几个部分,一个 Layout 中可以有一个或多个Module

    .nav {}.footer {}.sidebar {}
    Copy after login

  3. Module—— 这个部分是可复用,模块化的代码,比如弹出框,汉堡按钮。

    .panel {}.panel-body {}
    Copy after login

  4. State—— 状态是用来描述Layout,Module的特殊样式。

    .is-hidden {}
    Copy after login

  5. Theme—— Theme是定义多种主题

命名规则

通常使用 l- 前缀标记这个类名是 Layout, is- 前缀标记为状态类等等,而Modules不需要,该是什么名字就叫什么。

/* Example Module */.example { }/* Callout Module */.callout { }/* Callout Module with State */.callout.is-collapsed { }/* Inline layout */.l-inline { }
Copy after login

属性分组

修改css属性时,我们常常需要肉眼搜索目标属性。如果属性杂乱无章,那么我们得花更多的时间查找。有人开发过按css属性字母顺序排列的插件,个人认为不好用,比如需要修改宽高时,需要找到height,再找到width,还是挺麻烦。而通常这类参数是成组出现的。

我们可以按照下列顺序书写:

  • Box ( display, float, position, left, top, height, width… )
  • Border ( border, border-image, border-radius… )
  • Background ( background-color, background-image… )
  • Text ( font-family, font-size, text-transform… )
  • Other ( other )

选择器性能优化

CSS选择器从右到左进行匹配

body div#content p {  color: #003366; }
Copy after login

在浏览器查找到p元素的时候,就会往上找是否有div#content这样的父元素,若找到了,则继续向上搜索,知道匹配成功,添加到渲染树中;若找不到,则匹配失败,这块样式不起作用。

控制好饮食,小心吃撑:虽然后代选择器很方便,但是从上面的分析看出来,没遇到p元素,我们都要执行一次向上搜索,代价显然是很昂贵的。

遵循下面三条规范可以帮助你健身:

  • 使用子类选择器(’>’,IE6我对不起你)
  • 避免使用常用的标签选择器
  • 最右选择器使用类名

比如你DOM中有一打H3,假设使用 .module h3 这样的选择器,那么对于 html > body > div > div > ... > h3 这样的节点,向上搜索的路径就长了去。改成 .module > h3 我们只需要搜索24个元素。

颜色声明

尽量使用3或6位的16进制颜色值,因为它们足够简洁。rgb,hsl就稍微有点长了。当然rgba, hsla就避免不了,因为十六进制没有alpha值。

文件组织

  • 把所有基本样式放在单独文件
  • layout 取决你文件的大小,细化成多个文件或者是放在同一个文件
  • 每个模块都独立一个文件
  • 子模块取决于项目的大小
  • 全局states放在单独文件
  • layout,modules states 以及 media queries 放在相应的 modules 文件中

结合Sass, 文件树如下:

+-layout/ | +-grid.scss | +-alternate.scss +-module/ | +-callout.scss | +-bookmarks.scss | +-btn.scss | +-btn-compose.scss +-base.scss +-states.scss
Copy after login

然后在main.scss中引入,

@import "base", "states", "layout/grid", "module/btn", ...
Copy after login

最终产出一个 main.css 文件。可以参考 jonathanpath 的 SASS-SMACSS, github地址

其他资源

CSS Preprocessors

  • LESS
  • Sass

其他方法论

  • OOCSS
  • BEM

文档

  • Front-end Style Guides

其他资源

  • mustache 是一个轻逻辑的模板语言,不受环境影响。

更高级的规范

  • 墙裂推荐 Hugo Giraudel 的 Sass Guidelines

彩蛋

我买了 SMACSS 电子书 ,这篇文章是我的学习笔记 + 要点总结。如有需要,留下邮箱,我可以把电子书共享给你(仅供学习交流使用),或者扫描 WeChat 二维码加我为好友。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1268
29
C# Tutorial
1242
24
Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

HTML, CSS, and JavaScript: Essential Tools for Web Developers HTML, CSS, and JavaScript: Essential Tools for Web Developers Apr 09, 2025 am 12:12 AM

HTML, CSS and JavaScript are the three pillars of web development. 1. HTML defines the web page structure and uses tags such as, etc. 2. CSS controls the web page style, using selectors and attributes such as color, font-size, etc. 3. JavaScript realizes dynamic effects and interaction, through event monitoring and DOM operations.

HTML: The Structure, CSS: The Style, JavaScript: The Behavior HTML: The Structure, CSS: The Style, JavaScript: The Behavior Apr 18, 2025 am 12:09 AM

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

The Future of HTML, CSS, and JavaScript: Web Development Trends The Future of HTML, CSS, and JavaScript: Web Development Trends Apr 19, 2025 am 12:02 AM

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

The Future of HTML: Evolution and Trends in Web Design The Future of HTML: Evolution and Trends in Web Design Apr 17, 2025 am 12:12 AM

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

HTML vs. CSS vs. JavaScript: A Comparative Overview HTML vs. CSS vs. JavaScript: A Comparative Overview Apr 16, 2025 am 12:04 AM

The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

HTML: Building the Structure of Web Pages HTML: Building the Structure of Web Pages Apr 14, 2025 am 12:14 AM

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

The Role of HTML: Structuring Web Content The Role of HTML: Structuring Web Content Apr 11, 2025 am 12:12 AM

The role of HTML is to define the structure and content of a web page through tags and attributes. 1. HTML organizes content through tags such as , making it easy to read and understand. 2. Use semantic tags such as, etc. to enhance accessibility and SEO. 3. Optimizing HTML code can improve web page loading speed and user experience.

See all articles