什么是最好的方式使用CSS? (NOT * learn * but really * use *) [英] What is the best way to use CSS? (NOT *learn* but really *use*)
问题描述
我知道CSS和我正在学习越来越常见的模式。然而,我总是觉得我是黑客,而不是做正确的方式。
例如,我尊重的一些人告诉我,使用css reset是不必要的,而另一些人为它而活。此外,有这么多的伟大的CSS框架(网格等),甚至认为我理解他们,我从来不真正知道什么时候使用它们。
所以,基本上,我的问题是:一旦你了解CSS的工作原理,是否有一个公认的最好的在Python中,应该尝试使用通用模式并读取PEP 8.或者,在C ++中,在理解语法之后,通过Meyer读取有效的系列是一个非常好的第二读。
对不起,花了这么长时间来解释..我只是不想有答案like:阅读Beginning CSS,解释如何更改背景或如何设置字体。我真的在寻找一个好的标准方法。
- 我们应该使用重置吗?
- 我们每个网站应该只使用一个档案吗?一个是主页,一个是其余的?一个基本文件,每个大部分一次不同?
- 有一个2k + css文件不好吗?是否意味着它应该被重构,并且它包含多少重复?
- 我应该在顶部定义正常字体,颜色,h1等的父项,然后,不同的改变它每个部分..或者改为总是使用标准的一个,并重新定义每个部分。
- 我应该在所有地方使用.class和#id,或者我应该尝试最小化它们,而使用长描述符,例如:
.content .main tr td span a
或
span.classname a
谢谢!
tl / dr:
一旦你已经了解了CSS,但想要以干净/专业的方式使用它,最好的第二读取是什么?
感谢您的回答。我知道我问了很多问题,但他们只是真正的问题的例子是:最好的第二阅读一旦你已经了解CSS,但希望使用它在一个干净/专业的方式。即,我希望读一本书,解释我提出的例子..但也将解释很多其他的东西,不是CSS语法,但更多的CSS最好的专业使用。
人们会对这些问题有一系列的答案,但这里是我会采取的方法:
重置
如果您使用的是脆弱的布局,几个像素不是你期望他们是 - 考虑使用重置。我会查看 normalize.css 。
您也可以考虑重置特定的元素,如果你发现自己添加了很多
- Google建议将其拆分,以便各个网页不会强制用户下载他们访问的网页上实际未使用的大量样式表规则。
- Yahoo建议合并文件以最小化HTTP请求数
- 很明显,找到平衡很重要,而且这个SO问题权衡一些优点和缺点。
选择器的类和ID或长链?
我尽量保持类和ID的最小(在我的HTML和我的CSS)。当你创建页面时,其他人会用所见即所得编辑器更新它们,他们往往更脆弱。我向页面的大块中添加了几个ID或类,然后使用CSS定位这些块中的特定元素。
使用CSS预处理器(如 LESS 或 SASS 可帮助您编写更多可读的样式表。在LESS和SASS中嵌套样式规则的能力帮助我避免了很多与特定性相关的问题。
然而,特异性是一个很好的CSS主题, :
- CSS-tricks有一篇涵盖特异性的文章
- W3C自己的文档值得阅读。
其他资源
至于其他阅读...
- SitePoint的CSS文章和教程集是一个用于更高级CSS教程的好资源,并且它们还包含一些很好的文章,涵盖的CSS问题比许多初学者水平的书更高级。
I know CSS and I'm learning more and more common pattern. However, I always feel like I'm hacking instead of doing it the right way.
For instance, some people I respect tell me that using css reset is unnecessary while others live for it. Also, there are so many great css framework (for grids, etc.) that even thought I understand them, I never really know when to use them.
So, basically, my question is: Once you understand how CSS work, is there a recognized "best" approach used by excellent web developer? As in python, one should try to use the common pattern and read PEP 8. Or, in C++, after understanding the syntax, reading the effective serie by Meyer is an excellent "second" read.
Sorry for taking that long to explain.. I just didn't want to have answer like: Read "Beginning CSS" which explain how to change the background or how to set font. I'm really looking for a good standard approach.
- Should we use reset?
- Should we use only one file per site? One for homepage and one for the rest? One basic file and once different for every big sections?
- Is it bad to have a 2k+ css files? Does it mean it should have been refactored and that it contain to much duplicate?
- Should I define parent at the top for normal font, color, h1, etc., and then, when it's different change it per sections.. or instead always use the standard one and redefine each and every section.
- Should I use .class and #id a little bit everywhere, or should I try to minimize them and instead use long descriptor such as:
.content .main tr td span a
or
span.classname a
Thank you!
tl/dr:
What's the best "second" read once you already understand CSS but would like to use it in a clean/professional way?
[EDIT]
Thanks all for your answer. I know I asked quite a lot of questions.. but they were only examples for the real question which is: What is the best "second" read once you already understand CSS but would like to use it in a clean/professional way. I.e., I was hoping to read a book explaining the examples I proposed.. but also would explain lots of other things that aren't css-syntax but more css-best-professional-use.
People will have a range of answers for these questions, but here are the approaches I would take:
Resetting
If you're working on a fragile layout -- one that could easily break if a few pixels aren't where you expect them to be -- consider using a reset. I would look into normalize.css. Instead of completely overwriting browser defaults, it smooths out the differences between browsers.
You could also consider resetting specific elements if you find yourself adding a lot of margin: 0;
to your stylesheets.
Splitting up CSS documents
- Google recommends splitting them up so that individual pages aren't forcing users to download lots and lots of stylesheet rules that aren't actually used on the page that they're visiting.
- Yahoo recommends combining files to minimize the number of HTTP requestion
- Obviously, finding a balance is important, and this SO question weighs some of the pros and cons.
Classes and IDs or long chains of selectors?
I try to keep classes and IDs to a minimum (in both my HTML and my CSS). They tend to be more fragile when you're building pages that others will update with WYSIWYG editors. I add a few IDs or classes to large blocks of the page, then use CSS to target specific elements within those blocks. This is easier if you avoid deep nesting in your HTML as much as possible.
Working with a CSS preprocessor such as LESS or SASS can help you write more readable stylesheets. The ability to nest style rules in both LESS and SASS has helped me avoid a lot of specificity-related issues.
Still, specificity is a good CSS topic to be familiar with:
- CSS-tricks has a good article covering specificity
- The W3C's own documentation is worth reading.
Additional Resources
As far as additional reading is concerned...
- SitePoint's collection of articles and tutorials on CSS is a great resource for more advanced CSS tutorials, and they also feature some good articles covering CSS issues that are more advanced than what you'd find in many beginner-level books.
这篇关于什么是最好的方式使用CSS? (NOT * learn * but really * use *)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!