search
HomeWeb Front-endHTML TutorialCSS naming rules specification_html/css_WEB-ITnose

Here, I am very grateful to the author of this article for sharing. This article is completely reproduced from the link above. It is here as a backup for easy viewing and use.

CSS naming rules specification

When people write css, they often encounter questions about Naming issue. With hundreds or even thousands of classes or IDs on the page, we will become more and more troubled. Therefore, it is necessary for us to organize our own set of naming conventions. Here I will talk about my own life

When everyone writes css, they often encounter naming problems. With hundreds or even thousands of classes or IDs on the page, we will become more and more troubled.

So, it is necessary for us to organize our own set of naming conventions. Here I will talk about my own naming convention.

Class-class

Class name, I usually use horizontal lines like this

.head-logo { … } /*Page header The class name of logo*/
.nav-li { … } /*The class name of the list on the navigation bar*/
.link-panel-h2 { … } /*The title of the link module*/

ID

id, I usually use camel case naming. In my understanding, camelCase naming is used exclusively for unique variables. So I often use this method to define variables in javascrpt.

#navLastLi { … } /*Get the last list of the navigation bar*/
#panelContent { … } /*The text part of the module*/
#sidebarFooter { … } /*Side Sidebar bottom module*/

So, there are many page modules, we can organize them like this

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 and right center: left right center

Login bar : loginbar Logo: logo Advertisement: banner Page body: main Hotspot: hot

News: news Download: download Sub-navigation: subnav Menu: menu

Submenu: submenu Search: search Friendly links: friendlink Footer: footer

Copyright: copyright Scroll: scroll Content: content Tag page: tab

Article list: list Prompt message: msg Tips: tips Column title: title

Join: joinus Guide: guild Service: service Registration: regsiter

Status: status Vote: vote Partner: partner

(1) Page structure

Container: container Header: header Content: content/container

Page body: main Page footer: footer Navigation: nav

Sidebar: sidebar Column: column Page peripheral control overall layout Width: wrapper

Left right center

Navigation: nav Main navigation: mainbav 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: logo Advertising: banner Login: login Login bar: loginbar

Register: regsiter Search: search Ribbon: shop

Title: title Join: joinus Status: status Button: btn

Scroll: scroll Tab: tab Article list: list Prompt message: msg

Current: current Tips: tips Icon : icon Comments: note

Guide: guildService: service Hotspot: hot News: news

Download: download Vote: votePartner: partner

Friendly link: linkCopyright :copyright

When we use scripts to dynamically transform pages, the recommended method is to modify the class name or modify the id name to modify the display style. Of course, the most commonly used method is to modify the display style. name class.

Modify the class name - naming specification

(1) Color: use the name of the color or Hexadecimal code, such as

.red { color: red; }

