现有网站 CSS 替换(重新换肤)最佳实践,无需更改 HTML

发布于 2024-08-27 02:28:08 字数 1928 浏览 8 评论 0原文

我可以看到与堆栈溢出上的 CSS 最佳实践相关的问题的许多其他很好的答案:

但我认为我有一个不同的问题。

我正在尝试“重新设计”一个使用 divul 等精心构建的现有网站,并且它具有良好的现有 CSS文件,但当我开始更改 CSS 时,我很快发现我破坏了布局。我的感觉是,很难了解所有 CSS 如何协同工作,以及 CSS 到底在影响 HTML 中的父元素和同级元素。

所以,我的问题是“通过仅替换 CSS 而不修改现有 HTML 来重新设计现有网站的最佳实践是什么?”我无法更改类、ID、节点层次结构等。

我尝试重新设计的特定站点的示例是 http://demo.nopcommerce.com/

现有的 CSS 可以像主 CSS 文件中的摘录一样复杂/详细:

.header-selectors-wrapper
{
    text-align: right;
    float: right;
    width: 500px;
}

.header-currencyselector
{
    float: right;
}

.header-languageselector
{
    float: left;
}

.header-taxDisplayTypeSelector
{
    float: right;
}

.header-links-wrapper
{
    float: right;
    text-align: right;
    width: 570px;
}

.header-links
{
    border: solid 1px #9a9a9a;
    padding: 5px 5px 5px 5px;
    margin-bottom: 5px;
    display: inline-table;
}

.order-summary-content .cart .cart-item-row td, .wishlist-content .cart .cart-item-row td
{
    border-bottom: 1px solid #c5c5c5;
    vertical-align: middle;
    line-height: 30px;
}

.order-summary-content .cart .cart-item-row td.product, .wishlist-content .cart .cart-item-row td.product
{
    text-align: left;
    padding: 0px 10px 0px 10px;
}

.order-summary-content .cart .cart-item-row td.product a, .wishlist-content .cart .cart-item-row td.product a
{
    font-weight: bold;
}

任何帮助将不胜感激。

I can see a number of other good answers to questions relating to CSS best-practices on stack overflow:

But I think I have a different problem.

I'm trying to "re-skin" an existing site that has been nicely built using div's and ul's, etc, and it has a good existing CSS file, but when I start making changes to the CSS I quickly find that I break the layout. My feeling is that it is very hard to get a feel for how all the CSS will work together and indeed what CSS is affecting parent and sibling elements in the HTML.

So, my question is "what are the best-practices around re-skinning an existing web-site by replacing the CSS only and not modifying the existing HTML?" I can't change the classes, ids, node hierarchy, etc.

An example of the particular site that I am trying to re-skin is http://demo.nopcommerce.com/.

The existing CSS can be as complicated/detailed as this extract from the main CSS file:

.header-selectors-wrapper
{
    text-align: right;
    float: right;
    width: 500px;
}

.header-currencyselector
{
    float: right;
}

.header-languageselector
{
    float: left;
}

.header-taxDisplayTypeSelector
{
    float: right;
}

.header-links-wrapper
{
    float: right;
    text-align: right;
    width: 570px;
}

.header-links
{
    border: solid 1px #9a9a9a;
    padding: 5px 5px 5px 5px;
    margin-bottom: 5px;
    display: inline-table;
}

.order-summary-content .cart .cart-item-row td, .wishlist-content .cart .cart-item-row td
{
    border-bottom: 1px solid #c5c5c5;
    vertical-align: middle;
    line-height: 30px;
}

.order-summary-content .cart .cart-item-row td.product, .wishlist-content .cart .cart-item-row td.product
{
    text-align: left;
    padding: 0px 10px 0px 10px;
}

.order-summary-content .cart .cart-item-row td.product a, .wishlist-content .cart .cart-item-row td.product a
{
    font-weight: bold;
}

Any help would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

酒儿 2024-09-03 02:28:08

crimson_penguin 和 Jon P 都推荐了一些很好用的工具,所以我将为您提供一些一般性建议。

这就是我在你的情况下会做的事情,尽管这绝不是解决方案。

首先,了解您希望最终产品的外观如何。准备好设计,这样您从一开始就知道您想要的结果。也不要忽略任何细节,“训练中流的汗越多,战斗中流的血就越少”

其次,熟悉当前的 CSS,因为你很快就会熟悉它。如果您觉得有必要对所有内容进行重新排序,以便您更喜欢某些内容,也许可以通过将按特定层次结构起作用的内容分组在一起。

例如:

#navigation {....}
   #navigation #left {....}
     #navigation #left h1 {....}
     #navigation #left p {....}
   #navigation #right {....}

一旦您有了设计并了解当前的代码,就开始进行一些更改。首先从简单的低级元素开始,例如文本块或图像。然后你就可以围绕这些进行构建。正如前两张海报提到的,Firebug(适用于 Firefox)和 DOM-Inspector(适用于 Safari/Chrome)非常有帮助,因为您可以在页面上尝试一些东西,而无需做出任何承诺,直到您看到它的外观。

这是一个漫长且可能充满压力的过程,但只要有一点耐心和毅力,您很快就会到达目的地。

crimson_penguin and Jon P have both suggested good tools to use, so I'll throw a bit of general advice in for you.

Here's what I'd do in your situation, although it's by no means THE solution.

First of all, know how you'd like the end product to look. Get a design ready so you know from the beginning where you want to end up. Don't skimp on any details either, "The more you sweat in training, the less you bleed in battle"

Secondly, get familiar with the current CSS as you'll be getting very intimate with it soon. Re-order everything if you feel it necessary to something you're more comfortable with, perhaps by grouping things together that act on particular hierarchies.

For example:

#navigation {....}
   #navigation #left {....}
     #navigation #left h1 {....}
     #navigation #left p {....}
   #navigation #right {....}

Once you have a design and know the current code, start making some changes. Start with simple, low-level elements first such as a block of text or an image. Then you can build up around these. As the two previous posters mentioned, Firebug (for Firefox) and the DOM-Inspector (for Safari/Chrome) can be very helpful as you can try things out on the page without committing to anything until after you've seen how it looks.

It's a long and potentially stressful process, but with a bit of patience and perseverance you'll get there in no time.

因为看清所以看轻 2024-09-03 02:28:08

如果我这样做,我将广泛使用 WebKit 的 DOM 检查器(在 Safari 或 Chrome 中右键单击页面并选择“检查元素”即可使用)。我希望它能帮助你理解节点/类/id 结构,并在你编写新的 css 时帮助调试它。

它比仅仅查看源代码方便得多,因为它是在树视图中,并且默认情况下节点是折叠的。它具有一些非常有用的功能,用于检查节点的度量(宽度/高度/填充/边框/边距),以及显示适用于它们的样式、顺序以及最终使用的样式。

If I were doing this, I would be using WebKit's DOM-inspector extensively (available in Safari or Chrome by right-clicking on a page and selecting Inspect Element). I expect it would help a lot with both wrapping your mind around the node/class/id structure, as well as helping to debug the new css as you're writing it.

It's much more convenient than just looking at the source, because it's in tree view, and nodes are collapsed by default. And it has some very useful features for inspecting the metrics (width/height/padding/border/margin) of nodes, as well as showing what style applies to them, in what order, and the final style being used.

少女净妖师 2024-09-03 02:28:08

我建议使用 Firefox 的 Firebug 插件。您可以在浏览器中查看页面时通过编辑现有 CSS 来预览 CSS 更改,而无需对 CSS 文件进行永久更改。您还将了解样式的“级联”等。

这也是一个非常方便的 JavaScript 调试工具。

I would suggest using the Firebug plug in for Firefox. You can use this to preview changes to the CSS by editing the existing CSS while viewing the page in the broweser without making permanent changes to the CSS file. YOu will alos get an idea of the "Cascade" of styles etc.

A very handy tool aswell for javascript debugging.

俯瞰星空 2024-09-03 02:28:08

我们刚刚开始对我的工作进行大规模的重新设计 - 大约 13000 个 JSP 页面,应用程序有超过 150 万行代码。

