Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set floated div's text to float and wrap properly without hardcoding div width?

Tags:

html

css

Take a look at the following jsfiddle:

http://jsfiddle.net/tTNNm/

How do you get the div.text to be floated next to the image div without hard-coding the width of div.text in the CSS?

A div is supposed to be the full width of whatever it's content is and then the height is supposed to be determined by how much of the content needs to wrap to the next line due to width constraints of it's parent element. So in my fiddle, how come the div.text takes on the full 300px width and force itself to the next line instead of fitting in next to the image's div with a calculated width of 200px?

Reference:

<div class="test">
    <div>
        <img src="http://www.cadcourse.com/winston/Images/SoccerBall.jpg" width="100" height="100"/>
    </div>
    <div class='text'>
        Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
    </div>
</div>​

div.test {
    width: 300px;
    outline: 1px solid blue;
    height: 100px;
}

div.test div {
    float: left;
    outline: 1px solid red;
}

like image 821
Jake Wilson Avatar asked Apr 17 '12 21:04

Jake Wilson


People also ask

How to wrap text around a Div in CSS?

Although DIV will follow your command and occupy only 50% of width –but it will still not allow other elements to wrap around it. At times, we need to wrap text around a DIV. In such cases, CSS property float comes to our rescue. This property float forces DIV to give space to other elements on its sides.

What is the difference between float left and float right in HTML?

float:left; This property is used for those elements (div) that will float on left side. float:right; This property is used for those elements (div) that will float on right side. Example 1: This example place three div side by side using float property. It is a good platform to learn programming. It is an educational website.

How to float three Div side by side using CSS?

How to float three div side by side using CSS? - GeeksforGeeks How to float three div side by side using CSS? Three or more different div can be put side-by-side using CSS. Use CSS property to set the height and width of div and use display property to place div in side-by-side format.

What is the default value of float in CSS?

The float property can have one of the following values: left - The element floats to the left of its container. right - The element floats to the right of its container. none - The element does not float (will be displayed just where it occurs in the text). This is default.


1 Answers

It's because both divs are inheritting the float: left;. Try adding this to your CSS:

div.test div.text {
    float: none;
    outline: 1px solid red;
}
like image 94
wachpwnski Avatar answered Nov 11 '22 18:11

wachpwnski