简体   繁体   English

内联样式与 CSS 中的样式

[英]Inline styles vs styles in CSS

I know placing all your styles in a CSS file is the best thing to do as it is a lot neater.我知道将所有样式放在 CSS 文件中是最好的做法,因为它更整洁。

But does it REALLY matter if the styles are inline or in a CSS?????但是,如果样式是内联的还是在 CSS 中,这真的很重要吗?????

Edit below在下面编辑

My plan is to just place the styles in my MasterPage and all other pages will use the MasterPage....I believe the correct term is not "INLINE" but Embedded???我的计划是将样式放在我的 MasterPage 中,所有其他页面都将使用 MasterPage ....我相信正确的术语不是“内联”而是嵌入式???

Some thoughts from one with experience, rather than a 'purist':来自有经验的人的一些想法,而不是“纯粹主义者”:

Storing all styles, for a large application, in one CSS file is not maintainable.对于大型应用程序,将所有样式存储在一个 CSS 文件中是不可维护的。 You'll have perform a text search of the file to find the style you're looking for, or scroll a lot, and there's a higher chance that you'll overlook related styles when making an update.您必须对文件执行文本搜索以找到您要查找的样式,或者滚动很多,并且在进行更新时您更有可能忽略相关样式。

If certain styles are particular to a page, not globally used, it is more maintainable to keep them in a style tag within the head tag.如果某些样式特定于页面,而不是全局使用,则将它们保存在 head 标签内的样式标签中更易于维护。

Deep CSS inheritance hierarchies are also not maintainable.深层 CSS 继承层次结构也无法维护。 These are much, much worse than inline styles!这些比内联样式要糟糕得多! The CSS language itself does a poor job of applying styles to many elements in more complex structures. CSS 语言本身在将样式应用于更复杂结构中的许多元素方面做得很差。 Consider lesscss, sass, or even jQuery for more than basic application of styles.考虑使用lesscss、sass 甚至jQuery 来实现更多样式的基本应用。

Lots of developers use HTML for presentation, mostly DIVs, when they think they are doing the right thing, or lecturing others.许多开发人员在他们认为自己在做正确的事情或向他人演讲时使用 HTML 进行演示,主要是 DIV。 Some example above!上面的一些例子!

It matters because your code becomes very difficult to maintain or update if you use inline styles.这很重要,因为如果您使用内联样式,您的代码将变得非常难以维护或更新。 Keeping your styles in style tags or separate CSS files allows you to comply with Don't Repeat Yourself , which is probably the most important development principle.将样式保存在样式标签或单独的 CSS 文件中可以让您遵守不要重复自己,这可能是最重要的开发原则。

That being said, if you are absolutely certain that a piece of styling is unique to a given element, and also that it won't ever need to be tweaked, you can feel free to use inline styling.话虽如此,如果您绝对确定某个样式对于给定元素来说是独一无二的,并且永远不需要调整,那么您可以随意使用内联样式。 I sometimes use inline style for throwaway code and for things like landing pages (once they're done, they're done).我有时将内联样式用于一次性代码和登录页面之类的内容(一旦完成,就完成了)。

Using Inline CSS:使用内联 CSS:

  • Repeat the same rule for every element in the page.对页面中的每个元素重复相同的规则。
  • More code and bigger file size to transfer to the client.更多的代码和更大的文件大小传输到客户端。
  • Harder to maintain, suppose you want to change the width to 200px, you will need to go through all the page and edit one by one.更难维护,假设您想将宽度更改为 200px,您将需要浏览所有页面并逐一编辑。

inline:内联:

<div style="width:100px; height:100px;"></div>
<div style="width:100px; height:100px;"></div>

external OR put css classes in the head [embedded styling]:外部 OR 将 css 类放在头部 [嵌入式样式]:

<div class="big"></div>
<div class="big"></div>

Based on your edit: that seems not to be inline CSS as in my example above, it is the same idea as using an external file, so if you want to do that go ahead, it is the same.根据您的编辑:在我上面的示例中,这似乎不是内联 CSS,它与使用外部文件的想法相同,因此如果您想继续这样做,它是相同的。

Storing styles in one document helps you to control on your entire project.将样式存储在一个文档中可帮助您控制整个项目。 Furthermore less code to maintain and applying changes.此外,用于维护和应用更改的代码更少。

No but it is alot easier to make changes to the css if you only have to look one place instead of all your headers/inline不,但如果您只需要查看一个地方而不是所有标题/内联,则对 css 进行更改会容易得多

One other thing, your markup looks alot cleaner if you dont have eny css/javascript inline另一件事,如果您没有内联的 css/javascript,您的标记看起来会更干净

When creating master pages I use in-line styles to create the basic layout of the page.创建母版页时,我使用内嵌样式来创建页面的基本布局。 For instance I include all of the styles that position the header at the top of the page, main content in the middle and footer at the bottom.例如,我包含了所有将页眉置于页面顶部、主要内容在中间、页脚在底部的样式。 Pretty much every style attribute related to positioning, I include in the masterpage as an inline style.几乎所有与定位相关的样式属性,我都作为内联样式包含在母版页中。

维护起来要容易得多……这真的很重要取决于您认为什么是重要的……为什么不使用 css 文件?

您的意思是将您的样式放在 with 中还是将它们作为 'style="x"' 附加到您的元素中?

There's several reasons for avoinding inline CSS.避免使用内联 CSS 有几个原因。

