Home >Web Front-end >CSS Tutorial >How to write elegant and durable css code? Sharing css naming tips!
If you've been working with CSS for a while, you know that inheritance, specificity, and naming are some of the hardest things to deal with. Since all CSS is global, as your codebase grows, it becomes increasingly difficult to prevent styles from conflicting with each other and causing unintended side effects! The following article will share with you some CSS naming tips to make our code elegant and attractive. I hope it will be helpful to everyone!
What is BEM
BEM is a CSS naming convention. [Recommended learning: css video tutorial]
BEM stands for "block, element, modifier".
In the selector, the following three symbols represent extended relationships:
- 中划线 :仅作为连字符使用,表示某个块或者某个子元素的多单词之间的连接记号。 __ 双下划线:双下划线用来连接块和块的子元素 _ 单下划线:单下划线用来描述一个块或者块的子元素的一种状态
Use BEM example comparison
As in the example below, li.item is a sub-element of the list
/* 常规写法和BEM写法相同 */ .list
/* 常规写法 */ <ul class="list"> <li class="item">Pricing</li> <li class="item">Contact</li> </ul> /* BEM写法 */ <ul class="list"> <li class="list__item">Pricing</li> <li class="list__item">Contact</li> </ul>
/* 常规写法 */ .list{} .list .item{} /* BEM写法 */ .list{} .list__item{}
A "modifier" can be understood as a specific state of a block!
/* 常规写法 */ <ul class="list"> <li class="item">Pricing</li> <li class="item">Contact</li> </ul> /* BEM写法 */ <ul class="list"> <li class="list__item_active"> Pricing </li> <li class="list__item">Contact</li> </ul>rrree
Benefits of BEM
##Short names are better than long names Because short naming shortens writing time, it also reduces the size of css files! For example, this example:
/* 常规写法 */ .list{} .list .item{} .list .item.active{} /* BEM写法 */ .list{} .list__item{} .list__item_active{}
Combined naming is better than single naming// 推荐
.some-intro{...}
// 不推荐
.some-introduction{...}
Attribute-oriented naming and Semantic namingAttribute-oriented naming means that the naming of the selector follows the specific CSS style and has nothing to do with the project, page, or module. For example, the more classic clear floating class name.clearfix:
// 不建议 .header{...} //推荐 .cs-header{...}
The semantic-oriented naming is named according to the context in which the application element is located. For example:
.clearfix:after { content : ''; display: table; clear: both; }
The above two naming methods have their own advantages and disadvantages:
1. Attribute-oriented
Status.header { background-color: #333; color: #fff; }
.logo {font-size: 0; color : transparent;}
Layout前一个 prev
后一个 next
当前的 current
显示的 show
隐藏的 hide
打开的 open
关闭的 close
选中的 selected
有效的 active
默认的 default
反转的 toggle
禁用的 disabled
危险的 danger
主要的 primary
成功的 success
提醒的 info
警告的 warning
出错的 error
大型的 lg
小型的 sm
超小的 xs
Common Parts文档 doc
头部 header(hd)
主体 body
尾部 footer(ft)
主栏 main
侧栏 side
容器 box/container
Component列表 list
列表项 item
表格 table
表单 form
链接 link
标题 caption/heading/title
菜单 menu
集合 group
条 bar
内容 content
结果 result
Semantic widget按钮 button(btn)
字体 icon
下拉菜单 dropdown
工具栏 toolbar
分页 page
缩略图 thumbnail
警告框 alert
进度条 progress
导航条 navbar
导航 nav
子导航 subnav
面包屑 breadcrumb(crumb)
标签 label
徽章 badge
巨幕 jumbotron
面板 panel
洼地 well
标签页 tab
提示框 tooltip
弹出框 popover
轮播图 carousel
手风琴 collapse
定位浮标 affix
Functional widget品牌 brand
标志 logo
额外部件 addon
版权 copyright
注册 regist(reg)
登录 login
搜索 search
热点 hot
帮助 help
信息 info
提示 tips
开关 toggle
新闻 news
广告 advertise(ad)
排行 top
下载 download
Recommended naming websitecodelf: https://unbug.github.io/codelf
Reference document:2. https://www.cnblogs.com/qianxiaox/p/13816077.html
(Learning video sharing:
webfrontendThe above is the detailed content of How to write elegant and durable css code? Sharing css naming tips!. For more information, please follow other related articles on the PHP Chinese website!