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!
CSS Naming——BEM
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
Block
As in the example below, li.item is a sub-element of the list
/* 常规写法和BEM写法相同 */ .list
Element
/* 常规写法 */ <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{}
Modifier
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>
Benefits of BEM
- ##Get rid of the trouble of specificity
- Fix inheritance problems
- stop worrying about naming
Several principles of css naming
##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
- Advantages
- : Reuse of CSS High efficiency, best performance, plug and play, convenient and fast, and development is also extremely fast because it saves a lot of time switching between HTML and CSS files. Disadvantages
- : Due to its single attribute, its applicable scenarios are limited. In addition, because it is easy to use, it is easily overused, resulting in higher maintenance costs. 2. Semantics-oriented
- Advantages
- It has a wide range of application scenarios, can achieve very exquisite layout effects, and is easy to expand; Disadvantages
- : The code is long and the development efficiency is average, because all HTML needs to be named, even if it is a 10-pixel spacing. This leads many developers to either choose to use the tag selector directly, or to choose a simple class name and then define the style through the parent-child relationship, which results in worse maintenance problems.
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:1. "CSS Selection World" by Zhang Xinxu
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

There are two ways to create a Bootstrap split line: using the tag, which creates a horizontal split line. Use the CSS border property to create custom style split lines.

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

To adjust the size of elements in Bootstrap, you can use the dimension class, which includes: adjusting width: .col-, .w-, .mw-adjust height: .h-, .min-h-, .max-h-

How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text

To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.

There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.
