What is the best way to enclose a div that holds spans with the text that needs to be

Considering the following HTML structure:

<div>
    <span>foo</span>
    <span>bar</span>
    <span>some really long text with word breaks</span>
    <span>baz</span>
</div>

Upon rendering, it currently displays as:

foo bar some really long text with word breaks baz

The desired display format is:

foo bar some really 
long text with word 
breaks baz

In exploring solutions to achieve this layout modification, various resources were consulted including:

  • Make text wrap inside an absolutely positioned div
  • CSS word-wrapping in div

Although successful when directly styling the text within a <div>, difficulties arise when attempting to replicate this behavior with text distributed across multiple <span> elements.

In light of these constraints, where direct alterations to the <span> arrangement are not feasible, what alternative approaches can be employed to address this issue?


UPDATE:

Presently, the CSS properties applied to the <div> in Chrome web inspector are as follows:

color: rgb(0, 105, 173);
display: block;
font-family: Arial, sans-serif;
font-size: 25px;
font-weight: bold;
height: 93px;
margin-bottom: 0px;
margin-left: 208px;
margin-right: 208px;
margin-top: 0px;
text-align: center;
text-shadow: rgb(255, 255, 255) 0px 0px 3px;
width: 300px;

Answer №1

To start off, the first task is to specify the width of this particular element.

div{
    width: 250px;
    word-wrap: break-word; /*option to allow breaking unbreakable words*/
    display: inline-block; /*Although displayed inline, it behaves as a block element*/
}

Check out the live demo on JSFiddle

Answer №2

Be sure to set the width of your div element

div{width: 150px;}

see demo

Answer №3

Give this a shot...

 div {
    text-align: justify;
    width: 130px;
}

See it in action

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

Eliminate entry upon checkbox completion from task schedule

Currently working on developing my own to-do list web application. I have set everything up except for one thing. I am trying to figure out how to remove a checkbox from the array when it is checked. Can someone please assist me in achieving this using DOM ...

Information will not be displayed when using getjson (undefined)

The data from the API is showing as undefined and I'm not sure why. Here is the HTML snippet: $(document).ready(function() { $.getJSON("https://data.iledefrance.fr/api/records/1.0/search/?dataset=repertoire-bibliotheques&q=&refine.typeins ...

The issue of the footer kicking out of place in the HTML layout was successfully resolved after

I am in the process of creating a simple footer that will always stay at the bottom of the page. The content on the page will not exceed the screen size, eliminating the need for scrolling. (There are a total of 5 HTML pages, and I want the footer and head ...

What is the mechanism through which Material Icons determine the appropriate icon to display according to the element's content?

Can anyone explain how the icons in this specific structure function? <span class="material-icons"> accessibility_new </span> I've been exploring the Material Icons repository in an attempt to comprehend how the icon is displ ...

Unable to invoke Kendo JavaScript

I'm facing an issue with my source code where I am trying to call kendo.all.min.js. Everything works fine when I call kendo.common.min.css and kendo.default.min.css, but as soon as I try to call kendo.all.min.js, I encounter the following error: http ...

Utilize PHP to select the same checkbox across multiple pages of a form through a post request

I am a newcomer to coding and I am trying to figure out how to automatically select the same check-box that was marked on the first page of my multi-page form. For example, if the user checks the box labeled Red, I want the box on page 2 to be auto-filled ...

Trouble getting inline SVG to scale properly with its parent

Attempting to utilize SVG as an alternative to clippath due to limited cross-browser support has proven challenging. Each time I make the attempt, I face the same issue: the SVG functions properly but appears small and does not scale to fit the designated ...

The issue encountered when trying to load Javascript through PHP

As a beginner, please be patient with me. I am attempting to dynamically load this JavaScript easy slider into a div on my index page using the following code ... switch($_GET['page']) { case '#YELLOW' : $page = ' <div id ...

Can a rotation animation be incorporated into an image in next.js when it is clicked?

Looking to enhance a next.js image with an animation? How about making it rotate 360 degrees upon each click? Despite my attempts at toggling classes, I can't seem to achieve the desired outcome. Any guidance would be greatly appreciated. Thank you in ...

Issue with creating a custom YouTube navigation bar positioned above suggested video content

bar image Currently, I am attempting to recreate this navigation bar for my YouTube-inspired project. Although I have successfully made the background of the buttons fade using linear-gradient, I am unsure how to apply the same effect to the text itself. ...

When you zoom in, the HTML elements tend to shift out of place

Spent the entire day yesterday attempting to make my page responsive while Zooming In, but I just can't seem to get it right. Even after adding height and weight, elements still get mixed up when zooming in on the page. I've scoured countless w ...

What happens when a PHP field is left empty?

I have created 3 PHP files. The first file contains fields to be filled out, similar to an HTML form. The second file takes the data from the first file and saves it in a text file with the title as the date and time. The third file confirms the registrati ...

Jquery and CSS3 come together in the immersive 3D exhibit chamber

Recently, I stumbled upon an amazing 3D image gallery example created using jQuery and CSS3. http://tympanus.net/codrops/2013/01/15/3d-image-gallery-room/ Excited by the concept, I attempted to incorporate a zoom effect (triggered every time a user clic ...

Showing the URL beyond the search bar: A Guide using PHP, JavaScript, and HTML

How can I display the URL link outside the search box instead of opening a new page with the search result? I want to show the full URL (https://www.php.net.) below the search box, not within the search results. I only want to see the URL, not the contents ...

Issues arise when incorporating an iframe into a responsive website without the use of CSS styling

Having trouble with my site's iframes. Created animated banners using Bannersnack and embedded them, but the images always appear optimized for mobile, even on desktop. View the site here: The image at the bottom scales correctly, but not the animat ...

Regular expressions can be used to locate a specific string and remove the href attribute associated with it

I need to eliminate the anchor tag associated with "check out the slideshow". The text for checking out the slideshow may change, so we must use #show-gallery to identify it from the href. var jStr = $('.gallery-wrapper').html(); cStr = jStr ...

Personalized validation messages with jQuery

I am currently working on a contact form using jQuery, but I am facing challenges in displaying my custom validation messages. Instead of showing the message I specified, it only displays a small popup with the generic message: "Please fill in the form". I ...

Leveraging backstretch.js in combination with blur.js (or equivalent)

Query Update I provided a response to the query below, however, I am experiencing some lag and a noticeable stepped transition between images. Can anyone offer advice on how to optimize this issue? Perhaps it would be better to go back to using a static ...

Is there a way to switch the cursor from grab to grabbing when the mouse is clicked down?

I am working on a draggable element and I would like the cursor to change to grab when hovering over it, and switch to grabbing when the user clicks and holds the mouse down. My current implementation closely follows the recommended approach in the top an ...

The Bootstrap Mobile Navigation transition is not displaying as intended in my CSS coding

On both desktop and mobile screen sizes, I have a white navigation bar. However, for the mobile dropdown menu, I want the background to be grey. I managed to achieve this by targeting .show on smaller screens in the CSS and specifying the grey background ...