Break up in the middle of a sentence: Material UI

Currently facing an issue where my sentences break in the middle. Working with Material UI requires manual styling of different components like MUI. The problem I'm encountering can be seen below. I tried removing the display: flex, but then the square stops floating to the right, which is not what I want. How can I resolve the text breaking issue while keeping the square aligned to the right?

.box {
  display: flex;
  word-break: break-word;
  align-items: center;
  border: 4px dotted blue;
  width: 25em;
}

.highlighted {
  font-weight: bold;
}

.square{
  width:10px;
  height:10px;
  background-color:red;
}
<div class="box">
  This sentence should contain a 
  <span class="highlighted">   highlighted</span>
  piece of text.
  <div class="square"></div>
</div>

UPDATE: Here's the Desired Result

.container2{
   border: 4px dotted blue;
   width: 20em;
}
    
.square2{
    width:10px;
    height:10px;
    background-color:red;
    float: right;
}
<div class='container2'>
    This sentence should display a <b>styled </b>text. <div class='square2'></div>
</div>

Answer №1

The utilization of Flex is resulting in the text being divided into multiple blocks, as it interprets the text on either side of .special and the element itself as distinct children.

By enveloping the sentence in a <span>, flexbox recognizes it as a unified element.

.container {
  display: flex;
  word-break: break-word;
  align-items: center;
  border: 4px dotted blue;
  width: 25em;
}

.special {
  font-weight: bold;
}

.square{
  width:10px;
  height:10px;
  background-color:red;
}
<div class="container">
  <span>This sentence is supposed to display a <span class="special">special</span> styled piece of text.</span>
  <div class="square"></div>
</div>

Answer №2

Give this a try: is this the solution you seek?

.container {
  display: flex;
  word-break: break-word;
  align-items: center;
  border: 4px dotted blue;
  width: 25em;
}

.container .text {
  display: inline-block;
  margin: 0
}

.special {
  font-weight: bold;
}

.square{
  width:10px;
  height:10px;
  background-color:red;
}
<div class='container'>
  <p class='text'>
    This sentence is supposed to display a 
    <span class='special'>   special</span>
    styled piece of text.
  </p>
  <div class='square'></div>
</div>

Answer №3

One way to enhance your text presentation is by enclosing it in an additional div element, while maintaining the flex properties on the container.

.container {
  display: flex;
  word-break: break-word;
  align-items: center;
  border: 4px dotted blue;
  width: 25em;
}

.special {
  font-weight: bold;
}

.square{
  width:10px;
  height:10px;
  background-color:red;
}
<div class="container">
    <div>
      This sentence is supposed to display a <span class="special">special</span> styled piece of text.
    </div>
  <div class="square"></div>
</div>

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

Having trouble removing a DOM element with jQuery?

Hey there, I need your help with a jQuery issue I'm facing. I am currently working on cloning a div element that contains a span with a click event attached to it. The purpose of the click event is to remove the newly cloned section from the DOM. Whil ...

Utilizing d3 Charts in Angular 4 Framework

I need assistance with integrating a bar chart in an Angular 4 project, which includes HTML and TypeScript files as components. Can someone please provide guidance on this? The bar chart should show the increase in the number of employees each month, star ...

CSS Flexibility

Greetings everyone, it's been a while since I dabbled in web development. I recently worked on my site with the intention of making it responsive using flexbox. This is my first time posting here, so please guide me on how to seek help more effective ...

JavaScript: Modifying the Style of a :lang Selector

Currently, I am working on creating a basic multilingual static website using only HTML5, CSS3, and vanilla JavaScript. The structure of the HTML looks something like this: <html> <head> <title>WEBSITE</title> ...

The data type 'Event' cannot be assigned to the data type 'string' in this context

Recently diving into Angular, I came across a stumbling block while working through the hero tutorial. The error message that popped up was: Type 'Event' is not assignable to type 'string' You can see the error replicated here. ...

Unable to reinitialize MUI DatePicker after keydown event

