Skip to main content
Home  › ... Learn

Prevent Orphan Titles & Text in 2scx ;NBSP

Prevent Orphan Titles & Text in 2scx ;NBSP

VIEW IN DESKTOP MODE Mobile or full screen desktop: As you see above, one is orphanless.

WAIT! Look Above:The Title of this Post has an Orphan

Gorillafied reader can you fix this:  ...  a little help below.

The Concept - using ;nbsp

to utilize the HTML non-breaking space "nbsp" in place of the space between the last two words. This forces them to join and break together in a new line, thus preventing any orphaned words.

The Solution:

You would typically separate out the cs function into a helper file and then use a createinstance to call the class into your razor template OR directly into it to PreventOrphans.

Note: createInstance used to be the way to include Helpers but I do believe there is a new method 

To Do: Lookup documentation new method, see the razor view of THIS FILE for example of how this is implemented.

Example Implementation

var textOrphanFree = PreventOrphans(post.String("Tiitle"));

@post.String("Title") // example without oprhan fiix 
@Html.Raw(textOrphanFree) // example with fix

@******************* START: EXAMPLE PreventOrphans Helper Class ****************** *@


@using ToSic.Sxc.Web
    public string PreventOrphans(string input)
        if (string.IsNullOrWhiteSpace(input)) return input;

        var words = input.Trim().Split(' ');
        if (words.Length < 2) return input;

        // Rebuild the string without using index operators
        var lastSpaceIndex = input.LastIndexOf(' ');
        if (lastSpaceIndex > 0) // Check if there's a space to replace
            return input.Substring(0, lastSpaceIndex) + " " + input.Substring(lastSpaceIndex + 1);

        return input;

    // Example usage
    var text = "This is an example text that might wrap to a new line.";
    var textWithNoOrphans = PreventOrphans(text);