Float left word wrap

Let’s say I have this.

<div>
<div id="lala">
lalalala
</div>
</div>

css:

#lala
{
   float:left;
   word-wrap:break-word;
}

if the text inside #lala is big, it will look like this.

|                              |
|                              |
| lalalalalalalalalalalalalala | lalalalalala <-- these won't be show.
|                              |
|                              |

If I remove float:left; this will be the result.

|                              |
|                              |
| lalalalalalalalalalalalalala |
| lalalalalala                 |
|                              |

How can I have the second result while still using float?

asked Dec 9, 2012 at 9:07

user1665700's user avatar

1

give the element a fixed width so we know where to break the word:

#lala
{
float:left;
word-wrap:break-word;
width:?px;
}

The word-wrap:break-word property should only apply if the element has a visual rendering, is an inline element with explicit height/width, is absolutely positioned and/or is a block element.

answered Dec 9, 2012 at 9:10

Justin McDonald's user avatar

2

My HTML looks like this:

<body>
  <div class="nav"><ul>...</ul></div>
  <div class="view">this text won't wrap if I resize browser</div>
</body>

and my CSS looks like this:

.nav {
    width:200px;
    float:left;
    font-weight:bold;
    margin-right:46px;
}

.nav a {
    font-weight:normal;
}

.nav ul {
    padding:0;
    margin:0;
    list-style-type:none;
    text-align:right;
}

.nav ul li {
    margin-bottom:7px;
}

.view {
    float:left;
}

If I resize the browser to be skinnier, then it won’t word wrap the text in the view div at all. It will just un-float the view div and put it below the nav div.

How can I make the text in the view div word wrap instead of un-float to under the nav div?

mskfisher's user avatar

mskfisher

3,2634 gold badges35 silver badges48 bronze badges

asked Nov 4, 2011 at 22:09

Ryan's user avatar

You want your .nav div to be 200 pixels wide and I assume you want 46 pixels between the .nav div and the .view div, at least that’s what I understand from the margin-right:46px on the .nav div. You don’t need to float the .view div. In fact, you can’t because if it’s floated, the only way to get it next to the .nav div is to set a width (otherwise it will default to 100% of it’s parent). But you can’t set the width because you want it to grow and shrink with the size of the browser.

So floating is not an option but also not necessary. The .nav div is floated and because of this the .view div will appear underneath the .nav div (because floated div’s are taken out of the flow). To make the .view div appear next to the .nav div you simply set a margin-left of 246 pixels (200px width of .nav + 46px margin).

.nav {
    width:200px;
    float:left;
}

.view {
    margin-left:246px;
}

answered Nov 4, 2011 at 22:57

Marco Miltenburg's user avatar

Marco MiltenburgMarco Miltenburg

6,1131 gold badge29 silver badges29 bronze badges

1

You need to set a width on floated elements, otherwise they will take UP TO 100% width of their containers.

also, you should clear your float containers.

answered Nov 4, 2011 at 22:20

Jason's user avatar

JasonJason

51.2k37 gold badges133 silver badges185 bronze badges

SitePoint Forums | Web Development & Design Community

Loading

  • Remove From My Forums
  • Question

  • User1324895001 posted

    I have a div tag that is configured like below, but just had a case where the data presented within the div was to wide, which cause the div to get pushed down a row and to the left.. currently you will see that i added a width to the style of the div
    and that corrected it, but is that the best way?

    Is there a way to force the div to wrap the contents with the use of width? If i remove the width, it moves downa nd shifts to the left, if i leave the width the page positions and looks right.

    <div style="float:left;display:inline; width:265px;">

     

Answers

  • User1224194097 posted

    You can force wrap using Word-Wrap style attribute

    <div style="floatleftdisplayinlinewidth265pxword-breakbreak-allword-wrapbreak-word;">
        your content here
    </div>
    • Marked as answer by

      Thursday, October 7, 2021 12:00 AM

  • User442013226 posted

    If you use fixed width it will wrap anyway without the word-wrap style. 

    Not necessarily. If the content has spaces or carriage returns, text might wrap and if the content is continuous long text, it NEVER wraps without word-wrap.

    Ex: ajdlkfjlksadjflksajflksajdflkjsadlkfjsalkadfasdfsadfdsafdsafdsa

     

    This is very true, my apologies.

     Nevertheless, as you have figured out already, the main thing to do will be to set a fixed width, and optionally add word-wrap to allow for cases in which the text wont automatically wrapped

    • Marked as answer by
      Anonymous
      Thursday, October 7, 2021 12:00 AM

