Display an indicator in CSS to show when a line has wrapped

Can CSS be used to visually indicate when a line is wrapped? For instance, consider the following code inside a "pre" block:

<html><body>Hello World</body></html>

I have set white-space to pre-wrap to prevent horizontal scrolling issues. It displays like this:

<html><body>Hello World</body>
</html>

However, I would like to show some sort of indication that the line has wrapped. Something along these lines:

<html><body>Hello World</body>
| </html>

This way, I can inform the reader that the | symbol signifies a line wrap. This could be particularly helpful in situations where breaking mid-line is undesirable, such as:

form.getSubmit().style.background
Color = 'green';

Answer №1

Here's a creative idea to add style to your code snippets by using pseudoelements:

Check out the Codepen demo here


Markup

<pre>&lt;html>
   &lt;body>
       Hello World
   &lt;/body>
&lt;/html&gt;
</pre>

CSS

pre {
  background: yellowgreen;
  color: #fff;
  line-height: 1.5;
  font-size: 1.3rem;
  overflow: hidden;
  margin: 30px;
  border: 15px yellowgreen solid;
  position: relative;
  padding-left: 1.25em;
  text-indent: -1.25em;
}

pre:before {
  position: absolute;
  z-index: 1;
  line-height: inherit;
  font-size: inherit;
  color: #424242;
  content: "\A\2424\A\2424\A\2424..." /* Get creative with this */
}

You can customize the newline symbol used in the pre:before like the unicode U+2424 () SYMBOL FOR NEWLINE.

The approach involves creating multiple pairs of \A\2424 inside the content property for each line of code to be displayed.


See the final result:

https://i.sstatic.net/81YHL.png

Answer №2

When it comes to utilizing pure CSS, one option is to explore the ::first-line selector to apply different styling to your text (or other font-related properties). However, this method only affects the first line of content. In my suggestion, this would require creating a new <pre> block for each line of content, which may not be the most adaptable solution.

body {
  margin: 0;
}

pre {
  width: 250px;
  white-space: pre-wrap;
  background: aliceblue;

  /* Styling for lines after the first */
  color: red;
}

pre::first-line { /* Styling for the first line */
  color: green;
}
<pre>
&lt;html&gt;&lt;body&gt;Hello World&lt;/body&gt;&lt;/html&gt;
</pre>

Answer №3

This process is how I managed to achieve the desired outcome, thanks to the presence of an interpreter that serves as a bridge between the original text and HTML format. Initially, the interpreter inserted <pre> before and after the text. My modification involved placing <pre> at the beginning of each line and \n</pre> at the end of each line (where \n signifies a newline character for line breaks). Additionally, I created a 1 pixel gray image named gray.png and executed the following steps:

pre {
  white-space:pre-wrap;
  padding-left:1em;
  text-indent:-1em;
  background-image:url(gray.png);
  background-repeat:no-repeat;
  background-position:0px 1.5em;
  background-size:1em 100em;
}

As a result, when a normal line is displayed, the padding and text-indent negate each other's effects, positioning the text normally. However, when the text wraps around, it becomes indented due to the padding, while the background image appears on the left side. With a height of 100em, the background image spans multiple lines. To avoid repeating in both directions, I refrained from using repeat-y. Ultimately, I find the final visual appearance quite pleasing.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

similar element with alternate background shade

I have a custom component that I am using multiple times on a Vue page. I want to customize the background color for each instance of this component. Here is the code for my component: <template> <div class="project-card" :style="{backgroundCo ...

Generating HTML using a filter

