CSS Wordwrap behaving inconsistently

After consulting a forum post, I am attempting to implement and grasp the recommended method for aligning two divs side by side using the overflow property.

Initially, the two divs align properly with my brief text. However, when I introduce longer text, it shifts below the image. Can someone shed light on why this is occurring and provide guidance on resolving it?

Visit my JSFIDDLE: Link

HTML:

<div class="container" style="overflow: hidden; width: 100%">
<div class="left">
    <img src="http://localhost/new/img/sampleimg.png" class="wall-thumb-small" />
</div>
<div class="right">
    <div style="word-wrap: break-word;">Some long text here Some long text here Some long text here Some long text here Some long text here </div>
</div>
</div>

CSS:

div.container {
border: 1px solid #000000;
overflow: hidden;
width: 100%;
}

div.left {
padding:5px;
float: left;
}

div.right { 
float: left;
}

.thumb-small{
width:35px;
height:35px;
border: 1px solid #B6BCBF;
}

Answer №1

Floats have a tendency to expand in order to contain their content, typically up to the width of the surrounding area regardless of positioning. This is why they may wrap to a new line when the text is lengthy.

If you are aiming to position an image to the left of some text, you will need to apply clearfix CSS to the outer region (to ensure all floats are encompassed):

.container {
  overflow: hidden;
  min-width: 1px;
}
/* For IE7 and newer */
*+html .container {
  min-height: 1%;
}

Following this, float only your image to the left and refrain from floating your content. Adjust margins around the image as needed, for example:

.left {
  float: left;
  margin: 0 10px 10px 0; /* 10px spacing on the right and bottom */
}

By following these steps, the content within the div will behave as expected.

Answer №2

To fix the issue with vertical placement of floated elements, it is recommended to remove the float rule on the long text. When one element is floated after another, it should not come before it vertically.

<div>
    <div style="word-wrap: break-word;">Long text content goes here Long text content goes here Long text content goes here Long text content goes here</div>
</div>

For more detailed information, refer to W3's documentation.

The outer top of a floating box should not exceed the outer top of any block or floated box created by an element earlier in the source document.

Answer №3

Try removing the float:left; from the style rule to see if that resolves the issue. Consider also optimizing and testing for compatibility with Internet Explorer versions 6 and above.

Answer №4

To prevent your text from taking up too much space and to ensure it wraps to the next line for maximum visibility, you need to set the max-width attribute.

Check out this example on jsFiddle

<div class="container" style="overflow: hidden; width: 100%">
    <div class="left">
        <img src="http://localhost/new/img/sampleimg.png" class="thumb-small" />
    </div>
    <div class="right">
    <div style="word-wrap: break-word;max-width:400px">Some long text here Some long text here   Some long text here Some long text here Some long text here </div>
    </div>
</div>

<br>

<div class="container" style="overflow: hidden; width: 100%">
    <div class="left">
        <img src="http://localhost/new/img/sampleimg.png" class="thumb-small" />
    </div>
    <div class="right">
    <div style="word-wrap: break-word;">Some short text here</div>
    </div>
</div>

Answer №5

The advice suggests that it's important to set the width of floated elements. For a deeper understanding of floats, check out this informative resource:

      http://coding.smashingmagazine.com/2007/05/01/css-float-theory-things-you-should-know/

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

Writing to multiple files using the file_put_contents function

