Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS - Div Won't Stay on Right Side of Other Div

Tags:

css

css-float

Pretty simple set-up here, I'm just trying to get two divs to sit side-by-side. Image on the left, text on the right. For some reason, if the text is too long, it pushes the div down. I'd like to just make the cs-summary div understand that it should wrap the text in order to NOT jump down like it is:

http://jsfiddle.net/csaltyj/5Huau/

Code:

<div class="container">
    <div class="cs-image">
        <img src="http://www.electroniccampus.org/school_logos/CFNC/Wake_Forest_University/Wake_Forest_University2.jpg" />
    </div>
    <div class="cs-summary">
        <p>Texty text text McTexterson likes to text. Why is div getting shoved down?</p>
    </div>
</div>
<div class="container">
    <div class="cs-image">
        <img src="http://www.electroniccampus.org/school_logos/CFNC/Wake_Forest_University/Wake_Forest_University2.jpg" />
    </div>
    <div class="cs-summary">
        <p>Super short text behaves.</p>
    </div>
</div>

CSS:

.container {
    overflow: hidden;
    border: 2px solid #0f0;
    width: 400px;
    margin-bottom: 1em;
}

.cs-image {
    float: left;
    border: 2px solid red;
}

.cs-summary {
    font-size: 0.8em;
    border: 2px solid blue;
    float: left;
    margin-left: 1em;
}

As you can see from the second container below, the short text works just fine. I don't want to hardcode any pixel or em values for how wide the text is, I just want it to conform and "know" its bounds.

Thanks in advance.

like image 421
CaptSaltyJack Avatar asked May 05 '11 17:05

CaptSaltyJack


People also ask

How do I put a div on the right side of another div?

Use CSS property to set the height and width of div and use display property to place div in side-by-side format. 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.

How do you keep a div in the right corner?

To place a div in bottom right corner of browser or iframe, we can use position:fixed along with right and bottom property value assigned to 0.

How do you get a div to stay in place?

Use position fixed. Position fixed makes it so that an element stays at it's specified place even if the user scrolls up or down.

How do I keep divs next to each other?

With CSS properties, you can easily put two <div> next to each other in HTML. Use the CSS property float to achieve this. With that, add height:100px and set margin.


1 Answers

If your div.cs-summary doesn't have a set width it will take up as much space as it can

http://jsfiddle.net/hunter/5Huau/7/

.cs-summary {
    width: 180px;
    ...
}

or you could remove the wrapping inner div elements and just do this:

http://jsfiddle.net/hunter/5Huau/11/

HTML

<div class="container">
    <img src="http://www.electroniccampus.org/school_logos/CFNC/Wake_Forest_University/Wake_Forest_University2.jpg" />   
    <p>Texty text text McTexterson likes to text. Why is div getting shoved down?</p>        
</div>
<div class="container">
    <img src="http://www.electroniccampus.org/school_logos/CFNC/Wake_Forest_University/Wake_Forest_University2.jpg" />
    <p>Super short text behaves.</p>
</div>

CSS

.container {
    overflow: hidden;
    border: 2px solid #0f0;
    width: 400px;
    margin-bottom: 1em;
}

.container img {
    float: left;
    border: 2px solid red;
}

.container p {
    font-size: 0.8em;
    border: 2px solid blue;
    margin-left: 1em;
}
like image 199
hunter Avatar answered Sep 30 '22 13:09

hunter