<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>~iany/ HTML</title><link>https://blog.iany.me/tags/html/</link><description>Recent content in HTML «~iany/»</description><language>en-US</language><managingEditor>me@iany.me (Ian Yang)</managingEditor><webMaster>me@iany.me (Ian Yang)</webMaster><copyright>CC-BY-SA 4.0</copyright><lastBuildDate>Sat, 04 Feb 2012 00:00:00 +0000</lastBuildDate><atom:link href="https://blog.iany.me/tags/html/index.xml" rel="self" type="application/rss+xml"/><item><title>CSS Line Wrap Indicator</title><link>https://blog.iany.me/2012/02/css-line-wrap-indicator/</link><pubDate>Sat, 04 Feb 2012 00:00:00 +0000</pubDate><author>me@iany.me (Ian Yang)</author><guid>https://blog.iany.me/2012/02/css-line-wrap-indicator/</guid><description>&lt;p&gt;Many editor can wrap a line that reaches the window width and show an indicator in the margin, for example, Emacs.&lt;/p&gt;
&lt;figure class="kg-image-card"&gt;
&lt;img alt="Emacs line wrap with bent arrow in fringe" class="kg-image" loading="lazy" src="https://blog.iany.me/2012/02/css-line-wrap-indicator/emacs_hu_21617bd44e47069e.png" srcset="https://blog.iany.me/2012/02/css-line-wrap-indicator/emacs_hu_5d8ba910cd98aab5.png 400w, https://blog.iany.me/2012/02/css-line-wrap-indicator/emacs_hu_21617bd44e47069e.png 476w" sizes="(max-width: 400px) 100vw, 476px" /&gt;
&lt;figcaption &gt;Emacs line wrap with bent arrow in fringe&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;I want to add such line wrap indicators to the code block in HTML. It is easy to add indicators as background image in CSS. But the indicators should only be shown when the line is wrapped. And from the figure, the wrap indicator is not displayed on the last line in the right margin, and not on the first line in the left margin. There&amp;rsquo;s a &lt;code&gt;:first-line&lt;/code&gt; pseudo element selector, there&amp;rsquo;s no &lt;code&gt;:last-line&lt;/code&gt;. However, it can be achieved by using &lt;code&gt;:before&lt;/code&gt; and &lt;code&gt;:after&lt;/code&gt; with position trick.&lt;/p&gt;
&lt;p&gt;You can check the result in this &lt;a href="https://jsfiddle.net/doitian/97bfwx6q/5/"&gt;jsfiddle&lt;/a&gt;. Resize the result panel to see the line wrap indicators. Following is a detailed explanation.&lt;/p&gt;
&lt;h2 id="add-line-markup"&gt;Add Line Markup&lt;/h2&gt;
&lt;p&gt;First, the code block in pre must be split by lines and add markup so that CSS can be applied.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;lt;pre&amp;gt;&amp;lt;code&amp;gt;(font-lock-add-keywords
'ruby-mode
'((&amp;quot;\\(\\b\\sw[_a-zA-Z0-9]*:\\)\\(?:\\s-\\|$\\)&amp;quot; (1 font-lock-constant-face))))
&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;is converted to&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;lt;pre&amp;gt;&amp;lt;code&amp;gt;&amp;lt;span class=&amp;quot;line&amp;quot;&amp;gt;(font-lock-add-keywords
&amp;lt;/span&amp;gt;&amp;lt;span class=&amp;quot;line&amp;quot;&amp;gt; 'ruby-mode
&amp;lt;/span&amp;gt;&amp;lt;span class=&amp;quot;line&amp;quot;&amp;gt; '((&amp;quot;\\(\\b\\sw[_a-zA-Z0-9]*:\\)\\(?:\\s-\\|$\\)&amp;quot; (1 font-lock-constant-face))))
&amp;lt;/span&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It is easy to do such conversion by replace new line character &lt;code&gt;\n&lt;/code&gt; to &lt;code&gt;\n&amp;lt;/span&amp;gt;&amp;lt;span class=&amp;quot;line&amp;quot;&amp;gt;&lt;/code&gt; and then wrap with first open tag and the last close tag.&lt;/p&gt;
&lt;p&gt;Because I want to show indicators in left, right padding of &lt;code&gt;span.line&lt;/code&gt;, it should be displayed as block to expand horizontally. Also enable line wrap on &lt;code&gt;pre&lt;/code&gt;.&lt;/p&gt;
&lt;pre&gt;&lt;code class="language-css"&gt;pre {
white-space: pre-wrap;
}
pre code, span.line {
display: block;
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Pay attention to the new line position. It is included in the added
&lt;code&gt;span.line&lt;/code&gt; elements. Otherwise extra empty line is added between lines.&lt;/p&gt;
&lt;h2 id="add-indicators"&gt;Add Indicators&lt;/h2&gt;
&lt;p&gt;The indicators are added through &lt;code&gt;:before&lt;/code&gt; and &lt;code&gt;:after&lt;/code&gt;. They have the same height with the line by setting &lt;code&gt;height&lt;/code&gt; to &lt;code&gt;100%&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;To ensure the indicators are aligned to each wrapped line, the picture must be the same height with &lt;code&gt;line-height&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;I use following two icons. The height is 28px. So also set &lt;code&gt;span.line&lt;/code&gt;
&lt;code&gt;line-height&lt;/code&gt; to 28px.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Left: &lt;picture class="img-inline-wrapper"&gt;
&lt;img alt="left line wrap indicator" class="img-inline" loading="lazy" src="https://blog.iany.me/2012/02/css-line-wrap-indicator/line-left_hu_7ea5f85ee4a4c98a.png" title="" /&gt;
&lt;/picture&gt;
&lt;/li&gt;
&lt;li&gt;Right: &lt;picture class="img-inline-wrapper"&gt;
&lt;img alt="right line wrap indicator" class="img-inline" loading="lazy" src="https://blog.iany.me/2012/02/css-line-wrap-indicator/line-right_hu_ff318c8c4822bedd.png" title="" /&gt;
&lt;/picture&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;pre&gt;&lt;code class="language-css"&gt;span.line {
line-height: 28px;
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then just add &lt;code&gt;:before&lt;/code&gt; and &lt;code&gt;:after&lt;/code&gt; and apply vertically repeat background on them. &lt;code&gt;position&lt;/code&gt; of &lt;code&gt;span.line&lt;/code&gt; is set to &lt;code&gt;relative&lt;/code&gt; so &lt;code&gt;:before&lt;/code&gt; and &lt;code&gt;:after&lt;/code&gt; can be positioned relative to it. It is easy to add them in &lt;code&gt;span.line&lt;/code&gt; padding. I use padding instead of margin because later I&amp;rsquo;ll set overflow to hidden to hide indicators outside of &lt;code&gt;span.line&lt;/code&gt;, where margin is considered outside.&lt;/p&gt;
&lt;pre&gt;&lt;code class="language-css"&gt;span.line {
padding: 0 13px; /* 8px for indicator, 5px for space around text */
position: relative;
}
span.line:before, span.line:after {
background-repeat: repeat-y;
content: &amp;quot;&amp;quot;;
display: block;
width: 10px;
height: 100%;
position: absolute;
}
span.line:before {
background-image: url(&amp;quot;line-left.png&amp;quot;);
top: 0;
left: 1px;
}
span.line:after {
background-image: url(&amp;quot;line-right.png&amp;quot;)
top: 0;
right: -1px;
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now the indicators are shown in left and right padding on all lines. Use &lt;code&gt;top&lt;/code&gt; to shift down left indicators and &lt;code&gt;bottom&lt;/code&gt; to shift up right indicators, so left indicator is not shown on the first line and right indicator is not shown on the last line. But some indicators are moved out, use &lt;code&gt;overflow:hidden&lt;/code&gt; to hide them.&lt;/p&gt;
&lt;pre&gt;&lt;code class="language-css"&gt;span.line {
overflow: hidden;
}
span.line:before {
top: 28px;
}
span.line:after {
top: auto;
bottom: 28px;
}
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id="add-color-to-padding"&gt;Add Color to Padding&lt;/h2&gt;
&lt;p&gt;Use border and background on &lt;code&gt;pre&lt;/code&gt; to add different color to indicators padding and code block. Use negative margin to align indicators to the
borders.&lt;/p&gt;
&lt;pre&gt;&lt;code class="language-css"&gt;pre {
border-style: solid;
border-color: #EEE;
border-width: 0 8px;
background-color: #AAA;
}
span.line {
margin: 0 -8px;
}
&lt;/code&gt;&lt;/pre&gt;</description><category domain="https://blog.iany.me/post/">Posts</category><category domain="https://blog.iany.me/tags/css/">CSS</category><category domain="https://blog.iany.me/tags/html/">HTML</category></item></channel></rss>