search
HomeWeb Front-endCSS TutorialHow to standardize writing css and increase the code reading experience

When we are developing the front-end web page, writing css according to a good css specification can not only improve the reading experience of the code; it can also quickly find the location of the error when an error occurs, making it easy to correct. Here Design Expert Network summarizes a CSS writing specification and CSS writing order for your reference. These are summarized with reference to some foreign articles and my personal experience. I think it is worth learning for front-end users who write CSS.

CSS writing order

1. Position attributes (position, top, right, z-index, display, float, etc.)
2. Size (width, height, padding, margin)
3. Text series (font, line-height, letter-spacing, color-text-align, etc.)
4. Background (background, border, etc.)
5. Others ( animation, transition, etc.)

How to standardize writing css and increase the code reading experience

CSS writing specifications use CSS abbreviation properties

Some CSS properties can be abbreviated, such as padding, margin, font, etc., which streamlines the code and improves the user's reading experience.

How to standardize writing css and increase the code reading experience

Remove the "0" before the decimal point

How to standardize writing css and increase the code reading experience

Abbreviated naming

Many users like to abbreviate class names, but the premise is that people can understand your naming before you can abbreviate it!

How to standardize writing css and increase the code reading experience

Hexadecimal color code abbreviation

Some color codes can be abbreviated, let’s try to abbreviate them as much as possible to improve user experience Lord.

How to standardize writing css and increase the code reading experience

Hyphen CSS selector naming convention

1. Long names or phrases can use hyphens to name selectors .

2. It is not recommended to use "_" underscore to name CSS selectors. Why?

Press one less shift key when typing;

Browser compatibility issues (such as using the _tips selector naming is invalid in IE6)

Can be distinguished well JavaScript variable naming (JS variable naming uses "_")

How to standardize writing css and increase the code reading experience

Don't use id randomly

id is unique in JS , cannot be used multiple times, but the class selector can be used repeatedly. In addition, the priority of id is first and class, so id should be used as needed and cannot be abused.

How to standardize writing css and increase the code reading experience

Add status prefix to selector

Sometimes you can add a prefix indicating status to the selector to make the semantics clearer , for example, the following figure adds the ".is-" prefix.

How to standardize writing css and increase the code reading experience

CSS naming convention (rules) Commonly used CSS naming rules

Header (header), content (content/container) tail (footer), navigation (nav), sidebar (sidebar), column (column), page peripheral control overall layout width (wrapper), left right center (left right center), login bar (loginbar), logo (logo), advertising (banner) page main body, hot spot, news, download, subnav, menu, submenu, search, friendlink ), footer, copyright, scroll, content, tags, list of articles, msg, tips, title ), join (joinus), guide (guide), service (service), registration (regsiter), status (status), vote (vote), partner (partner)

注释的写法:
/* Header */内容区/* End Header */

id naming :

1) Page structure

Container: container, header: header, content: content/container, page body: main, footer: footer, navigation: nav, side Column: sidebar, column: column, page peripheral control overall layout width: wrapper, left right center: left right center

(2) Navigation

Navigation: nav, main navigation: mainnav, sub-navigation : subnav, top navigation: topnav, side navigation: sidebar, left navigation: leftsidebar, right navigation: rightsidebar, menu: menu, submenu: submenu, title: title, summary: summary

(3) Function

标志:logo,广告:banner,登陆:login,登录条:loginbar,注册:register,搜索:search,功能区:shop,标题:title,加入:joinus,状态:status,按钮:btn,滚动:scroll,标籤页:tab,文章列表:list,提示信息:msg,当前的: current,小技巧:tips,图标: icon,注释:note,指南:guild,服务:service,热点:hot,新闻:news,下载:download,投票:vote,合作伙伴:partner,友情链接:link,版权:copyright

注意事项::
1.一律小写;
2.尽量用英文;
3.不加中槓和下划线;
4.尽量不缩写,除非一看就明白的单词。

CSS样式表文件命名

主要的(master.css) ,模块(module.css) ,基本共用 (base.css),布局、版面 (layout.css),主题( themes.css),专栏(columns.css) ,文字( font.css),表单 (forms.css),补丁( mend.css),打印 (print.css)

The above is the detailed content of How to standardize writing css and increase the code reading experience. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
The Lost CSS Tricks of Cohost.orgThe Lost CSS Tricks of Cohost.orgApr 25, 2025 am 09:51 AM

In this post, Blackle Mori shows you a few of the hacks found while trying to push the limits of Cohost’s HTML support. Use these if you dare, lest you too get labelled a CSS criminal.

Next Level CSS Styling for CursorsNext Level CSS Styling for CursorsApr 23, 2025 am 11:04 AM

Custom cursors with CSS are great, but we can take things to the next level with JavaScript. Using JavaScript, we can transition between cursor states, place dynamic text within the cursor, apply complex animations, and apply filters.

Worlds Collide: Keyframe Collision Detection Using Style QueriesWorlds Collide: Keyframe Collision Detection Using Style QueriesApr 23, 2025 am 10:42 AM

Interactive CSS animations with elements ricocheting off each other seem more plausible in 2025. While it’s unnecessary to implement Pong in CSS, the increasing flexibility and power of CSS reinforce Lee's suspicion that one day it will be a

Using CSS backdrop-filter for UI EffectsUsing CSS backdrop-filter for UI EffectsApr 23, 2025 am 10:20 AM

Tips and tricks on utilizing the CSS backdrop-filter property to style user interfaces. You’ll learn how to layer backdrop filters among multiple elements, and integrate them with other CSS graphical effects to create elaborate designs.

SMIL on?SMIL on?Apr 23, 2025 am 09:57 AM

Well, it turns out that SVG's built-in animation features were never deprecated as planned. Sure, CSS and JavaScript are more than capable of carrying the load, but it's good to know that SMIL is not dead in the water as previously

'Pretty' is in the eye of the beholder'Pretty' is in the eye of the beholderApr 23, 2025 am 09:40 AM

Yay, let's jump for text-wrap: pretty landing in Safari Technology Preview! But beware that it's different from how it works in Chromium browsers.

CSS-Tricks Chronicles XLIIICSS-Tricks Chronicles XLIIIApr 23, 2025 am 09:35 AM

This CSS-Tricks update highlights significant progress in the Almanac, recent podcast appearances, a new CSS counters guide, and the addition of several new authors contributing valuable content.

Tailwind's @apply Feature is Better Than it SoundsTailwind's @apply Feature is Better Than it SoundsApr 23, 2025 am 09:23 AM

Most of the time, people showcase Tailwind's @apply feature with one of Tailwind's single-property utilities (which changes a single CSS declaration). When showcased this way, @apply doesn't sound promising at all. So obvio

See all articles

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

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.