Imagine a print magazine. Flipping through the pages, you see text and images laid out in different ways. Maybe there’s an image at the center with text all around it. Maybe there’s an image to the left of the intro paragraph. Maybe there’s an image with text overlaid on top of it.

woman using a laptop computer at a desk to use the CSS float property

Mostly, though, you’ll see text wrapped around images. This makes it easier to see both the text and the images while limiting the amount of white space on the page.

Like print designers, web designers need a solution for making text wrap around an element in a layout — this ensures they maintain the flow of the page while maximizing space.

Download Now: 50 Code Templates [Free Snippets]

That solution is the float property in CSS. In this post, we’ll look at what float is and what it does in CSS. Then we’ll explore how to use — and clear — the property. Let’s get started.

What is the float property in CSS?

The CSS float property controls the positioning and formatting of content on the page. Its most common use is to wrap text around images. However, you can use the float property to wrap any inline elements around a defined HTML element, including lists, paragraphs, divs, spans, tables, iframes, and blockquotes.

The float property can be specified with any of the following values:

  • none (default): The element doesn’t float. It is simply displayed where it occurs in the text.
  • left: The element floats to the left of its container.
  • right: The element floats to the right of its container.
  • inherit: The element inherits the float value of its parent.

Notice that there are no property values for center, top, and bottom. That’s because you can’t float elements to the center, top, or bottom of a container — only left or right.

If you’re using the Bootstrap CSS framework to build your site, then there are additional values to create responsive floats. Otherwise, you’ll need to use media queries and set the specified screen width in pixels for when the element should float.

Now that we understand the basics of the CSS float property, let’s take a closer look at how it works.

What does float do in CSS?

In CSS, the float property specifies how an element should float. The floated element will be removed from the normal flow of the page, but it will remain part of the flow — meaning, the element will be shifted to the left or right until it touches the edge of its container or another floated element.

This is the key difference between the float property and the absolute position property in CSS.

While floated elements are still a part of the flow of the page, elements using absolute positioning are not. That means floated elements will affect the position of other elements and vice versa. Absolutely positioned elements, on the other hand, will not affect the position of other elements nor will other elements affect their position.

Now that we understand how the float property works, let’s look at how to use it in CSS.

To use float in CSS, you only need a CSS selector and the defined float property inside the brackets. So the syntax would look something like:

 

element { float: value; }

While float will function properly on its own, you’ll often see it combined with margin properties. Creating space around the floated elements can help improve the appearance of the layout.

CSS Float Examples

Now, let’s look at some specific use cases of the float property.

How do you float an image in CSS?

Let’s say you want an image to float to the right of the text in a container. You can use a type selector to target the image and define it with the rule float: left or float: right.

Here’s the CSS:

 

img { float: right; }

Here’s the HTML:

 

<h2>CSS Float Property: Right Value</h2>

<h2>CSS Float Property: Right Value</h2>

<p><img src='https://images.unsplash.com/photo-1613535449480-bb56b88d1886?crop=entropy&cs=tinysrgb&fit=max&fm=jpg&ixid=MnwxNDU4OXwwfDF8cmFuZG9tfHx8fHx8fHx8MTYxNzgwNzU4MA&ixlib=rb-1.2.1&q=80&w=400' alt='surfer holding a surfboard walking in the ocean'>In this example, the CSS float property is defined by the right value. That tells the browser two things. The first is that the image floats to the right in the paragraph. The second is that the text in the paragraph wraps around the image. Notice that the CSS includes a type selector to target the image, which is contained in the paragraph.</p>

Here’s the result:

See the Pen CSS Float Property: Right Value by Christina Perricone (@hubspot) on CodePen.

Compare how the image appears on the page below when the float property is not applied.

See the Pen CSS Float Property: None by Christina Perricone (@hubspot) on CodePen.

How do you float other HTML elements in CSS?

As mentioned earlier, any HTML element can be floated in CSS, not just images.

Let’s say you want a button to float to the left of the text in a container. You can use a class selector to target the button class and define it with the rule float: left or float: right. You’ll also see CSS rules defining the color, size, border, padding, and margins of the button.