I have been working on creating a filter that will render HTML tags. Here is the code for my filter: filters: { limitStrLength: function (value, maxLength) { if (value && value.length > maxLength) { let partialVal = value.substr(0, ...

When the menu active item is clicked, the header will automatically scroll to the top

I've implemented a sticky header using CSS and JavaScript along with a mega menu. However, I've encountered an issue where when scrolling to the bottom and clicking on the "more" link in the dropdown, the page scrolls back to the top. This is the ...

Alignment of slider arrows in a CSS carousel with Bootstrap 4.1.1

Is there a way to position the carousel control arrows at the left and right ends of the screen in Bootstrap 4.1.1? I attempted the code below but it didn't yield the desired result. .carousel-control .icon-prev{ left: 5px } .carousel-control .icon ...

Discover the concealed_elem annotations through the power of JavaScript

As I work on my new website, I am struggling with narrowing down the web code. I came across a solution that seems fitting for what I need, but unfortunately, I can't seem to make it work: I attempted the non-jQuery solution, however, I must be missi ...

Would it be advisable to store HTML data in a database?

Currently, I am in the process of developing a blog-app project using Angular 7. For the backend, I am utilizing firebase cloud functions and performing CRUD operations by storing HTML content in firebase with the help of Angular - CKEditor component. Whe ...

Searching for hidden elements within a div using a filter option

An accordion is located inside a div and a search box has been added to the div with the intention of serving as a search filter. Some accordion elements are visible within the div while others are hidden. The problem arises when trying to make the filter ...

Enhancing the Appearance of MUI Date Calendar

I'm in the process of creating a calendar similar to mui's <DateCalendar />, and I want to customize the header from 'S M T W T F S' to 'Sun Mon...' as well as adjust the position of the arrows. Before: https://i.sstat ...

Ensure that each row contains no more than 5 items, and use flexbox to automatically resize

I currently have the following setup: .container { background: gray; width: 600px; display: flex; flex-flow: row wrap; position: relative; } .item { background: blue; width: auto; height: auto; margin: 4px; flex: 1; flex-basis: 20% ...

Avoiding parent animations from affecting child elements using CSS

nav { display: inline-flex; align-items: center; width: 100%; height: 8rem; border-bottom: 1px solid black; } .nav-list { display: flex; width: 100%; } .nav-list li { line-height: 8rem; position: relative; } .sub-menu li { color: #c4 ...

HTML: Mark the chosen hyperlink or tag

In my HTML page, I am looking to keep the link selected when it is clicked on. Here is the initial HTML code: <table class="main-dev"> <tr> <td> <a class='titleForm' style="cursor:pointer"> ...

Achieving a scrollable div with ng-repeat

I have implemented ng-repeat to showcase some messages, and I am attempting to create a scrollable "message_area" as the messages overflow naturally over time. However, my current code is not delivering the desired outcome. <div class="main_area"> ...

Display function not functioning properly following AJAX request

I'm working on a functionality where I want to initially hide a table when the page loads, and then display it with the results when a form is submitted using Ajax. The issue I'm facing is that the code refreshes the page and sets the table back ...

The alignment of the labels on the left side of the div is out of place

After spending hours trying to troubleshoot, I still can't seem to align my hidden radio buttons properly within a label with a background image. Instead of lining up on the left side as intended, they are appearing closer to the middle of the div. Fo ...

Parent container is not as wide as the child element

When a parent <div> has no specified width and a child <div> has a fixed width, what CSS/HTML rules cause the parent <div> to only be as wide as the window rather than the child's fixed width? In the code snippet below, the parent & ...

Experiencing challenges with showcasing numerical values in the grid cells

My latest project involves creating an interactive sudoku solver. I've successfully created a grid made up of various elements to resemble an empty sudoku grid. However, my challenge lies in getting the numbers to display within the grid cells. Click ...

Can someone explain the significance of this in an HTML form?

Can anyone explain the following code snippet found in a web form? I found this button code on a webpage, but I'm having trouble understanding it. Could someone provide some clarity? <input type="submit" name="ctl00$ContentPlaceHolder1$Button8" v ...

Maintain the grouping of elements within a div when printing in Internet Explorer 8

When printing in IE8, I'm facing an issue with keeping the "Table title" line on the same page as the <table>. Despite using page-break-inside:avoid;, there is still a page break between the table title and the actual table. My expectation is f ...

Ways to conceal scrollbar when it is not in use

Typically, the scrollbar is visible only when scrolling through a page and then disappears immediately after. I attempted to modify the default scrollbar properties using basic CSS. While this made the scrollbar always visible, adding "overflow-y: auto" d ...

Is it possible to meta-refresh a page for redirection?

When creating a webpage, I included a META tag like this: <META http-equiv="refresh" content="5;URL=http://www.google.com"> The issue is that mobile browsers do not support this meta tag. It redirects properly on web browsers, but not on mobile dev ...