Code snippet: function fetchAndSaveInvoices($invoicesToFetch=array()) { foreach ($invoicesToFetch as $invoice) { if ($fileStream = fopen($invoice, 'r')) { file_put_contents(PATH . '/invoices/' . ran ...

Hide object scroll percentage with CSS styling

Is there a way to dynamically hide an element based on the user's scrolling behavior? I have incorporated floating social buttons into my website, and I am looking for a solution that will hide them when the user reaches the bottom of the page (foote ...

Deciphering base64 encoded data inside an HTML document and substituting these encoded strings with their decoded representation using Python

Can someone please assist me with this flipping program that is causing me endless headaches? I am dealing with multiple files containing base64 encoded strings. Here is an excerpt from one of the files: charset=utf-8;base64,I2J...encoded string... Thes ...

Utilizing ng-href in Angular.js Template: A Guide

I am attempting to develop a simple Single Page Application (SPA) with just one index.html file that includes templates. However, I encountered an issue with the ng-href directive: <a ng-href="#/myPage">myPage</a> This works fine in index.h ...

Bx Slider not displaying second slide on mobile devices

Encountering an issue with my bx slider. It functions properly on desktops, and even when you resize the screen down. However, on mobile devices, the second slide image disappears. You can view the live site here: This is the HTML code used: <ul clas ...

How can I retrieve all the data for the chosen item in a mat-select within a mat-dialog, while still preserving the code for setting the default selected value

In my modal dialog, I have a dropdown menu populated with various languages. The ID of the selected language is bound to the (value) property for preselection purposes when data is passed into the dialog. However, I am looking for a way to also display the ...

Having trouble bringing in icons from the @mui/icons-material library

An error occurs when attempting to run the code. wait - compiling... error - ../../../../../node_modules/@mui/icons-material/esm/utils/createSvgIcon.js:1:0 Module not found: Can't resolve '@mui/material/utils' null Note: @mui/material pack ...

Updating the video tag's source attribute using JSON information

I am in the process of creating a unique video player using HTML and Javascript that will sequentially play a set of videos until all 6 have been displayed. The URLs for these videos are stored within an array that is mapped in a .json file named clips.jso ...

I must modify the HTML code so that it can generate a random response to a webhook, rather than providing a fixed reply

Currently working on some code where I'm trying to send a random message to a webhook instead of a static one. I think I need to use an array or something similar for this. Any help would be greatly appreciated! <meta charset="UTF-8"> ...

Navigate through the website by transforming the arrow and clicking the link

I am struggling with transitioning my arrow from › (right) to ˇ (down) when toggled. I can't figure out where to place it. If you look at Menu 1 > Submenu 1 > Text with hyperlink I want the 'Text with Hyperlink' to be clickable on ...

Tips for moving a title sideways and adjusting the bottom section by x pixels

I am seeking to accomplish a design similar to this using CSS in order to create a title of <h1>DISCOVER <span>WEB 3</span> DIMENSIONS</h1>. How do you recommend achieving this? Thank you, I have searched on Google and asked frie ...

The for loop seems to be malfunctioning when placed within the append function

I am facing an issue where I need to wrap the entire content into a single div so that it can be used within a loop inside the append function in JavaScript. However, after wrapping the content in a div, nothing is being displayed. Below is the code snipp ...

Unveiling the Invisible: A Guide to Revealing Conce

I have a dilemma when it comes to displaying a hidden form upon clicking a button. The problem is that the form shows up for just a brief moment before the page reloads. Here is the code I am using: Here is my JavaScript: function show(){ document.ge ...

Issue displaying values in select option tag

My issue lies in initializing a select dropdown with static values for male and female. When I try to use ngFor to dynamically populate the options, nothing appears: component.ts: export class MemberListComponent implements OnInit { user: User = JSON.pa ...

Is there a way to add a disappearing large space that vanishes when a line break occurs?

I am currently working on a website where I aim to have the name and airport code displayed in a centered title above an image of the airport. https://i.sstatic.net/Vwi2V.png The name and code will be separated by an em space: div.terminal-silhouette- ...

Trouble with Bootstrap Popover's visibility

My current setup involves a popover that is not initializing properly. The code I am using is shown below: HTML: <div data-toggle="popover" data-placement="bottom" data-content="xyz">...</div> JavaScript: $(function () { $('[data-t ...

When attempting to print a Rectangle on my webpage using JavaScript and taking user input, I encountered an error stating that 'str' is not defined

I'm trying to implement a feature where clicking on the "try it" button will prompt the user for the number of lines and then print that many lines in the form of a rectangle. However, when I run my code, nothing appears on the DOM and there is an err ...

Transforming color images into black and white using JavaScript

     I have implemented this code to convert colored images to grayscale. function applyGrayscaleEffect() {         var imageData = contextSrc.getImageData(0, 0, width, height);         var data = imageData.data;         var p1 = 0.99;   ...

Retrieve an item from the Ionic Firebase database

My current challenge is retrieving data from the Firebase database. user-service.ts getProfile(){ try {; return this.afDatabse.object(`profile/${this.afAuth.auth.currentUser.uid}`); } catch (e) { console.log(e); } } c ...

Using Javascript to extract data from the <pre> tag and store it into an array

Our report pages are set up to display information line by line at the bottom by appending a <pre> tag. For example: <pre> Site Report Info Any errors will appear as numbers: 938109283091238109281092 Account ID will be shown here. Acc ...