如何将 CKEditor 的 CSS 应用于输出

3
Ck-editor在编辑时表现良好,但是当我将编辑后的文本保存到数据库中并加载到页面上时,生成的HTML格式不规范。是否需要应用其他CKeditor js功能到目标区域,或者需要向文本容器添加默认类?我已经检查了CK-editor css文件,但没有特定的类,就像当您检查“contents.css”中的CKeditor文件时,“img.left{border: 1px solid #ccc; ..”那很令人毛骨悚然,因为没有特定的类,它会在纯iframe中工作,但如果我在更复杂的页面中显示来自CKeditor的文本,则必须像“.wysiwyg img.left”一样重写CSS,然后通过修改.wysiwyg类的reset.css来重置所有CSS,而且重置所有内容非常困难。CK-editor文档中是否有我错过的其他方法?因为我在那里看到的都是实际编辑器中的示例,而不是如何为生成的文本自定义样式。
1个回答

3
如果您只想让在CKEditor中编写的HTML在页面内看起来相同,首先必须将其插入到一个具有自定义类(例如“my-container”)的div元素内。
然后,您必须在页面中包含contents.css。这里有两个选择:1)使用Scoped Stylesheets或2)修改contents.css,并为每个规则设置作用域。 1. 使用Scoped Stylesheets 在这种情况下,您应该使用Scoped Stylesheets和JQuery Scoped CSS plugin(由于当前浏览器支持的缺乏)。
您的HTML代码将如下所示:
<div class="my-container">
    <style scoped>
        @import "ckeditor/contents.css";
    </style>
    <!-- Your HTML goes here -->
</div>

2. 在contents.css中为每个规则设置作用域

在这种情况下,您必须链接到经过修改的CKEditor contents.css文件。每个规则的选择器必须作用于“my-container”类,以便不影响页面的其余部分。以下是示例 contents.css 文件:

.my-container
{
    /* Font */
    font-family: sans-serif, Arial, Verdana, "Trebuchet MS";
    font-size: 12px;

    /* Text color */
    color: #333;

    /* Remove the background color to make it transparent */
    background-color: #fff;

    margin: 20px;
}

.my-container .cke_editable
{
    font-size: 13px;
    line-height: 1.6em;
}

.my-container blockquote
{
    font-style: italic;
    font-family: Georgia, Times, "Times New Roman", serif;
    padding: 2px 0;
    border-style: solid;
    border-color: #ccc;
    border-width: 0;
}

.my-container .cke_contents_ltr blockquote
{
    padding-left: 20px;
    padding-right: 8px;
    border-left-width: 5px;
}

.my-container .cke_contents_rtl blockquote
{
    padding-left: 8px;
    padding-right: 20px;
    border-right-width: 5px;
}

.my-container a
{
    color: #0782C1;
}

.my-container ol,.my-container ul,.my-container dl
{
    /* IE7: reset rtl list margin. (#7334) */
    *margin-right: 0px;
    /* preserved spaces for list items with text direction other than the list.    (#6249,#8049)*/
    padding: 0 40px;
}

.my-container h1,.my-container h2,.my-container h3,.my-container h4,.my-container h5,.my-container h6
{
    font-weight: normal;
    line-height: 1.2em;
}

.my-container hr
{
    border: 0px;
    border-top: 1px solid #ccc;
}

.my-container img.right
{
    border: 1px solid #ccc;
    float: right;
    margin-left: 15px;
    padding: 5px;
}

.my-container img.left
{
    border: 1px solid #ccc;
    float: left;
    margin-right: 15px;
    padding: 5px;
}

.my-container pre
{
    white-space: pre-wrap; /* CSS 2.1 */
    word-wrap: break-word; /* IE7 */
}

.my-container .marker
{
    background-color: Yellow;
}

.my-container span[lang]
{
   font-style: italic;
}

.my-container figure
{
    text-align: center;
    border: solid 1px #ccc;
    border-radius: 2px;
    background: rgba(0,0,0,0.05);
    padding: 10px;
    margin: 10px 20px;
    display: block; /* For IE8 */
}

.my-container figure figcaption
{
    text-align: center;
    display: block; /* For IE8 */
}

我已经在config.js中添加了config.bodyClass = 'my-container';,并将类my-container添加到文档容器中,但仍然没有运气,输出仍然是相同的丑陋结果。 - Youans
你是否从你的页面中链接到自定义的CSS文件?一个jsfiddle会非常有用。 - ncardeli
不用,我只需要按原样查看文档,我不想要任何自定义 CSS。 - Youans
但是CKEditor加载contents.css文件(您可以在其主目录中找到该文件),因此您需要加载相同的文件。您还可以修改它(或者更好地将其复制并保存在CKEditor目录之外,然后进行编辑),以便其中的所有选择器都以相同的类开始(例如my-container)。 - Reinmar
+1 对于作用域样式表来说是个不错的选择。@Jiro 你也应该给他的答案点赞,因为你已经接受了它(除非你有不这样做的理由,我猜你没有)。 - Andrea Ligios
经过这么长时间,这仍然是实际的,但必须删除“.cke_contents_ltr”,因为它是 ckeditor 的包装类之一,在您的页面上可能会大多缺失。 - Jiro Matchonson

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接