search
HomeWeb Front-endHTML TutorialCSS 编码规范_html/css_WEB-ITnose

在编写CSS时也存在一些编码规范,平时注意这些基本的规范,可使代码更易阅读和维护。

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title></title>    <style>        /* 语法        用两个空格来代替制表符(tab) -- 这是唯一能保证在所有环境下获得一致展现的方法。        为选择器分组时,将单独的选择器单独放在一行。        为了代码的易读性,在每个声明块的左花括号前添加一个空格。        声明块的右花括号应当单独成行。        每条声明语句的 : 后应该插入一个空格。        为了获得更准确的错误报告,每条声明都应该独占一行。        所有声明语句都应当以分号结尾。最后一条声明语句后面的分号是可选的,但是,如果省略这个分号,你的代码可能更易出错。        对于以逗号分隔的属性值,每个逗号后面都应该插入一个空格(例如,box-shadow)。        不要在 rgb()、rgba()、hsl()、hsla() 或 rect() 值的内部的逗号后面插入空格。这样利于从多个属性值(既加逗号也加空格)中区分多个颜色值(只加逗号,不加空格)。        对于属性值或颜色参数,省略小于 1 的小数前面的 0 (例如,.5 代替 0.5;-.5px 代替 -0.5px)。        十六进制值应该全部小写,例如,#fff。在扫描文档时,小写字符易于分辨,因为他们的形式更易于区分。        尽量使用简写形式的十六进制值,例如,用 #fff 代替 #ffffff。        为选择器中的属性添加双引号,例如,input[type="text"]。只有在某些情况下是可选的,但是,为了代码的一致性,建议都加上双引号。        避免为 0 值指定单位,例如,用 margin: 0; 代替 margin: 0px;。*/        /* Bad CSS */        .selector, .selector-secondary, .selector[type=text] {            padding: 15px;            margin: 0px 0px 15px;            background-color: rgba(0, 0, 0, 0.5);            box-shadow: 0px 1px 2px #CCC, inset 0 1px 0 #FFFFFF        }        /* Good CSS */        .selector,        .selector-secondary,        .selector[type="text"] {            padding: 15px;            margin-bottom: 15px;            background-color: rgba(0, 0, 0, .5);            box-shadow: 0 1px 2px #ccc, inset 0 1px 0 #fff;        }        /* 声明顺序       相关的属性声明应当归为一组,并按照下面的顺序排列:       Positioning       Box model       Typographic       Visual       由于定位(positioning)可以从正常的文档流中移除元素,并且还能覆盖盒模型(box model)相关的样式,因此排在首位。盒模型排在第二位,因为它决定了组件的尺寸和位置。       其他属性只是影响组件的内部(inside)或者是不影响前两组属性,因此排在后面。*/        .declaration-order {            /* Positioning */            position: absolute;            top: 0;            right: 0;            bottom: 0;            left: 0;            z-index: 100;            /* Box-model */            display: block;            float: right;            width: 100px;            height: 100px;            /* Typography */            font: normal 13px "Helvetica Neue", sans-serif;            line-height: 1.5;            color: #333;            text-align: center;            /* Visual */            background-color: #f5f5f5;            border: 1px solid #e5e5e5;            border-radius: 3px;            /* Misc */            opacity: 1;        }    </style>    <!-- 不要使用 @import    与 <link> 标签相比,@import 指令要慢很多,不光增加了额外的请求次数,还会导致不可预料的问题。替代办法有以下几种:    使用多个 <link> 元素    通过 Sass 或 Less 类似的 CSS 预处理器将多个 CSS 文件编译为一个文件    通过 Rails、Jekyll 或其他系统中提供过 CSS 文件合并功能 -->    <!-- Use link elements -->    <link rel="stylesheet" href="core.css">    <!-- Avoid @imports -->    <style>        @import url("more.css");    </style>    <!-- class 命名    class 名称中只能出现小写字符和破折号(dashe)(不是下划线,也不是驼峰命名法)。破折号应当用于相关 class 的命名(类似于命名空间)(例如,.btn 和 .btn-danger)。    避免过度任意的简写。.btn 代表 button,但是 .s 不能表达任何意思。    class 名称应当尽可能短,并且意义明确。    使用有意义的名称。使用有组织的或目的明确的名称,不要使用表现形式(presentational)的名称。    基于最近的父 class 或基本(base) class 作为新 class 的前缀。    使用 .js-* class 来标识行为(与样式相对),并且不要将这些 class 包含到 CSS 文件中。 -->    <style>        /* Bad example */        .t { ... }        .red { ... }        .header { ... }        /* Good example */        .tweet { ... }        .important { ... }        .tweet-header { ... }    </style></head><body></body></html>

 

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
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.

HTML in Action: Examples of Website StructureHTML in Action: Examples of Website StructureMay 05, 2025 am 12:03 AM

HTML is used to build websites with clear structure. 1) Use tags such as, and define the website structure. 2) Examples show the structure of blogs and e-commerce websites. 3) Avoid common mistakes such as incorrect label nesting. 4) Optimize performance by reducing HTTP requests and using semantic tags.

How do you insert an image into an HTML page?How do you insert an image into an HTML page?May 04, 2025 am 12:02 AM

ToinsertanimageintoanHTMLpage,usethetagwithsrcandaltattributes.1)UsealttextforaccessibilityandSEO.2)Implementsrcsetforresponsiveimages.3)Applylazyloadingwithloading="lazy"tooptimizeperformance.4)OptimizeimagesusingtoolslikeImageOptimtoreduc

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 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

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),

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.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.