What steps should I take to ensure that my text aligns seamlessly within the container?

I've recently encountered an issue while working on my website. I have a container where I place my page content, but the words were breaking at the end of the container. To fix this, I used white-space: pre; which stopped the words from breaking, but now the text is not fitting 100% of the container.

You can view the website here: . The text fits perfectly within its container, but the words break at the end of each line. How can I make the text fill the whole container without the words breaking at the end?

HTML:

<div class="mainbox">
<img class="grow-shadow increase leftimage" src="assets/img/bubbles/firstb.png" align="left">

<p class="maintext textchange"><strong>GLOBETROT WITH ARABIC</strong> is your online language classroom that gives you the opportunity to learn the Arabic language through Skype and/or Zoom wherever you are and anytime you wish. Whether you are an adventurer trotting around the globe and exploring its wonders, an entrepreneur seeking flexible learning hours, a fulltime mom/dad, or simply someone who aspires to evolve in the comfort of their home while sipping on warm coffee, this is your chance to finally take on the opportunity of learning the Arabic language with a lot of fun and flexibility and most importantly at your own pace and comfort. You do not need to commit to physical place and space or to a particular time to learn the Arabic language and connect with its culture and people. With Globetrot with Arabic, you can make the most of the increasing space-time compression and cross social and cultural borders. All you need is a phone or a computer, a set of earphones, good Wi-Fi and the enthusiasm to learn the Arabic language and the beautiful culture that comes with it.</p>
</div>

CSS:

.mainbox {
    width: 100%;
    margin-top: -133px;
}

.leftimage {
    width: 350px;
    height: auto;
    position: relative;
    top: 98px;
    margin-right: 152px;
    margin-bottom: 59px !important;
    left: 8.8em;
}

.increase {
    display: block;
    margin-left: auto;
    margin-right: auto;
    cursor: pointer;
}

.textchange {
    margin-bottom: -11em;
}

.maintext {
    word-break: break-all;
    font-size: 41px;
    padding: 186px;
}

Any assistance would be greatly appreciated!

Answer №1

The problem arises from your utilization of the word-break property. Using break-all causes text to break at any character to prevent overflow.

You may want to consider using break-word instead, as it helps maintain word integrity when breaking. Keep in mind that this approach might not fully fill the container due to varying word lengths.

<!DOCTYPE html>
<html>
<head>
<style>
p {
  width: 140px;
  border: 1px solid #000000;
}

p.a {
  word-break: normal;
}

p.b {
  word-break: keep-all;
}

p.c {
  word-break: break-all;
}

p.d {
  word-break: break-word;
}
</style>
</head>
<body>

<h1>The word-break Property</h1>

<h2>word-break: normal (default):</h2>
<p class="a">Thisissomeveryveryverylong word. Words will break according to usual rules.</p>

<h2>word-break: keep-all:</h2>
<p class="b">Thisissomeveryveryverylong word. This text will-break-at-hyphens.</p>

<h2>word-break: break-all:</h2>
<p class="c">Thisissomeveryveryverylong word. This text will break at any character.</p>

<h2>word-break: break-word:</h2>
<p class="d">Thisissomeveryveryverylong word. This text will break at any word.</p>

</body>
</html>

Answer №2

One method to ensure that your text spans 100% of the width is by utilizing the text-justify property with inter-word. This will insert white space between words to expand the text to fill the entire width without breaking:

.mainbox {
    width: 100%;
    margin-top: -133px;
}

.leftimage {
    width: 350px;
    height: auto;
    position: relative;
    top: 98px;
    margin-right: 152px;
    margin-bottom: 59px !important;
    left: 8.8em;
}

.increase {
    display: block;
    margin-left: auto;
    margin-right: auto;
    cursor: pointer;
}

.textchange {
    margin-bottom: -11em;
}

.maintext {
  text-align: justify;
  text-justify: inter-word;

    font-size: 41px;
    padding: 100px;
}
<div class="mainbox">
<img class="grow-shadow increase leftimage" src="assets/img/bubbles/firstb.png" align="left">

<p class="maintext textchange"><strong>GLOBETROT WITHH ARABIC</strong> is your online language classroom that
 gives you the opportunity to learn the Arabic
 language through Skype and/or Zoom wherever 
 you are and anytime you wish. Whether you are 
 an adventurer trotting around the globe and exploring 
 its wonders, an entrepreneur seeking flexible learning 
 hours, a fulltime mom/dad, or simply someone who aspires 
 to evolve in the comfort of their home while sipping on 
 warm coffee, this is your chance to finally take on the 
 opportunity of learning the Arabic language with a lot 
 of fun and flexibility and most importantly at your own pace and comfort. 
 You do not need to commit to physical place and space or to a particular time
 to learn the Arabic language and connect with its culture and people. With 
 Globetrot with Arabic, you can make the most of the increasing space-time 
 compression and cross social and cultural borders. All you need is a phone or a 
 computer, a set of earphones, good Wi-Fi and the enthusiasm to learn the Arabic
 language and the beautiful culture that comes with it.  </p>
</div>

Answer №3

Consider using the word-wrap: break-word property for your styling needs. Hopefully, this solution will address your issue. Thank you.

div {
  border: 1px solid #ccc;
  width: 250px;
  word-wrap: break-word;
}
<div>PlaceholderTextPlaceholderTextPlaceholderTextPlaceholderTextPlaceholderTextPlaceholderTextPlaceholderTextPlaceholderTextPlaceholderTextPlaceholderTextPlaceholderTextPlaceholderTextPlaceholderTextPlaceholderTextPlaceholderText</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