在我们的第一次计划会议上,我大笑起来,因为在大多数情况下,每个人都假设我们将对每个页面进行手动重构。手动重构绝对是我最不会考虑做的事情——这是最昂贵的方式,但产生的好处却最少。您获得的软件版本不会使重新设计变得更容易,但在其余时间必须并行维护。换句话说,你承担了责任。

无论如何,我将建议创建或找到一个实用程序来扫描 JSP 页面的 HTML 输出,以便它可以捕获每个元素上使用的实际最终 CSS。这将避免我们编写自己的 CSS 解析器来模仿 CSS 选择器的规则等。相反,我们将获得每个元素的每个 CSS 值的完整最终列表,并将此信息写入一组数据库表。从那里,我们可以分析 CSS 寻找共性,然后将其编写为共享类或混合(使用 SASS),并将其余部分编写为使用这些共享定义的元素特定类。

输出所有 CSS 后,该工具将在每个元素上使用适当的 CSS 类重写 JSP 文件。

应该指出的是,所讨论的任何页面之间几乎没有共同点。我认为,当我们对工具进行多次迭代微调时,我们也许可以将页面的特定部分识别为属于一组页面级组件,如面板、字段组、容器等。如果我们可以的话识别每个页面中的这些元素,我们最终可以注入实际的可重用组件来代替现有的 HTML。

我认为这可能是唯一有效的方法,有机会以一定程度的信心转换页面,确保没有任何内容被破坏。我们根本不会更改 HTML 元素结构,因此最初,该工具的输出应该与当前完全相同,但结构到位后,我们就可以开始对整个应用程序的外观进行更改对正确的类进行单点编辑等。

为此,我已经找到了一个可以完全执行我所描述的操作的工具 - 它来自 LinkedIn,名为 CSS Blocks。他们提到了另一个用于优化 CSS 的工具,名为 OptiCSS(听起来像“光学”),但显然它还没有完全准备好迎接黄金时段。

不管怎样,我希望其中的一些内容有意义并且有用。如果有问题或意见,请随时给我发电子邮件。

We are just beginning a massive reskinning effort at my work - some 13000 JSP pages, with the application being over 1.5 million lines of code.

I laughed my behind off at our first planning meeting because for the most part, everyone was assuming we are going to do a manual refactoring of each page. Manual refactoring would be the absolute last thing I would consider doing - it's the most expensive way of doing it that produces the least benefits. You get a version of the software that doesn't make reskinning any easier and yet has to be maintained in parallel for the rest of time. In other words, you get a liability.

Anyway, what I am going to be proposing is to create or find a utility that will scan the HTML output of the JSP pages so that it can capture the actual final CSS being used on every element. This will avoid us having to write our own CSS parser that mimics the rules of CSS selectors, etc. Instead, we will get the full final list of every CSS value for each element and write this information out to a set of database tables. From there, we can analyze the CSS looking for commonalities that we can then write out as shared classes or mixins (using SASS), and write the rest out as element specific classes that use those shared definitions.

After all of the CSS is output, the tool will then rewrite the JSP files with the appropriate CSS classes on each element.

It should be noted that there are very few commonalities among any of the pages in question. I figure as we fine tune the tool over several iterations, we can perhaps get to the point where we can identify specific portions of a page as belonging to a set of page level components like Panels, Field Groups, Containers, etc. If we can identify these elements in each page, we can eventually inject actual reusable components in place of the existing HTML.

I'm thinking this is probably the only valid approach that has a chance at converting the pages with some level of confidence that nothing is getting broken. We wouldn't be changing the HTML element structures at all, so initially, the output of the tool should appear exactly as it currently does, but with the structure in place that will enable us to start making changes to appearance across the entire application with single point edits to the correct classes, etc.

Towards this end, I have already located a tool that may do exactly what I am describing - it's from LinkedIn, and it is named CSS Blocks. They mention another tool that is used to optimize the CSS named OptiCSS (sounds like 'optics'), but apparently it is not quite ready for prime time.

Anyway, I hope some of this makes sense and can be useful. Feel free to email me with questions or comments.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文