简体   繁体   English

设计 HTML 以轻松定制 CSS 的指南

[英]Guidelines for designing HTML for easy CSS customization

I am doing a Saas project, and we want each customer to be able to upload a css page for "his" web site.我正在做一个Saas项目,我们希望每个客户都能够为“他的”网站上传一个css页面。 (Although actually, all of the customer's sites are hosted by us using virtual hosting). (尽管实际上,所有客户的站点都由我们使用虚拟主机托管)。

What are good guidelines when designing the html of a page so that css can be applied a posteriori .在设计页面的 html 以便 css 可以后验地应用时,什么是好的指导方针。

You can have a look at the code at CSS Zen Garden , it's specifically designed to be stylable.您可以查看CSS Zen Garden中的代码,它专门设计为可样式化。

Generally, you should just use the most natural HTML elements for the function you want.通常,您应该只为您想要的功能使用最自然的 HTML 元素。 Lists should be lists, links for navigation, et.c.列表应该是列表、导航链接等。

Use div tags to separate the contents in logical divisions To make elements easy to target, set appropriate classes and id's on them.使用 div 标签来分隔逻辑分区中的内容 为了使元素易于定位,请在它们上设置适当的类和 id。

To make links really stylable you can simply add a span tag around the text:要使链接真正具有样式,您只需在文本周围添加一个 span 标签:

<a href="#"><span>text</span></a>

This way it's easy to give it a dynamic button like look by using the " sliding doors " technique, by adding one background to the anchor tag and another to the span tag.通过这种方式,通过使用“滑动门”技术,通过将一个背景添加到锚点标签并将另一个背景添加到跨度标签,可以轻松地为其提供类似动态按钮的外观。 If the link is not styled, the span tag is not affecting the layout at all.如果链接没有样式,span 标签根本不会影响布局。

你可以看看css Zen Garden ,看看他们是怎么做的。

Don't do it.不要这样做。

The customers will screw the design and will be calling you to reset it for them.客户会搞砸设计,并会打电话给您为他们重置。

Better make a BIG customization section with LOTS of options that would only produce valid values.最好制作一个包含大量只会产生有效值的选项的大自定义部分。 Allow images to be uploaded, check their size limits at the submission.允许上传图像,在提交时检查它们的大小限制。

A custom CSS may also be a source of attacks.自定义 CSS 也可能是攻击源。 We talked about it yesterday.我们昨天谈过了。

Is it safe to allow users to edit css? 允许用户编辑 css 是否安全?

Anyway, this kind of customization can only be performed for relatively simple pages, before all, just the pages.反正这种定制只能对比较简单的页面进行,毕竟只是页面而已。 We are talking here about a SaaS application which usually has a terribly complex UI trying to mimic classic desktop software.我们在这里谈论的是 SaaS 应用程序,它通常具有非常复杂的 UI,试图模仿经典的桌面软件。 I doubt customers will be able to correctly customize a respectively complex CSS.我怀疑客户是否能够正确定制各自复杂的 CSS。 Well, not without hiring a professional programmer that will be calling you back all the time to clear up things.好吧,并非没有聘请专业程序员,他会一直给你回电话以清理事情。 It will be easier to simply offer an advanced settings screen.简单地提供高级设置屏幕会更容易。

Some might be blatantly obvious:有些可能是显而易见的:

  • Name all your classes, IDs with a consistent and obvious naming scheme, but don't go naming everything, you can let smaller objects that are children to be unnamed objects if they are all identical.使用一致且明显的命名方案为所有类和 ID 命名,但不要命名所有内容,如果它们都相同,您可以让较小的子对象成为未命名对象。

  • Cut out Javascript that changes anything of the look of the page.删除改变页面外观的任何 Javascript。 Users will break it.用户会破坏它。

  • Don't use CSS reset stylesheets.不要使用 CSS 重置样式表。 It'll complicate things.它会使事情复杂化。 If users are going to deal with CSS, let them deal with the cross-browser issues.如果用户要处理 CSS,让他们处理跨浏览器的问题。

  • All of the HTML will be valid , won't it?所有的 HTML 都是有效的,不是吗?

Basically, start off with reasonably semantic (and valid!) html, and be sure to use a fairly wide array of elements (don't make everything a div) and classes (sprinkle liberally where it seems appropriate. Using a variety of elements makes for more readable css and html, which is important for easy stylability.基本上,从合理语义(和有效!)的 html 开始,并确保使用相当广泛的元素(不要把所有东西都变成一个 div)和类(在它看起来合适的地方自由地洒。使用各种元素使得为了更易读的 css 和 html,这对于简单的样式很重要。

Then, don't be afraid to add extra elements just to make styling easier.然后,不要害怕添加额外的元素只是为了使造型更容易。 For instance, if you have a table representing a grid of some sort (ie, not just a plain text table), and you'd like a particular data-cell to be the focus of attention, then although it may be possible to simply set a class on the td, you're better off nesting a new element within the td (and setting the class on that).例如,如果您有一个表示某种网格的表格(即,不仅仅是一个纯文本表格),并且您希望某个特定的数据单元成为关注的焦点,那么尽管可以简单地在 td 上设置一个类,最好在 td 中嵌套一个新元素(并在其上设置类)。 Extra nesting elements like this greatly reduce the need for complex css selectors which may not even be supported across browsers.像这样的额外嵌套元素大大减少了对复杂 css 选择器的需求,这些选择器甚至可能不被浏览器支持。 It's better for an element to have multiple classes than to have a single class with multiple meanings;一个元素有多个类比有一个具有多种含义的类要好; and it's better for a class to have the same meaning everywhere (ie not to be dependent on other classes).并且一个类最好在任何地方都具有相同的含义(即不依赖于其他类)。

Your html will be an API of sorts, so keeping it comprehensible is important.您的 html 将是各种 API,因此使其易于理解很重要。 Javascript makes things tricky, so if you need any, I'd try to keep javascript out of css's way - make sure than any modifications that javascript makes to the dom are consistent with your "API" - don't hardcode specific layout overrides into the javascript. Javascript 让事情变得棘手,所以如果你需要的话,我会尽量让 javascript 不受 css 的影响——确保 javascript 对 dom 所做的任何修改都与你的“API”一致——不要将特定的布局覆盖硬编码到javascript。 If javascript needs to make a popup to display extra info, say, then simply add a <div class="js-extrainfo-pop>...</div> to the document at an appropriate location and let the css decide how to style the popup, rather than dealing with z-indexes, display: absolute etc. in javascript. That way, the client CSS need learn only one API - your html structure - rather than work around a JS-wart. The DOM is a manageable interface, javascript is quite a bit more complex.如果 javascript 需要制作一个弹出窗口来显示额外的信息,那么只需在文档的适当位置添加一个<div class="js-extrainfo-pop>...</div>并让 css 决定如何在javascript中设置弹出窗口的样式,而不是处理z-indexes,display:absolute等。这样,客户端CSS只需要学习一个API - 你的html结构 - 而不是解决JS-wart。DOM是一个可管理的界面,javascript 有点复杂。

Finally, most customers are likely to want only minor modifications to your basic layout;最后,大多数客户可能只希望对您的基本布局进行微小的修改; so don't be afraid to use tables where necessary.所以不要害怕在必要时使用表格。 It's surprisingly tricky (often outright impossible) to apply grid-based layout via css without using tables.在不使用表格的情况下通过 css 应用基于网格的布局是非常棘手的(通常是完全不可能的)。 At the very least, you'll need to use a syntactic structure parallel to the table-tbody-tr-td structure, and let people style that as a table via css's display:table-* rules (which aren't supported everywhere equally well).至少,您需要使用与 table-tbody-tr-td 结构平行的句法结构,并让人们通过 css 的 display:table-* 规则(并非所有地方都同样支持)将其样式设置为表格好)。 If you're sure you need a grid, use a table and stop worrying.如果您确定需要网格,请使用表格并停止担心。 Real css "heroes" will override any of your "implicit" layout anyhow, and starting with a reasonable base point will make the initial unstyled page look more obvious, and will help clients make their own styling choices.真正的 css “英雄” 无论如何都会覆盖您的任何“隐式”布局,并且从一个合理的基点开始将使初始无样式页面看起来更明显,并将帮助客户做出自己的样式选择。

In summary,总之,

  1. Make sure your html is sufficiently verbose (when in doubt use an extra nested element or an extra class).确保您的 html 足够冗长(如有疑问,请使用额外的嵌套元素或额外的类)。
  2. Your html should be semantically "obvious" if only for readability如果只是为了可读性,您的 html 在语义上应该是“明显的”
  3. Don't be afraid to strongly hint at a particular type of layout.不要害怕强烈暗示特定类型的布局。
  4. Make sure your API is "clean" - don't introduce complexities by including style-sensitive javascript.确保您的 API 是“干净的” - 不要通过包含对样式敏感的 javascript 来引入复杂性。

Short tip: I think semantics are key in this approach.简短提示:我认为语义是这种方法的关键。 I've seen a lot of websites that tend to name their classes like floatleft , width100px or right_column .我已经看到了很多的网站,往往会说出自己类,如floatleft,width100pxright_column。 This means you are saying in the HTML how you expect the website to look like.这意味着您在 HTML 中说明了您期望网站的外观。 Give the CSS classes a semantic meaning, like sidebar or widget , this makes the HTML and CSS code much more readable.为 CSS 类赋予语义,例如sidebarwidget ,这使 HTML 和 CSS 代码更具可读性。

Maybe have a look at HTML5 and the new tags this will introduce?也许看看 HTML5 和这将引入的新标签? Nice blogpost can be found at Woork: CSS code structure for HTML5不错的博文可以在 Woork 中找到: HTML5 的 CSS 代码结构

I too am kind of going through this type of issue.我也正在经历这种类型的问题。 For class we needed to take all of the styling we had in HTML and strictly include it to only a css file.对于类,我们需要采用 HTML 中的所有样式,并将其严格包含在一个 css 文件中。 I wish that I had known about CSS and only needing to do styling within that file instead of having to go back through and fix everything that I messed up switching the style from HTML to CSS.我希望我知道 CSS 并且只需要在该文件中进行样式设置,而不必返回并修复所有我搞砸的将样式从 HTML 切换到 CSS 的问题。 I think if I were to have to design another website I'd lay everything out in a CSS file and then just input the content into and HTML file and then run them together.我想如果我必须设计另一个网站,我会将所有内容放在一个 CSS 文件中,然后将内容输入到 HTML 文件中,然后将它们一起运行。

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

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