disabling text alignment as justified for specific sections of text

To avoid wrapping a specific part of text, you can use

<span style="white-space: nowrap;">
.

If you have justified text (text-align: justify), and want to prevent justification for certain words without affecting the rest, is there a way to do it?

For example, instead of having equal spacing between words like this:

Lorem ipsum dolor sit amet, consectetur |
adipiscing    elit.     Phasellus    et |
ullamcorperenim sed velit fermentum.    |

You want to keep "adipiscing elit" together only like this:

Lorem ipsum dolor sit amet, consectetur |
adipiscing elit.      Phasellus      et |
ullamcorperenim sed velit fermentum.    |

Is there a way to achieve this formatting?

Answer №1

Here are some different options to consider:

  1. Try using FOUR-PER-EM SPACE U+2005 instead of a normal space, for example adipiscing&#x2005;elit. Browsers may not always recognize it as non-stretchable space, but many do treat it this way. This method is considered natural and falls under the category of fixed-width spaces. However, note that it may not work in older versions of Internet Explorer if the text's primary font is not Arial Unicode MS or similar.
  2. Wrap the words within inline markup (often span) and apply text-justify: none and display: inline-block to it. Keep in mind that this technique might not be supported by certain older browsers and will force the two words together unless the latter declaration is applied (and without it, modern browsers may not recognize this approach).
  3. Consider using NO-BREAK SPACE instead of a regular space. While this method used to be effective, it now tends to combine the words on the same line. Some current browsers like Firefox unfortunately interpret no-break spaces as stretchable.
  4. Explore techniques similar to Web_Designer’s updated answer. It is recommended to use em values for width to correspond with the font size, with 0.25em being the typical width of a space character.

Answer №2

I personally advise against using the html style attribute. It's much easier to make changes if you use a class instead:

Enclose the words "adipiscing elit." in a span with a class like this:

<span class="unjustify">adipiscing elit.</span>

Then, in your CSS:

.unjustify {
    word-spacing: 2px; //try different values here.
}


Update:

After testing, the solution above doesn't appear to work.
...so I have devised the following (functional) solution:

Enclose the space between your two words in a span with a class like this:

adipiscing<span class="unjustify"> </span>elit.

And then in your CSS:

.unjustify {
    display: inline-block;
    width: 6px;
}

Answer №3

If you're looking for a solution, consider using one of the fixed-width Unicode space characters available. Check out:

or

http://en.wikipedia.org/wiki/Space_(punctuation)

For instance:

adipiscing&#x2004;elit.

Just like Web_Designer's solution, keep in mind that these spaces will leave a space at the end of the line if there is a line break.

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

What is the best way to align the navbar-brand to the right using Bootstrap 4?

I am working with Bootstrap 4 and I have a specific requirement for the navbar-brand item to be positioned on the far right side. By default, the menu appears on the left side. I have attempted to use classes like ml-auto, mx-auto, mr-auto, pull-right, bu ...

Choose various div elements and modify their background colors

I have a PHP loop where I am creating multiple divs dynamically. Here is the code snippet: <?php for ($i=0; $i<=9; $i++) { for ($j=0; $j<=9; $j++) { echo "<div class=\"bg\" id=\"aaa".$i."&bsol ...

Using JQuery to swap out background images in CSS

I have been working on a slider that is supposed to fade one picture over another. The issue is that the background change seems to only work in the Dreamweaver preview and not in the actual browser. loop = setInterval(function(){ $("#slider").css("ba ...

Triggering a click event on various instances of a similar element type using the onclick function

Hey there, I'm a newcomer to Javascript. I've been practicing my Javascript skills and trying to replicate something similar to what was achieved in this example: Change Button color onClick My goal is to have this functionality work for multip ...

tips on adjusting margins or paddings in option select

How can I use the margin or padding options for my select dropdown? I tried using &nbsp; <select class="select"> <option value="100"></option> <option value="250">$593</option> ...

A method for changing the background of a textbox based on a specific condition

I have an input text field below. <label>User Type</label> <input name="user_type" id="user_type" class="form-control" readonly/> I am trying to change the background color of this textbox based on the text ...

Tips for generating an ordered list that begins with 01

Currently, I am developing a Ruby on Rails application in which I am iterating over the tuto. .container .row .col-xs-12.col-sm-12 h1.text-gray Tutorials br .col-xs-10.col-sm-5.index-background - @tut ...

List-group item in Bootstrap 4 featuring an arrow on the right side

Currently, I am utilizing Bootstrap 4 and are in need of developing a list-group featuring a title as well as an arrow positioned on the right side. This is what I currently have: <div class="accordion" id="accordionExample"> & ...

Tips for inserting HTML code within session statements

When a user signs in, I want to display this content. However, when I try to include the code within the echo statement, it doesn't work. How can I achieve this? <?php if(isset($_SESSION['userID'])){ echo 'PLACE CODE HERE'; } ? ...

React not displaying images with relative paths

In the past, I used to import images in React like this: import person from '../images/image1.png' And then use them in my code like this: <img src={person} alt="" /> Now, for some reason, I want to directly specify the image pa ...

Is there a way to eliminate the gap under the image?

https://i.sstatic.net/d73jo.jpg Is there a way to eliminate the gap under the image? ...

Place a material-ui React component at the center of a footer section

I'm facing a challenge with centering a material-ui Simple Breadcrumbs component within a footer as opposed to having it aligned to the left. Even though I'm new to this, I thought it would be straightforward but I can't seem to figure it ou ...

The best way to centralize the input group in bootstrap

I attempted to center align my search bar, but it stubbornly stays to the left. Here's the code I'm using: <head> <style> html { overflow: auto; } html, body, iframe { ...

What is the best way to toggle the visibility of my menu using JavaScript?

I recently implemented a script to modify a CSS property in my nav bar as I scroll down, triggering the change after reaching 100px. $(window).scroll(function() { var scroll = $(window).scrollTop(); //console.log(scroll); if ...

The server mistakenly sent the resource as a Stylesheet even though it was interpreted differently

Dear Fellow Coders, Could anyone lend a hand? I encountered this issue on my website after uploading it to my FTP: "Resource interpreted as Stylesheet but transferred with MIME type text/plain" </head> <body> <div class= "navbar navbar ...

What is the best way to align the right column section below the left column section on a reduced screen size in a responsive design?

When the screen size is small, I want the "left-side" of my project to be displayed above the right side. The problem I'm facing is that as the screen shrinks, the left side starts getting hidden by the right side until it suddenly jumps to the right ...

Customizing individual characters within HTML text using the mouse's current location

Is there a way to manipulate the css properties of individual characters within a text string based on their proximity to the mouse cursor? If you're interested, check out this Codepen: https://codepen.io/NewbCake/pen/qYXvoo The concept involves tak ...

Tips for making a circle and a bar overlap using CSS

Trying to create a circular image and a horizontal bar of equal height next to it in a user profile. It needs to be responsive and look like the image below, with text in the black bar. Looking for help with CSS to achieve this: I currently have the cod ...

The asp.net masterpage is not utilizing the stylesheet

I have the code snippet below in the MasterPage.master file of my project: <%= ConfigurationManager.AppSettings("System").ToString() %> <asp:ContentPlaceHolder ID="Stylesheets" runat="server"> <link rel="Stylesheet" href="/App_Dat ...

Alignment of Multiple Backgrounds in CSS3 on a Vertical Axis

Utilizing CSS3 allows for the use of multiple background images. Suppose there is a DIV container with dimensions: div {width:100px; height:200px;}. If I have 4 background images sized at 100/50px, can they be aligned vertically to completely fill the DI ...