1) Maintenance, it's easier to make changes to a code where all css is seperated from the markup itself. 1)维护,更容易对所有css与标记本身分开的代码进行更改。 It also makes the code more readable as avoiding alot of inline css gives you less code.它还使代码更具可读性,因为避免大量内联 css 会为您提供更少的代码。

<div class='test'></div>

is easier on the eye than:对眼睛来说比以下更容易:

<div style='background:yellow;width:10000px;height:10px;position:absolute;top:10003px;left:132032px;'></div>

When the css is inline you will also have a hard time finding where the code itself is and comparing styles.当 css 内联时,您也将很难找到代码本身的位置并比较样式。 You will also often end up repeating the same code several times because you can't use classes.由于无法使用类,您还经常会多次重复相同的代码。

2) Performance, CSS files can be gzipped, making for a smaller load. 2) 性能,CSS 文件可以压缩,从而减少负载。 It's also easier for the browser to handle when it get js and css served as files.当浏览器将 js 和 css 作为文件提供时,它也更容易处理。

3) Keeping with the best practice. 3) 遵循最佳实践。 Some other poor developer might want to edit your code later, and he sure would be happy if you kept away from inline CSS.其他一些糟糕的开发人员可能想稍后编辑您的代码,如果您远离内联 CSS,他肯定会很高兴。

Now of course you can do CSS in the head of a document too, but why make your files bigger than they need to be?现在当然你也可以在文档的头部做 CSS,但是为什么要让你的文件比它们需要的更大呢? More code into the same file makes for more mess.同一个文件中的更多代码会造成更多混乱。 And you can't gzip it if you do.如果你这样做,你就不能 gzip 它。

@Etienne , there is one disadvantage doing this way , if you want to deploy any changes to production you have make a build and push it. @Etienne ,这样做有一个缺点,如果您想将任何更改部署到生产中,您必须进行构建并推送它。

If you maintain everything as css , you can just push the css file changes and invalidate the load balancer cache.如果您将所有内容都保留为 css ,则只需推送 css 文件更改并使负载平衡器缓存无效。

I thought this is a good point to mention.我认为这是一个很好的提点。

When it is best to use inline style什么时候最好使用内联样式

Inline style is the best solution in situations when some style is created dynamically from user input via server-side code (ex, WordPress plugin), to be applied only to a single HTML element, in such cases insert it into an external CSS file causes only problems:当某些样式是通过服务器端代码(例如 WordPress 插件)从用户输入动态创建的情况下,内联样式是最佳解决方案,仅应用于单个 HTML 元素,在这种情况下将其插入外部 CSS 文件会导致唯一的问题:

  • There is the need for a server-side code that creates a CSS class with the dynamic style inside it.需要一个服务器端代码来创建一个包含动态样式的 CSS 类。
  • There is the need for a server-side code that write and save the .css file需要编写和保存 .css 文件的服务器端代码
  • There is the need for a server-side code that is able to link the CSS classes created to the correct HTML elements You must load an external CSS file for no reason, this is a downgrade of performance (file size and 1 more HTTP request)需要一个能够将创建的 CSS 类链接到正确的 HTML 元素的服务器端代码 您必须无缘无故地加载外部 CSS 文件,这是性能的降级(文件大小和 1 个 HTTP 请求)
  • In many cases, where the dynamic codes are just one or two, the problems are startling clears: you must create a file of ex.在许多情况下,动态代码只有一两个,问题出人意料地清晰起来:您必须创建一个 ex.txt 文件。 800bytes with 2 lines of code, and load it as external files. 800bytes 2行代码,作为外部文件加载。 Greater exposure to bugs.更容易接触到错误。 More a code is complex more are chances of bugs.代码越复杂,出现错误的机会就越大。 The server-side codes above are very complex in comparison to the simplicity of the task they do.与它们完成的任务的简单性相比,上面的服务器端代码非常复杂。

Real use-case:实际用例:

Imagine a scenario where a user wants to upload an image and use it as a background in an HTML element.想象一个场景,用户想要上传图像并将其用作 HTML 元素中的背景。 With old rule is just style="background-image:URL()".旧规则只是style="background-image:URL()". with the new rule some code must create and save an external file, with just the code style="background-image:URL()" , create a CSS class for it, write it in the HTML component, load this CSS file with just one line of code.使用新规则,一些代码必须创建并保存一个外部文件,只需使用代码style="background-image:URL()" ,为其创建一个 CSS 类,将其写入 HTML 组件中,然后加载此 CSS 文件一行代码。 Totally nonsense.完全是胡说八道。 Consider also that this operation must be done every time the user updates the image.还要考虑到每次用户更新图像时都必须执行此操作。

Final result:最终结果:

  • Worst performance due to 1 more HTTP request and large, complex, server-side codes.由于 1 个 HTTP 请求和大量复杂的服务器端代码导致性能最差。
  • Wasting hours of time for authors to develop something that is not only useless but also creates real problems.浪费作者数小时的时间来开发不仅无用而且会产生实际问题的东西。
  • At least one more file in the project项目中至少多了一个文件
  • Worst readability/comprehensibility of the code because a user must check the HTML and then find the CSS code of the linked CSS class to see the value of the style.代码的可读性/可理解性最差,因为用户必须检查 HTML,然后找到链接的 CSS 类的 CSS 代码才能看到样式的值。

When it is best to use an external CSS file何时最好使用外部 CSS 文件

In all other cases than the ones explained above, in short, when the style is fixed and never change you should put it in an external CSS file.在除上述情况之外的所有其他情况下,简而言之,当样式固定且永不更改时,您应该将其放在外部 CSS 文件中。

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

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