简体   繁体   English

这是你如何构建CSS样式表?

[英]Is this how you would structure your CSS stylesheet?

Leaving aside the question of whether you should serve single or multiple stylesheets, assuming you're sending just one, what do you think of this as a basic structure? 撇开是否应该提供单个或多个样式表的问题,假设您只发送一个样式表,您认为这是一个基本结构?

/* Structure */ /* 结构体 */

Any template layout stuff should be put into here, so header, footer, body etc. 任何模板布局的东西都应放在这里,所以页眉,页脚,正文等。

/* Structure End */ / *结构结束* /

/* Common Components*/ / *通用组件* /

Repeated elements, such as signup forms, lists, etc. 重复元素,例如注册表单,列表等。

/* Common Components End*/ / *公共组件结束* /

/* Specific Page 1 */ / *具体的第1页* /

Some pages might have specific styles, that would go here. 有些页面可能会有特定的样式。

/* Specific Page 1 End */ / *具体Page 1结束* /

/* Specific Page 2 */ / *具体的第2页* /

As above 如上

/* Specific Page 2 End */ / *具体Page 2结束* /

/* Specific Page etc */ / *特定页面等* /

And so on. 等等。

/* Specific Page etc End */ / *特定页面等结束* /

That's similar to how I structure mine, however, I find that using sub-headings is the best way to do it, so I use this structure: 这与我的结构类似,但是,我发现使用子标题是最好的方法,所以我使用这个结构:

/************************* * GLOBAL * *************************/ / ************************* *全球* ********************* **** /

/* All of the common stuff goes here under the appropriate sub headings */ / *所有常见的东西都在适当的小标题下* /

/* Heading formatting */ / *标题格式* /

/* Text formatting */ / *文字格式* /

/* Form formatting */ / *表格格式* /

/* Table formatting */ / *表格格式* /

/* etc */ / * etc * /

/************************* * LAYOUT * *************************/ / ************************* *布局* ********************* **** /

/* All the layout things go here under sub-headings */ / *所有布局的东西都在小标题下* /

/* Header */ / *标题* /

/* Left Sidebar */ / *左侧边栏* /

/* Right Sidebar */ / *右侧边栏* /

/* Footer */ / *页脚* /

/************************* * NAVIGATION * *************************/ / ************************* *导航* ********************* **** /

/* I put navigation separate to the layout as there can be a number of navigation points under their sub-headings */ / *我把导航分开布局,因为在他们的小标题下可以有许多导航点* /

/* Main (horizontal) Navigation */ / *主要(水平)导航* /

/* Left Navigation */ / *左导航* /

/* Right Navigation */ / *右导航* /

/* Breadcrumb Navigation */ / *面包屑导航* /

/************************* * FORMS * *************************/ /************************* * 形式 * ********************* **** /

/* Any form formatting that varies from the common formatting, if there are multiple differently formatted forms, then use sub-headings */ / *任何表单格式与常见格式不同,如果有多个不同格式的表单,则使用子标题* /

/************************* * TABLES * *************************/ / ************************* *表格* ********************* **** /

/* Same deal as forms */ / *与表格相同的交易* /

/************************* * LISTS * *************************/ / ************************* * LISTS * ********************* **** /

/* Same deal as forms and tables */ / *与表格和表格相同的交易* /

/************************* * CONTENT * *************************/ / ************************* *内容* ********************* **** /

/* Any specific formatting for particular pages, grouped by sub-headings for the page the same way as forms, tables and lists */ / *特定页面的任何特定格式,按页面的子标题分组,与表格,表格和列表相同* /

/************************* * CSS SUPPORT * *************************/ / ************************* * CSS支持* ******************** ***** /

/* This is for any special formatting that can be applied to any element on any page and have it override the regular formatting for that item. / *这适用于任何特殊格式,可以应用于任何页面上的任何元素,并使其覆盖该项目的常规格式。 For example, this might have things like: */ 例如,这可能包含以下内容:* /

.float-right { float: right; }
.float-left { float: left; }
.float-center { margin-left: auto; margin-right: auto; }
.clear { clear: both }
.clear-block { display: block }
.text-left { text-align: left }
.text-right { text-align: right }
.text-center { text-align: center }
.text-justify { text-align: justify }
.bold { font-weight: bold }
.italic { font-style: italic }
.underline { border-bottom: 1px solid }
.nopadding { padding: 0 }
.nobullet { list-style: none; list-style-image: none }

/* etc */ / * etc * /

Hope that helps. 希望有所帮助。

I generally don't recommend writing on a single line like that though, or like suggested in the link Adam posted, they get very difficult to skim over if they get long. 我一般不建议像这样写一行,或者像Adam发布的链接中建议的那样,如果他们变长,他们就很难略过。 For the examples above, it was just quicker to type them that way so I didn't have to indent every line. 对于上面的示例,以这种方式键入它们更快,因此我不必缩进每一行。

For formatting I would recommend this structure: 对于格式化,我会推荐这种结构:

.class {
    width: 200px;
    height: 200px;
    border: 1px solid #000000;
}

And so on, I put the structure of the class or ID at the top, then any other formatting, like the font etc below that. 依此类推,我将类或ID的结构放在顶部,然后是任何其他格式,如下面的字体等。 Makes it very quick and clear to skim over. 让它非常快速和清晰地浏览。

Whatever makes sense to you is good enough. 无论什么对你有意义都足够好。 Frankly, when someone else comes looking for something in your stylesheet - or when you come looking for something, for that matter - they're not going to try to figure out what your organizing structure was. 坦率地说,当其他人来到您的样式表中寻找某些东西时 - 或者当您为此寻找某些东西时 - 他们不会试图弄清楚您的组织结构是什么。 They're just going to search for whatever class or element they need to see. 他们只是要搜索他们需要看到的任何类或元素。 So as long as you generally keep stuff that's related together, and section things off with comments like @Matt suggests, you're fine. 因此,只要您通常保留与之相关的内容,并通过@Matt建议的评论来解决问题,您就可以了。

The fact of the matter is that even with a very well-thought-out organizational structure - just like with a well-thought-out filing system - it's not always obvious what goes where; 事实上,即使有一个经过深思熟虑的组织结构 - 就像经过深思熟虑的文件系统一样 - 并不总是显而易见的。 so you're better off just being somewhat sensible, not devoting a lot of time to keeping things organized, and relying on search tools to find what you need. 所以你最好只是有点明智,不要花很多时间来保持组织有序,并依靠搜索工具找到你需要的东西。

I organize my CSS in a similar way as yours but I do start with a reset section. 我以与你类似的方式组织我的CSS但我确实从重置部分开始。 The main idea is to go from general to specific. 主要思想是从一般到具体。 So here it goes: 所以这里:

  • reset 重启
  • structure 结构体
  • html_tags HTML_TAGS
  • navigation 导航
  • specific sections 具体部分
  • Error messages - that's my last section 错误消息 - 这是我的上一节

The structure you presented is exactly what I use. 您呈现的结构正是我使用的结构。 However, it seems to me that it still got too complex with new rules showing up and overriding each other... Perhaps I should try to stick to the solution suggested in the topic linked to by Adam instead. 然而,在我看来,它仍然过于复杂,新规则出现并超越对方......也许我应该尝试坚持与Adam相关联的主题中提出的解决方案。

It seems like every time I create a new css file, I find a new way to organize it. 似乎每次我创建一个新的css文件时,我都会找到一种新的方法来组织它。 And they are ALL better than the previous. 他们比以前更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM