Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the different manifestations of CSS code formatting

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

这篇文章主要介绍了CSS代码格式化的不同表现方式是什么,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

格式化与CSS的功能无关。这些仅仅是程序员的自己选择,但这并不是说格式化对于css来说不重要,就好比说画布的选择对画家来说并不重要,这是不对的,格式化会影响编写CSS的感觉,阅读的容易程度,导航的容易程度以及重新访问和重新熟悉之前编写的CSS的难易程度。

CSS格式之所以有这么多选择,是因为当涉及到间隔和换行时,没有严格的语法规则。例如:

div{width:50px}

与下面两个的代码是相同的

div{width:50px}

div

{

width:50px

}

多行格式

.navigation_rss_icon{

position:absolute;

left:940px;

bottom:0px;

}

#navigation_rss{

position:absolute;

left:720px;

font-family:Verdana,Arial,Helvetica,sans-serif;

text-transform:uppercase;

color:#897567;

line-height:2.5em;

}

#navigation_rssli{

display:inline;

}

#navigation_rsslia:link,#navigation_rsslia:visited{

color:#fffffe;

text-decoration:none;

padding:0px2px;

letter-spacing:-0.05em;

}

#navigation_rsslia:hover{

color:#eed2a1;

text-decoration:none;

}

我敢说这是最常见的,当涉及到短的代码片段时,它是最容易阅读的,这就是为什么书面教程最常使用这种格式的原因。上面的示例在右大括号和下一个选择器之间没有空行,但这也很常见。

具有缩进的多行格式

.navigation_rss_icon{

position:absolute;

left:940px;

bottom:0px;

}

#navigation_rss{

position:absolute;

left:720px;

font-family:Verdana,Arial,Helvetica,sans-serif;

text-transform:uppercase;

color:#897567;

line-height:2.5em;

}

#navigation_rssli{

display:inline;

}

#navigation_rsslia:link,#navigation_rsslia:visited{

color:#fffffe;

text-decoration:none;

padding:0px2px;

letter-spacing:-0.05em;

}

#navigation_rsslia:hover{

color:#eed2a1;

text-decoration:none;

}

缩进的块表示选择器是比其上方的父级更具体的选择器,并且指向上述选择器的子元素。

单行格式

div.wrapper{margin:0auto;padding:200px000;width:960px;z-index:2}

ul.nav{position:absolute;top:0;left:430px;padding:120px000}

ul.navli{display:inline;margin:010px00}

div.column{float:left;margin:070px00;padding:00070px;width:340px}

div.post_wrapper{background:url(http://cdn.images.elliotjaystocks.com/presentation/hr_long.png)bottomcenterno-repeat;margin:0040px0;padding:0040px0}

div.wrapperimg,div.wrapperaimg,div.article_illustration_mini{background:#d3d4cb;padding:10px;border:1pxsolid#999}

div.wrappera:hoverimg{background:#fff}

这可能是最有效的空间和尺寸,没有完全压缩以移除所有空间和换行符。在编写和编辑CSS时,这种技术需要最少的垂直和水平滚动,但是看起来很麻烦并且有些难以浏览和查找你正在寻找的东西。

带Tab键的单行格式

h2,h3,h4{font:24pxHelvetica,Sans-Serif;margin:0010px0;}

h3a,h3a:visited{color:#2e2e2e;}

h3a:hover{color:#fe4902;border-bottom:1pxdotted#2e2e2e;}

p,li,dd{font:13px/18px"LucidaGrande",Arial,Helvetica,Sans-Serif;margin:0015px0;color:#5e5d5d;}

td,th{font:13px/18px"LucidaGrande",Arial,Helvetica,Sans-Serif;text-align:left;}

具有缩进的单行格式

#content-areaol{margin:15px0025px;list-style:decimal;}

#content-areaolol{list-style:lower-alpha;}

#content-areaul{margin:0005px;list-style:none;}

#content-areaulli{padding:00020px;background:url(/images/bullet.png)03pxno-repeat;}

#content-areaulul{margin:15px0025px;list-style:disc;}

#content-areaululli{background:none;padding:0;}

一个缩进的选择器表示选择器的目标是它上面的选择器的子元素。

主要是单行格式

我最喜欢的就是单行格式,因为我在文本编辑器中使用Soft-Wrap,因此长行不会永远持续,它们会在窗口边缘处包裹。因此,对于具有大量选择器的非常长的行,我在新的属性行上添加了一个硬回车和制表符。

h2,h3,h4{font:24pxHelvetica,Sans-Serif;margin:0010px0;}

h2{font-size:36px;}

h3{font-size:30px;}

h3a,h3a:visited{color:#2e2e2e;}

h3a:hover{color:#fe4902;border-bottom:1pxdotted#2e2e2e;}

p,li,dd{font:13px/18px"LucidaGrande",Arial,Helvetica,Sans-Serif;

margin:0015px0;color:#5e5d5d;}

td,th{font:13px/18px"LucidaGrande",Arial,Helvetica,Sans-Serif;

text-align:left;}

变化

单行格式可以通过将开括号移动到它自己的行上来进一步实现,这是我在PHP中看到的相当多的东西:

div

{

padding:10px;

}

在带有标签的多行格式中,我看到了用作分隔墙的大括号:

#content-areaol{margin:15px0025px;list-style:decimal;}

#content-areaolol{list-style:lower-alpha;}

#content-areaul{margin:0005px;list-style:none;}

#content-areaulli{padding:00020px;background:url(/images/bullet.png)03pxno-repeat;}

#content-areaulul{margin:15px0025px;list-style:disc;}

#content-areaululli{background:none;padding:0;}

组合

单行和多行的组合可以将相关属性分组到一行:

.navigation_rss_icon{

position:absolute;

top:10px;left:10px;bottom:10px;right:10px;

font-size:12px;font-weight:bold;

}

您选择的格式归结为可读性。您需要能够快速导航CSS并找到您要查找的内容并快速进行更改。如果您发现单行格式很难,因为您的眼睛很难找到您正在寻找的属性,那么您应该避免使用它。

就个人而言,我发现多行格式易于阅读,但它将长度(如实际行数)增加了5-8倍。由于所有垂直滚动,这实际上使整个文档对我来说不太可读。如果您的监视器有点狭窄,单行格式可能会导致水平滚动,有时甚至更糟。

完美的格式是最大限度地提高可读性,同时最大限度地减少滚动。另外,从更抽象的意义上讲,它只需要感觉正确。

感谢你能够认真阅读完这篇文章,希望小编分享的"CSS代码格式化的不同表现方式是什么"这篇文章对大家有帮助,同时也希望大家多多支持,关注行业资讯频道,更多相关知识等着你来学习!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report