Encountering an unusual behavior that defies explanation has left me puzzled. You can explore the code sandbox here. I am attempting to enable resetting a readOnly field by pressing DEL or BACKSPACE, but it seems to be ineffective. Oddly enough, I can suc ...

Align columns to the right and left using Bootstrap

It should be simple enough, but I can't seem to find my mistake. Goal: The entire markup should be centered with a width of col-10. Within the same 10-column width, there should be two divs each taking up 50% of the space. Using Bootstrap 5, for so ...

What is the best way to position the footer at the bottom of the page without obstructing any content?

I am currently working on a project that utilizes Bootstrap, and I am facing an issue with positioning the footer. Ideally, I want the footer to stay fixed at the bottom of the screen when there is not enough content to fill the page. However, when the con ...

Gliding over the problem with the Azure line

https://i.sstatic.net/7N3hO.jpg Is there a way to hide the opacity and font on hover? I have tried using outline: 0; in my CSS but there is still a blue line lingering. Any ideas on how to remove it? If you need to see the code and the issue I'm des ...

Adding the output of a UNIX command to a div container

I am currently working on creating a UNIX web-based terminal for educational purposes. My progress so far includes setting up a text box and displaying the output, similar to this: <?php $output = shell_exec('ls'); echo "<pre>$output< ...

Executing a function defined in a .ts file within HTML through a <script> tag

I am attempting to invoke a doThis() function from my HTML after it has been dynamically generated using a <script>. Since the script is loaded from an external URL, I need to include it using a variable in my .ts file. The script executes successfu ...

Automate the process of launching the <select> picker through programming

In an Android WebView, I need a way to programmatically trigger the opening of the select element picker provided below. <select id="select1" multiple="multiple"> <option value="MDO1" selected="selected">MDO 1</option> <option val ...

JavaScript encoding and decoding challenges

Can anyone help me figure out what's wrong? I'm trying to encode and decode a simple input, but it just doesn't seem to work! Any ideas why? Thanks in advance for your assistance :) ENCODE: function encryption_encode(s, delta) { var te ...

Angular js is a powerful framework that allows for the seamless transition of views, except for the home

I am currently using the ng-animate module to implement sliding effects for my app views. Each route has its own unique slide animation. Take a look at my simple code below: HTML: <div ng-view ng-animate class="slide"></div> CSS: /*Animatio ...

Strip inline styles from HTML content in django-tinymce

I am experiencing an issue with django-tinymce where it automatically adds inline styles when I save content. I would prefer to remove this behavior and only save the content in plain HTML. Can someone assist me with resolving this problem? ...

What is the best way to line up three divs horizontally and center the contents inside each one?

I'm brand new to CSS and need some help simplifying things. I am trying to create 3 divs that are all the same size, placed next to each other, with centered content in each one. Currently, I have a central div with a rotating image, and on the left a ...

Issue with background in list items

I am struggling with aligning the background of my vertical menu to the right and center from the top. Here is the code I have tried: ul.nav { margin:0; background-position:center; background-image: url(../images/nav_bg.gif);font-family: "Century Go ...

What is the best way to conceal a modal using jquery?

Click button to open modal: <button type="button" class="btn" onclick="CountClass()" runat="server" data-toggle="modal" data-target="#inlineForm1">OPEN</button> The modal code: <div class="modal fade text-left" id="inlineForm1" tabindex=" ...

Transform CSS filters into SVG filters

Implementing CSS filters on the <img /> elements has been successful for me. For example: filter: brightness(1) contrast(67%) saturate(250%) grayscale(0%) invert(0%) hue-rotate(330deg) sepia(40%) blur(0px); Now, I am trying to apply the same filte ...

Launch a new window with the window.open() method and execute a function on the newly opened window

I am having trouble generating a barcode in a new window using the barcode generator script. Despite trying to use window.focus(), I can't get the barcode to appear in the new window. Any assistance on how to generate the barcode in a separate window ...