如果您只想让在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-family: sans-serif, Arial, Verdana, "Trebuchet MS";
font-size: 12px;
color: #333;
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
{
*margin-right: 0px;
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;
word-wrap: break-word;
}
.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;
}
.my-container figure figcaption
{
text-align: center;
display: block;
}
config.bodyClass = 'my-container';
,并将类my-container
添加到文档容器中,但仍然没有运气,输出仍然是相同的丑陋结果。 - Youanscontents.css
文件(您可以在其主目录中找到该文件),因此您需要加载相同的文件。您还可以修改它(或者更好地将其复制并保存在CKEditor目录之外,然后进行编辑),以便其中的所有选择器都以相同的类开始(例如my-container
)。 - Reinmar