简体   繁体   English

如何使用css应用换行/继续样式和代码格式

[英]How to apply a line wrap/continuation style and code formatting with css

When presenting preformatted text on the web (eg code samples), line wrapping can be a problem. 当在网络上呈现预先格式化的文本时(例如代码示例),换行可能是个问题。 You want to wrap for readability without scrolling, but also need it to be unambiguous to the user that it is all one line with no line break. 您希望在不滚动的情况下进行可读性包装,但也需要对用户明确说明它是一行而没有换行符。

For example, you may have a really long command line to display, like this: 例如,您可能需要显示一个非常长的命令行,如下所示:

c:\Program Files\My Application\Module\bin\..> Some_really_long_command line "with parameters" "that just go on and on" " that should all be typed on one line" "but need to be wrapped for display and I'd like the text style to indicate that it has wrapped"

(Stackoverflow forces a line like this not to wrap.) (Stackoverflow强制这样的一行不要换行。)

Is there a way of styling with CSS to give the same treatment as you see in books? 有没有一种使用CSS设计样式的方法来提供与书中看到的相同的处理方式? ie to wrap the line, but include an image or glyph that indicates a line continuation. 即包裹该行,但包括指示行继续的图像或字形。

Obviously I am looking for a style that can be applied to all text, and let the browser's XHTML/CSS rendering engine figure out which lines have wrapped and therefore need the special treatment. 显然我正在寻找一种可以应用于所有文本的样式,并让浏览器的XHTML / CSS渲染引擎找出哪些行已经包装,因此需要特殊处理。

The Solution so far.. 解决方案到目前为止..

Adding line continuation glyphs 添加行继续标志符号

Thanks to Jack Ryan and Maarten Sander, have a reasonably workable solution to add continuation glyphs to either the left or right of wrapped lines. 感谢Jack Ryan和Maarten Sander,有一个合理可行的解决方案,可以在包裹线的左侧或右侧添加连续字形。 It requires an image with repeating glyphs in the vertical, which is offset so that it is invisible if only one unwrapped line. 它需要在垂直方向上具有重复字形的图像,该图像是偏移的,因此如果只有一条未包装的线条则不可见。 The main requirement of this technique is that every line needs to be within a block (eg p, span or div). 该技术的主要要求是每条线都需要在一个块内(例如p,span或div)。 This means it cannot easily be used manually with existing text that is just sitting in a pre block. 这意味着它不能轻易地手动使用刚刚坐在前块中的现有文本。

The fragment below summarises the essential technique. 下面的片段总结了基本技术。 I posted a live example here . 我在这里发布了一个实例。

.wrap-cont-l {
  margin-left: 24px;
  margin-bottom: 14px;
  width: 400px;
}

.wrap-cont-l p {
  font-family: Courier New, Monospace;
  font-size: 12px;
  line-height: 14px;
  background: url(wrap-cont-l.png) no-repeat 0 14px; /* move the background down so it starts on line 2 */
  text-indent: -21px;
  padding-left: 14px;
  margin: 0 0 2px 7px;
}

.wrap-cont-r {
  margin-left: 24px;
  margin-bottom: 14px;
  width: 400px;
}

.wrap-cont-r p {
  font-family: Courier New, Monospace;
  font-size: 12px;
  line-height: 14px;
  background: url(wrap-cont-r.png) no-repeat right 14px; /* move the background down so it starts on line 2 */
  text-indent: -28px;
  margin: 0 0 2px 28px;
  padding-right: 14px;
}

To be used like this: 要像这样使用:

<div class="wrap-cont-l">
  <p>take a long line</p>
  <p>take a long line</p>
</div>
<div class="wrap-cont-r">
  <p>take a long line</p>
  <p>reel him in</p>
</div>

But wait, there's more! 但等等,还有更多!

I recently discovered syntaxhighlighter by Alex Gorbatchev. 我最近发现了Alex Gorbatchev的syntaxhighlighter It is a fantastic tool for dynamically and automatically formatting text blocks. 它是动态和自动格式化文本块的绝佳工具。 It is principally intended for syntax highlighting code, but could be used for any text. 它主要用于语法高亮代码,但可用于任何文本。 In v1.5.1 however, it does not wrap lines (in fact it forces them not to wrap). 但是在v1.5.1中,它不会换行(实际上它会强制它们不换行)。