Here’s the CSS:

 

.button {

float: left;

background: gray;

font-size: 48px;

color: white;

padding: 3px;

border-radius: 5px;

border: 3px solid black;

margin-right: 10px;

}

Here’s the HTML:

 

<h2>CSS Float Property: Button</h2>

<p><button class="button">Click me!</button>In this example, the CSS float property is defined by the left value. That makes the button float to the left in the paragraph and makes the text in the paragraph wrap around the button. Margin settings have also been added to add some space between the text and the button. This makes it easier to read and more visually appealing. Notice that the CSS includes a class selector to target the button defined by the button class.</p>

Here’s the result:

See the Pen CSS Float Property: Button by Christina Perricone (@hubspot) on CodePen.

How do you float multiple elements in CSS?

Now let’s say I want to float both elements — the image and button — to the left. The button will only go as far to the left as the image (and its margin settings allow). Take a look below.

Here’s the CSS:

 

.button {

float: left;

background: gray;

font-size: 48px;

color: white;

padding: 3px;

border-radius: 5px;

border: 3px solid black;

margin-right: 15px;

}

img {

float: left;

margin-right: 15px;

width: 70px;

height: 70px;

}

Here’s the HTML:

 

<h2>CSS Float Property: Multiple Elements</h2>

<p>

<img src='https://images.unsplash.com/photo-1613535449480-bb56b88d1886?crop=entropy&cs=tinysrgb&fit=max&fm=jpg&ixid=MnwxNDU4OXwwfDF8cmFuZG9tfHx8fHx8fHx8MTYxNzgwNzU4MA&ixlib=rb-1.2.1&q=80&w=400' alt='surfer holding a surfboard walking in the ocean'>

<button class="button">Click me!</button>

In this example, the CSS float property is defined by the left value. That makes the button float to the left in the paragraph and makes the text in the paragraph wrap around the button. Margin settings have also been added to add some space between the text and the button. This makes it easier to read and more visually appealing. Notice that the CSS includes a class selector to target the button defined by the button class.

</p>

Here’s the result (note: You may need to zoom out to 0.5x to see the full effect.)

See the Pen CSS Float Property: Multiple Elements by Christina Perricone (@hubspot) on CodePen.

How to Clear the Float Property in CSS

While useful, the float property can cause layout issues. Consider what happens if a containing element, like a <div> element, is too small to contain the floated element. When this happens, the floated element will extend beyond the bounds of its containing element and disrupt other parts of your page.

Issues like this can be fixed using the clear property in CSS. This property lets you «clear» floated elements from the left side, right side, or both sides of an element.

The clear property can be specified with any of the following values:

  • none (default): Floating elements are allowed on both sides of the cleared element.
  • left: No floating elements are allowed on the left side of the cleared element.
  • right: No floating elements are allowed on the right side of the cleared element.
  • both: No floating elements are allowed on either side of the cleared element.
  • inherit: The element inherits the clear value of its parent.

Most commonly, you’ll apply the clear property to an element that comes immediately after a floated element. Say you want to clear the float to the left (in other words, you don’t want any floating elements on the left side of the defined element). Then, you need to match the clear to the float.

So if the element is floated to the right, then the following element should be cleared to the right. That way, the floated element will float to the right and the cleared element will appear below it.

Consider this example — notice that div 2 comes after div 1 in the HTML. However, since div 1 floats to the right, the text in div 2 flows around div 1.

See the Pen CSS Float Property: Without Clear by Christina Perricone (@hubspot) on CodePen.

To keep the text in div2 from flowing around div1, you can use the clear property, which moves div 2 below the right-floating div 1. Here’s the result:

See the Pen CSS Float Property: With Clear by Christina Perricone (@hubspot) on CodePen.

Using Floated Elements in Your Design

Understanding how to use and clear the float property in CSS can help you create clean layouts that delight your visitors. You’ll just need some knowledge of HTML and CSS to master this property.

Editor’s note: This post was originally published in May 2020 and has been updated for comprehensiveness.

New Call-to-action

Понравилась статья? Поделить с друзьями:
  • Flip words in word
  • Flight number in word
  • Flexing on my excel
  • Flatter meaning of the word
  • Flash анимация в word