Do lengthy words sink while short words swim?

I need to display some text inside a box.

To achieve this, I enclose my text within an <article> element.

<article>
    <p>Here is the text that I want to display in a box.</p>
</article>

I then style it as a block with fixed width, enable word wrapping for long words, and hide overflowing text:

article {
   display: inline-block;
   width:160px;
   overflow: hidden;
   word-wrap: break-word;
}

Everything works fine so far. The text wraps correctly and long words break and wrap accordingly.

The issue arises when I add a floating image before the text.

<article>
    <img src="img.png"></img>
    <p>Now, here is the text with an image preceding it.</p>
</article>

The image is styled to float left of the text.

img {
   width: 32px;
   float: left;
}

If the text contains only short words, the image floats and wraps correctly. However, long words do not float anymore, instead they drop down to the bottom of the image.

For a demonstration, you can refer to this fiddle http://jsfiddle.net/s0pvgoqu/23/

Do long words have more weight than shorter ones?


UPDATEI am updating my question to include additional information based on the accepted answer.

It appears that the only solution to this problem is to insert <wbr> tags to break long words.

Below is the code snippet for inserting <wbr> tags in long words:

/* Inserting word break hint tags in long words at specific positions */
String.prototype.wbr = function(num) {  
  return this.replace(
    RegExp("(\\w{" + num + "})(\\w)", "g"), 
    function(match,submatch1,submatch2){return submatch1 + "<wbr>" +submatch2}
  );
}

Answer №1

One idea that came to me is using <wbr> to indicate word breaks.

While not a CSS solution, this method can be effective, especially with floated content.

Answer №2

Include word-break: break-all;

blog-post {
    display: inline-block;
    padding:12px;
    margin:2px;
    width:160px;
    height:160px;
    background-color:#eee;
    word-wrap: break-word;
    word-break: break-all;
    overflow: hidden;
}

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

The navigation bar overlays the background images

I'm struggling to adjust the positioning of my image so that it doesn't get hidden underneath my navigation bar. I want my background image to stay fixed while text scrolls over it, but I can't seem to figure out the correct height for my im ...

Create a screen divided into two separate sections using horizontal split dividers

Trying to figure out how to set a div in HTML and then have a second div take up the remaining space. It seems like it should be simple, but I'm struggling with it. I'd like to have a div with a fixed height and then have another div take up the ...

Position images inside a stylish border using CSS styling

//UPDATE: I've made some changes to the Pen, so now everyone can view the solution! Check it out here. My goal is quite simple in my opinion. I want to create an image that zooms in when you hover over it but doesn't increase in size. I attempte ...

When viewing my website on a mobile device in landscape mode, the CSS divs sized at 20% do not match the size of the div set at

Currently, I am working on a mobile application that is HTML5 cross-platform compatible. To design the app, I am utilizing Bootstrap 3, although it seems unrelated to the specific issue I am facing. Here's the problem at hand: Within my application, ...

What is the reason behind Svelte not scoping the tag under a class, but instead using individual tag.class to style a component?

It is common practice to scope CSS styles for components like this: .my-component-01 h1 { ... } .my-component-01 h2 { ... } However, Svelte takes a different approach by applying the styles directly to the tags: h1.svelte-hlsza1{color:orange} h2.svelte- ...

A feature to reveal additional content when it extends beyond a single line

<div class="questionnaire-description col-xs-12" id="questionnaire_description_wrapper"> <text-truncate> <span ng-class="questionnaire_description.show_more ? 'full-description' : 'ph-ellips ...

One-Time Age Verification Popup Requirement

Hi there! I'm facing a challenge with an age verification pop up on my webpage. Currently, the pop up appears on every page a user lands on, but I only want it to show on their first visit. I've tried using cookies to achieve this but haven' ...

Issue with jquery curvy corners not functioning properly on Internet Explorer 8

Check out my website at If you view the page in IE8 and then in IE7 compatibility mode, you'll notice a strange issue. The box on the right disappears in IE8 but displays perfectly rounded corners in IE7. I am currently using the jQuery Curvy Corner ...

Header text aligned with image to accommodate parent size

Having trouble aligning an image/logo next to my header text while maintaining its aspect ratio and fitting within the container size. The goal is to have both the text and image horizontally centered. The parent div cannot have a fixed height as it needs ...

Issues with hover functionality in Javascript, CSS, and HTML

Seeking assistance with my JavaScript, HTML, and CSS development, I ran into an issue while trying to create a hovering function for my webpage. Despite my efforts, the links are not displaying anything when hovered over, and the divs meant for specific ho ...

Error: Placeholder Loading Card malfunctioning

I attempted to incorporate a Placeholder Loading Card into my bootstrap 4 website by adding a sample image, but encountered an issue. The placeholder does not work when the website is loading. Is it supposed to always animate? Does anyone have knowledge ...

Is there a way for me to position my arrow beside the header while still keeping the toggle function intact?

Can anyone assist me in positioning my arrow next to the header text, which I toggle on click? I attempted placing the arrow <div> into the H1 tag, but then the toggle function stops working. Any help would be greatly appreciated! Please includ ...

What is the best way to ensure my background image adjusts properly for both iPhones and Android devices to be responsive?

I have set the background image in my CSS to fit perfectly without any need for adjustments. The goal is for the image to resize itself when viewed on mobile devices or tablets. .intro { display: table; width: 100%; height: auto; padding: ...

Need inspiration for testing web content with Protractor?

Exploring new possibilities for testing web content with Protractor. Any fresh ideas on what else can be tested? So far, I've checked links to ensure they redirect correctly. I've also verified text color and decoration changes when hovering ove ...

I seem to be having trouble properly sizing the width of my forms in Mui React

Having trouble with the sizing of my tabs in a project I'm working on. The first tab displays ingredients, while the second tab allows for adding new ingredients with various forms. However, when selecting different forms, the width of the form contai ...

Ways to display the main image on a blog post

This piece of code is designed for displaying the relevant post associated with wp_ai1ec_events function display_event($atts ) { global $wpdb; $event = $wpdb->get_results("SELECT * FROM wp_ai1ec_events ORDER BY start"); ...

Trouble adjusting opacity with CSS in @media queries for maximum width restrictions

When building a webpage with a collapsible header and a fading large logo that transitions to a smaller version in a different location upon scrolling down on the desktop view, there seems to be an issue. The smaller logo's opacity property does not r ...

Stop the Bootstrap row from automatically adjusting to the height of the tallest child column element, and instead utilize the extra space below the smaller element

My current framework is Bootstrap 5.2. I am aiming to create two columns, each with a col-md-6 class inside a row. You can visualize the layout through this image: . The challenge I'm facing is the inability to add another row with col-md-6 element ...

Understanding the Intrinsic Dimensions of Replaced Elements in CSS

I'm currently facing some challenges with the CSS Intrinsic & Extrinsic Sizing Module Level 3, specifically chapter 4.1 on Intrinsic Sizes. This section is proving to be quite difficult for me: When a block-level or inline-level replaced element h ...