I did a little hacking around though, and was able to add a simple line wrap option syntaxhighlighter and also incorporate the continuation glyph idea. 虽然我做了一些黑客攻击,并且能够添加一个简单的换行选项syntaxhighlighter并且还包含了延续字形构思。

I've added this to the live examples and included a few notes on the hacks required (they are trivial). 我已将此添加到实际示例中,并包含了一些关于所需黑客的注释(它们是微不足道的)。 So with this as the text in the page: 因此,将此作为页面中的文本:

<textarea name="code" class="java:wraplines" cols="60" rows="10">
public class HelloWorld {

  public static void main (String[] args)

  {

    System.out.println("Hello World! But that's not all I have to say. This line is going to go on for a very long time and I'd like to see it wrapped in the display. Note that the line styling clearly indicates a continuation.");

  }

}
</textarea>

This is a snapshot of the formatted result: 这是格式化结果的快照:

screenshot http://tardate.com/syntaxhighlighter/line-continuation-example.jpg 截图http://tardate.com/syntaxhighlighter/line-continuation-example.jpg

Here is one (unpleasant) way of doing this. 这是一种(令人不快的)这样做的方式。 It requires a number of bad practices. 它需要一些不良做法。 But SO is about solutions to real problems so here we go... 但是SO是关于解决实际问题的方法,所以我们走了...

First each line needs to be wrapped in some sort of containing block. 首先,每一行都需要包含在某种包含块中。 Span or p are probably the most appropriate. Span或p可能是最合适的。

Then the style of the containing block needs to have line height set. 然后包含块的样式需要设置行高。 and a background image that contains a number of newLine glyphs at the start of every line except the first one. 和一个背景图像,在第一行之外的每一行的开头都包含许多newLine字形。 As this is code it would be resonable to expect it to not wrap more than 5 times. 由于这是代码,因此期望它不会超过5次是合理的。 So repeating 5 times is probably enoygh. 所以重复5次可能是enoygh。

This can then be set as the background image, and should display at the beginning of every line except the first one. 然后可以将其设置为背景图像,并且应该显示在除第一行之外的每一行的开头。 I guess the resulting CSS might look like this: 我想最终的CSS可能看起来像这样:

p.codeLine
{
    font-size: 12px;
    line-height: 12px;
    font-family: Monospace;
    background: transparent url(lineGlyph) no-repeat 0 12px; /* move the background down so it starts on line 2 */
    padding-left: 6px; /* move the text over so we can see the newline glyph*/
}

I've found a solution very similar to Jack Ryan's, but with the 'continuation' character at the end of the line. 我找到了一个与Jack Ryan非常类似的解决方案,但在行尾有“延续”字符。 It also indents the continued lines. 它还缩进了续行。

The CSS: CSS:


p {
  font-family: Arial, Sans-Serif;
  font-size: 13px;
  line-height: 16px;
  margin: 0 0 16px 0;
}

.wrap-cont {
  font-family: Courier New, Monospace;
  margin-bottom: 16px;
  width: 400px;
}

.wrap-cont p {
  background: url(wrap-cont.gif) no-repeat bottom right;
  text-indent: -32px;
  margin: 0 0 0 32px;
  padding-right: 16px;
}

The HTML: HTML:


<p>For example, you may have a really long command line to display, like this:</p>
<div class="wrap-cont">
  <p>c:\Program Files\My Application\Module\bin\..&gt; Some_really_long_command line "with parameters" "that just go on and on" " that should all be typed on one line" "but need to be wrapped for display and I'd like the text style to indicate that it has wrapped"</p>
  <p>c:\Program Files\My Application\Module\bin\..&gt; Some_really_long_command line "with parameters" "that just go on and on" " that should all be typed on one line" "but need to be wrapped for display and I'd like the text style to indicate that it has wrapped"</p>
</div>
<p>Stackoverflow forces a line like this not to wrap.</p>

This cannot be done with CSS. 这不能用CSS完成。 Sorry. 抱歉。 :( :(

If you want it to be unambiguous, you'll have to add markup. 如果你想要它是明确的,你将不得不添加标记。 I'd suggest using an <ol> with one list item per line of code, because that way you get line numbering for free. 我建议每行代码使用一个<ol>一个列表项,因为这样你可以免费获得行号。 If this is too much work to do across the site, you could always add it using Javascript. 如果在整个网站上做太多工作,您可以随时使用Javascript添加它。

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

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