html - 如何使用 CSS 设置代码列表的样式?

标签 html css

我想使用 CSS 在 HTML 文档中显示编程语言代码片段以及 HTML 代码。我希望它缩进并使用固定宽度的字体...我在想类似的东西:

<blockquote style="some_style">
my code here
my code here also
</blockquote>

并让它以一种很好的格式显示(如果它是颜色编码的奖励,但不一定是。

如何使用 CSS 完成此操作?

最佳答案

分享我在网站中使用的示例,我在样式表中使用了以下 pre:

pre {
    background: #f4f4f4;
    border: 1px solid #ddd;
    border-left: 3px solid #f36d33;
    color: #666;
    page-break-inside: avoid;
    font-family: monospace;
    font-size: 15px;
    line-height: 1.6;
    margin-bottom: 1.6em;
    max-width: 100%;
    overflow: auto;
    padding: 1em 1.5em;
    display: block;
    word-wrap: break-word;
}

结果如下:

enter image description here

Disclaimer: In my leisure time, I have spend few hours to update this CSS with a bit extra features like code lines and code Copy button using CSS with JavaScript to my personal use that I like to share. Please use as you like github source code. To see a code example in real world, check this article from my blog that show how I use the code sample.

关于html - 如何使用 CSS 设置代码列表的样式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4000792/

相关文章:

asp.net - 为什么 CSS 类不能包含另一个 CSS 类

html - 无法使用 MVC 5 在 IE 11 中使用 FontAwesome

javascript - 当弹出窗口关闭时,我需要向红色背景 div 添加一个滚动条

javascript - 如何使用 jspdf 按每列设置单元格大小?

jquery - 对响应式网站使用前缀 id 标签而不是媒体查询是否不好?

javascript - 如何创建一个 OnClick 函数,当用户单击一个单词时将其更改为另一个单词?

css - 透明的 div 背景,带有淡淡的颜色?

html - Internet Explorer 10 入侵

javascript - 为什么 fontawesome 图标不显示?在 Chrome 和 Safari 上?

html - 尝试使用 CSS 将子元素垂直和水平居中