简体   繁体   English

CSS 和 HTML - CSS 文件能否生成 Z4C4AD5FCA2E7A3F74DBB1CED00381AAZ?

[英]CSS and HTML - Can CSS files generate HTML?

I am making a website that is styled using CSS, in two DIVs.我正在制作一个使用 CSS 在两个 DIV 中设置样式的网站。 One 'Header' DIV which is always the same, and a 'Content' DIV that changes.一个始终相同的“标题”DIV 和一个变化的“内容”DIV。

In my CSS file, is it possible to write the HTML with all the links that stay in the header, so I just need to call (or similar) on every page, instead of having to write out my header content every time? In my CSS file, is it possible to write the HTML with all the links that stay in the header, so I just need to call (or similar) on every page, instead of having to write out my header content every time? Would also help in editing only one source, as I often leave out pages by mistake.也有助于只编辑一个来源,因为我经常错误地遗漏页面。

I don't want to use frames, so looking for an alternative.我不想使用框架,所以寻找替代方案。

Thanks, Brett谢谢,布雷特

Depending on the server and server side languages supported, this can be done.根据支持的服务器和服务器端语言,可以这样做。

Some servers will let you use Server Side Includes , for example.例如,某些服务器将允许您使用Server Side Includes With others you could specify "block" of HTML for a header (for instance) that will be part of a site wide template.对于其他人,您可以为将成为站点范围模板的一部分的 header(例如)指定 HTML 的“块”。

What is not possible it to achieve this with purely CSS and HTML.纯粹用 CSS 和 HTML 是不可能做到这一点的。

This is why you usually have some server side code running, which will insert the common header eg php include, SSI or other templating framework.这就是为什么您通常会运行一些服务器端代码,这些代码将插入常见的 header 例如 php 包括、SSI 或其他模板框架。 If that's not an option, you could write JavaScript which writes out your header each time to a specific DIV.如果这不是一个选项,您可以编写 JavaScript ,它将您的 header 每次写入特定的 DIV。 Although I don't think that's great from an SEO perspective.尽管从 SEO 的角度来看,我认为这不是很好。

The usual options for doing this client-side are an iframe, or some javascript that does DOM to add content (perhaps loaded from an external file).执行此客户端的常用选项是 iframe 或一些 javascript,它们执行 DOM 以添加内容(可能从外部文件加载)。 Or some javascript that creates an iframe.或者一些创建 iframe 的 javascript。 Or an iframe that creates some javascript.或创建一些 javascript 的 iframe。 Some permutation of those odious techniques.这些可恶技术的一些排列。

CSS does have the content property , but i think it's limited to plain text. CSS 确实有content 属性,但我认为它仅限于纯文本。 I don't know if you can use it to pull in HTML, either using a string or URI.我不知道您是否可以使用它来拉入 HTML,无论是使用字符串还是 URI。

As others have mentioned, the most common approach is to do it server-side.正如其他人所提到的,最常见的方法是在服务器端进行。 You can do this bottom-up with includes, or top-down with things like Tiles and SiteMesh.您可以使用包含自下而上执行此操作,也可以使用 Tiles 和 SiteMesh 等自上而下执行此操作。

CSS can't help you to do this. CSS 无法帮助您做到这一点。

You can make ajax loading of content.您可以使 ajax 加载内容。 You can make one index.html and lot of 'content' files (about.html, contacts.html, etc).您可以制作一个 index.html 和许多“内容”文件(about.html、contacts.html 等)。 And in index.html you can load another.html in content-div (for example with jQuery method.load())在 index.html 中,您可以在 content-div 中加载另一个.html(例如使用 jQuery method.load())

Another way - you can make little templating engine in php (or another server-side language)另一种方式 - 您可以在 php(或其他服务器端语言)中制作小模板引擎

It isn't possible to achieve that using CSS, because CSS can't handle any events, it's simply a "refrence" for the browser to know how to style your web-page, it's done, however, using AJAX.使用 CSS 是不可能实现的,因为 CSS 无法处理任何事件,这只是浏览器知道如何设置网页样式的“参考”,但是使用 ZA394A665E75AB778 完成了。 I suggest you to take a look on jQuery lib, it'll speed up the process tremendously, however - the main disadvantage is that the search crawlers won't be able to index your page correctly, so it'll be bad by an SEO perspective.我建议你看看 jQuery lib,它会大大加快这个过程,但是 - 主要缺点是搜索爬虫无法正确索引你的页面,所以它会被 SEO看法。

It's possible, if you have the time, to make an index-able version and an AJAX one, that's what we did for a mobile project here.如果您有时间,可以制作可索引版本和 AJAX 版本,这就是我们在这里为移动项目所做的。

jQuery AJAX API jQuery AJAX API

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

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