What could be causing npm to fail to launch?

Whenever I execute node app.js, my server functions perfectly. However, when attempting to utilize nodemon for running the server, it fails to start. The error displayed by npm start is as follows: npm ERR! code ELIFECYCLE npm ERR! errno 9009 npm ERR! < ...

Master the art of directing your attention to a list element with ease using the tab function and jQuery

I've been tasked with creating a questionnaire for a client. They want the current active question to be displayed at 100% opacity, while the inactive questions should be at 20% opacity. Currently, when the page loads, all questions are dimmed to 20% ...

Add a CSS class to the main document via an iframe

Is there a simple method to apply a CSS class that is specifically defined in the head of an iframe to an element located in the main document? Can this be done without having to resort to using JavaScript to copy the class from the iframe's head to ...

JavaScript is not being loaded onto the client by Express

I am looking to incorporate requireJS into my client code. Here is my file structure: ProjectRoot |-server.js |-public/ |-index.html |-js/ |-app.js |-lib/ |-require.min.js |-underscore.js |-backbone.js ...

What is the best way to display the output of a Node function in HTML or console?

I am a beginner with node.js and I'm working on printing results to the console and eventually displaying them in HTML. I attempted to invoke the function as a variable that could be used later in HTML, but it was not successful. Below is an example o ...

Submit your Alpaca info without leaving this page!

Currently, I am in the process of constructing a form using PHP and ALPCA, which involves jquery and ajax. However, I seem to be encountering some difficulty when it comes to file submission while staying on the same page. Despite attempting various recomm ...

What is the best way to exclude a particular character from a text element utilizing jquery?

Looking to extract the numerical value from a div containing: <div class="balance"...>$500.48</div> The goal is to retrieve 500.48 as a number, not a string. One approach is to use alert($(".balance").text()) to verify that the content is ret ...

What is the best way to send a function or callback to a child process in Node.js?

In this scenario, imagine having a parent.js file with a method called parent var childProcess = require('child_process'); var options = { someData: {a:1, b:2, c:3}, asyncFn: function (data, callback) { /*do other async stuff here*/ } } ...

Hide button for the last input form to dynamically remove in jQuery

At first, I hide the remove button when there is only one form. However, if there are multiple forms and the user deletes all of them, I want the last form to remain visible and not be deleted. Can someone assist with this issue? Here is my code: <! ...

Using JavaScript to retrieve the URL from the window without having to submit it

How can I retrieve the current text from a page regardless of whether the URL has been submitted or not? For example, if a user changes the value directly in the URL but doesn't submit it. I've tried using window.location.href but that requires ...

Using JavaScript in Django templates: Displaying errors with a JavaScript function

Update: I recently made changes to my code, and it now looks like this: <script> function updateFunction(calibrationId) { document.getElementById(calibrationId).innerHTML = "<ul><li>" + calibrationId + "</li>" ...

Ways to align div elements

I am currently in the process of developing my own custom animation player. Utilizing Three.js for object rendering has been successful so far. However, the challenge lies in incorporating control options at the bottom of the player interface (such as play ...

Tips on sending form data, including a file, to Ajax using the onclick() method

My Modal Includes a Form: <div class="modal fade bs-example-modal-lg" id="myMODALTWO" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" id="form-content"> <div class="modal-dialog modal-lg" role="document"> ...

Query the Firebase database in Angular2 to locate the latitude and longitude values that are nearest to the user's current coordinates

I am working with a database table named locations, which contains a list of places along with their corresponding lat/long coordinates. Additionally, I am utilizing geolocation to retrieve the user's current lat/long. My goal is to identify the loc ...

Vue component with a variable number of rows, each containing a variable number of input fields

I am currently working on creating a form that can have a variable number of steps. Users should be able to add an additional step by clicking a button. Each step will contain some input fields and buttons to dynamically create more input fields. For inst ...

I need to input information into the <Pre> element using Selenium Webdriver

[insert description here][1] I am encountering issues with inserting data into the input tag. Whenever I try to do so, it shows that the element is not reachable through the keyboard. Is there a way to insert data using Javascript? Please assist me as XPat ...

Confirm the drag-and-drop functionality for customers by utilizing the accept feature

Is it possible to customize my draggable & droppable module with these classes? stackDrop1 (Kitchen) stackDrop2 (Road) stackDrop3 (Completed) https://i.sstatic.net/vxFCx.jpg If the Customer George is in the Road class, the Kitchen class will not accept ...

Using the geonames web service to access up-to-date information on earthquakes

Is there a way to utilize the longitude/latitude coordinates of a location to access a specific webservice with parameters like north, south, east, and west? Any suggestions or insights would be appreciated. Thank you! Discover Recent Earthquakes Webser ...

Is it possible to change the language on a static website without using PHP?

Exploring the option of incorporating multiple languages into my website and seeking solutions for implementing this feature. My website is static, leading me to wonder if there is a method that does not involve PHP to enable language switching through a b ...

Retrieve geographical JSON data from the mapbox API to identify if a point falls within a polygon area

I came across a helpful tutorial that guided me on how to check if a point lies within a specific polygon displayed by a marker: The tutorial worked smoothly by fetching the polygon geoJSON data using ajax and integrating it into the map. However, I alrea ...