.f60 { color: #f60; }

.ff8600 { color: #ff8600; }

(2) Font size, directly use 'font font size' as the name, such as

.font12px { font-size: 12px; }

.font9pt {font-size: 9pt; }

(3) Alignment style, use the English name of the alignment target, such as

.left { float:left; }

.bottom { float:bottom; }

 

(4)标题栏样式,使用’类别+功能’的方式命名,如

.barnews { }

.barproduct { }

 

 

注意事项::

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

 

 

根据页面分栏结构的命名

     CSS代码的命名惯例一直是大家热门讨论的话题。通过分析一个流行三栏布局中的必要元素,来为大家讲解关于使用语义化方法替代结构化方法来命名CSS类的建议和指导。

      CSS类命名的语义化VS结构化方式

      一般而言,CSS类名的语义化声明方式应当考虑你的页面中某个相对元素的”用意”,独立于它的”定位”或确切的特性(结构化方式)。像left-bar, red-text, small-title…这些都属于结构化定义的例子。

让我们看看下面这个例子:

 

 

 

 

  …而现在我们想把页面中的元素调换一下位置,如果你使用的是结构化方式(1),那么你就要把所有CSS类名重新进行定义,因为它们的位置变了。在布局(3)中,我们看到元素都倒转了: right-bar 现在成了 “left-bar”,而 left-content 成了 “right-content”。如果你使用语义化方式则避免了此类问题。

 

 

  换句话说,使用语义化方式的话,你在修改网站布局的时候可以仅仅修改相关CSS类的属性即可,而不用修改它们的类名了,如果网站的代码很庞大,这将节省大量的时间。

 

 

关于语义化的一些建议:

在开始之前,我想推荐两种简单的编写较好的CSS代码的指导方针:

1、为CSS类名定义的时候,尽量使用小写字母,如果有两个以上的单词,在每个单词之间使用”-”符或单词首字母大写(第一个单词除外)。如:”main-content”或”mainContent”。 
 

2、优化CSS代码,仅创建关键主要的CSS类并重新为子元素使用符合HTML标准的标签(h1, h2, p, ul, li, blockquote,…),例如,不要使用这种哦你那个方式:

  1.  
  2.     
     
  3.     
     
 

 

而要这样写:

  1.  
  2.     

     
  3.     

     

Example of using semantics in three-column layout

Let us use this simple example to explain how to The classic three-column layout uses semantic naming:

Using semantic naming for CSS can look like this:

  1. # container{…}
  2. /*?- Top section ?-*/
  3. #header{…}
  4. #navbar{…}
  5. /* ?- Main ?-*/
  6. #menu{…}
  7. #main{…}
  8. #sidebar{…}
  9. /*?- Footer ?-*/
  10. #footer{…}

  1. Container
    "#container" is the content of your page The part that wraps all the elements together. You can also name this part: "wrapper", "wrap", "page".
  2. Header
    "#header" is the header area of ​​the website page. Generally speaking, it contains the website's logo and some other elements. You can also name this part: "top", "logo", "page-header" (or pageHeader).
  3. Navbar
    "#navbar" is equivalent The horizontal navigation bar is the most typical web element. You can also name this section: "nav", "navigation", "nav-wrapper".
  4. Menu
    The "#Menu" area contains general links And menu, you can also name this part: "sub-nav", "links".
  5. Main
    "#Main" is the main area of ​​the website, If it's a blog it will contain your blog posts. You can also name this part: "content", "main-content" (or "mainContent").
  6. Sidebar
    The "#Sidebar" section can contain secondary content of the website, such as a list of recently updated content, an introduction to the website or advertising elements, etc... You can also name this part For: "sub-nav", "side-panel", "secondary-content".

  7. Footer
    "#Footer" contains some of the website Additional information, you can also name this part: "copyright".

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
What is the difference between an HTML tag and an HTML attribute?What is the difference between an HTML tag and an HTML attribute?May 14, 2025 am 12:01 AM

HTMLtagsdefinethestructureofawebpage,whileattributesaddfunctionalityanddetails.1)Tagslike,,andoutlinethecontent'splacement.2)Attributessuchassrc,class,andstyleenhancetagsbyspecifyingimagesources,styling,andmore,improvingfunctionalityandappearance.

The Future of HTML: Evolution and TrendsThe Future of HTML: Evolution and TrendsMay 13, 2025 am 12:01 AM

The future of HTML will develop in a more semantic, functional and modular direction. 1) Semanticization will make the tag describe the content more clearly, improving SEO and barrier-free access. 2) Functionalization will introduce new elements and attributes to meet user needs. 3) Modularity will support component development and improve code reusability.

Why are HTML attributes important for web development?Why are HTML attributes important for web development?May 12, 2025 am 12:01 AM

HTMLattributesarecrucialinwebdevelopmentforcontrollingbehavior,appearance,andfunctionality.Theyenhanceinteractivity,accessibility,andSEO.Forexample,thesrcattributeintagsimpactsSEO,whileonclickintagsaddsinteractivity.Touseattributeseffectively:1)Usese

What is the purpose of the alt attribute? Why is it important?What is the purpose of the alt attribute? Why is it important?May 11, 2025 am 12:01 AM

The alt attribute is an important part of the tag in HTML and is used to provide alternative text for images. 1. When the image cannot be loaded, the text in the alt attribute will be displayed to improve the user experience. 2. Screen readers use the alt attribute to help visually impaired users understand the content of the picture. 3. Search engines index text in the alt attribute to improve the SEO ranking of web pages.

HTML, CSS, and JavaScript: Examples and Practical ApplicationsHTML, CSS, and JavaScript: Examples and Practical ApplicationsMay 09, 2025 am 12:01 AM

The roles of HTML, CSS and JavaScript in web development are: 1. HTML is used to build web page structure; 2. CSS is used to beautify the appearance of web pages; 3. JavaScript is used to achieve dynamic interaction. Through tags, styles and scripts, these three together build the core functions of modern web pages.

How do you set the lang attribute on the  tag? Why is this important?How do you set the lang attribute on the tag? Why is this important?May 08, 2025 am 12:03 AM

Setting the lang attributes of a tag is a key step in optimizing web accessibility and SEO. 1) Set the lang attribute in the tag, such as. 2) In multilingual content, set lang attributes for different language parts, such as. 3) Use language codes that comply with ISO639-1 standards, such as "en", "fr", "zh", etc. Correctly setting the lang attribute can improve the accessibility of web pages and search engine rankings.

What is the purpose of HTML attributes?What is the purpose of HTML attributes?May 07, 2025 am 12:01 AM

HTMLattributesareessentialforenhancingwebelements'functionalityandappearance.Theyaddinformationtodefinebehavior,appearance,andinteraction,makingwebsitesinteractive,responsive,andvisuallyappealing.Attributeslikesrc,href,class,type,anddisabledtransform

How do you create a list in HTML?How do you create a list in HTML?May 06, 2025 am 12:01 AM

TocreatealistinHTML,useforunorderedlistsandfororderedlists:1)Forunorderedlists,wrapitemsinanduseforeachitem,renderingasabulletedlist.2)Fororderedlists,useandfornumberedlists,customizablewiththetypeattributefordifferentnumberingstyles.

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 Article

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor