CSS - Struggles with Keeping Div Positioned to the Right of Another

I'm facing a simple layout issue here - trying to align two divs next to each other. Image on the left, text on the right. However, when the text is too long, it pushes the div down instead of wrapping around. I want the cs-summary div to automatically wrap text and stay in place:

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;
}

In the second container example below, you can see that short text works fine. I don't want to set fixed width values for the text, but rather have it adjust its size accordingly.

Appreciate any help with this. Thanks!

Answer №1

When your div.cs-summary does not have a specified width, it will expand to occupy the available space.

Check out this example on JSFiddle!

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

Alternatively, you can eliminate extra wrapping div elements and achieve the same result like this:

See the updated version on JSFiddle here.

HTML

<div class="container">
    <img src="http://www.example.com/image.jpg" />   
    <p>Sample text goes here. Why is the div getting pushed down?</p>        
</div>
<div class="container">
    <img src="http://www.example.com/image.jpg" />
    <p>Short text fits perfectly.</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;
}

Answer №2

Have you thought about avoiding the use of floats altogether, or is it necessary to accommodate older versions of IE?

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

.container > div {
    display: table-cell;
    vertical-align: bottom;
}
.cs-image {
    border: 2px solid red;
}

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

Answer №3

Eliminate the float:left styling from .cs-summary...

Consider adding a margin-right: 5px or a similar property to the .cs-image for spacing between them.

Answer №4

When set to float left or right (as demonstrated in your example), DIV elements will automatically expand to fit their content. To address this issue, you have a couple of options:

  1. Specify a width for the div that contains your text
  2. Rather than enclosing your text within both a div and a paragraph tag (
    <div><p>text here</p></div>
    ), you can remove the div around the paragraph tag and apply float:left directly to the paragraph tag. For instance:
    <p style="float:left;">text here</p>"

Answer №5

As pointed out by hunter and user 671670, it is necessary to use a width for both elements to fit into the container and be displayed next to each other. One solution could be to modify your HTML code by placing the image inside the text and floating it left, as shown below:

 <p>Texty text text McTexterson enjoys texting.
    Why does the div keep getting pushed down?<img style="float:left" ... /></p>

Answer №6

Here is the code snippet for you:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
  <title></title>
    <style type="text/css">
    .container {
    overflow: hidden;
    border: 2px solid #0f0;
    width: 10px;
    margin-bottom: 1em;
    width: 400px;
}

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

.cs-summary {
    font-size: 0.8em;
    border: 2px solid blue;
    margin-left: 1em;
}
    </style>
</head>
<body>

    <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>
</body>
</html>

To fix the issue with the div being pushed down, simply remove the "float: right" property from cs-summary.

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 best way to organize and position a Label and TextBox in C#

I need help styling the aspx file. The Label and TextBox elements are not aligned properly. I want the Label and TextBox to be positioned side by side with appropriate spacing. <div class="col-md-12"> <p> ...

Bootstrap - Keeping track of the collapse state of <div> elements after page refresh

Looking for some guidance as a javascript/jquery beginner - I am utilizing Bootstrap along with data-toggle and collapse classes to display or hide divs. I have been searching online trying to find a solution that will maintain the state of all divs, wheth ...

What is the method for activating the on collapse event with a bootstrap navbar?

I am encountering a common issue with collapsing the navbar on smaller screens and triggering an event when the collapse button icon is clicked. Despite my efforts to find a solution, I have been unsuccessful in using the following JavaScript code: $(&apos ...

How come the background and border of the link are shown behind the image, while the link itself is displayed in front?

There seems to be an issue with the "Give Now" and "How You Help" links on the next page. The background color applied to them is showing up behind an image, making the link appear in front of it. Any suggestions on how to resolve this? ...

Leverage the power of Bootstrap's col-push and col-pull

Here is the code snippet I am currently working with: <div class="col-md-9 col-md-offset-3"> <div class="row> <div class="col-sm-4 col-sm-push-4"></div> <div class="col-sm-8 col-sm-pull-8"></div> </div> ...

Visuals Divide the Text

"Incorporate visual elements to enhance the content." Can anyone explain the concept? How can it be implemented using html or css? Much appreciated. ...

CSS - Organization of Rows and Columns

I am in the process of designing a website that will feature news content, and I would like to give it a layout similar to a reddit news box. The layout would resemble the following structure. I am using angular material and CSS exclusively, without any fr ...

Concealing a CSS element within another one

Hey there, I've got a little issue I could use some help with. So, I've created a progress bar for my portfolio website, but there's a small hiccup. The #ability div is causing some trouble - when I adjust the width property to 1%-3%, it e ...

What is the best way to position two elements inline?

I was trying to create a container named project-item that looks like the image linked below: https://i.stack.imgur.com/6XzZ9.png My goal was to align the project logo and the title (h3) in one line, but I struggled to find a suitable solution. This is ...

Switching the background color of alternating divs in reverse order: a step-by-step guide

I am looking to alternate the background color of divs between odd and even, with the last div being grey and the second to last div being green. I have tried using the odd/even classes in CSS, but it did not work as expected. .main{ width:500px; height ...

When the browser window is resized to mobile view, a div is overlapped by an image

I've encountered an issue with the image size and position when resizing to mobile view in the browser. .extension { display: table; padding: 50px 0px 50px; width: 100%; height: auto; color: #fff; background-color: #558C89; ...

JavaScript code does not seem to be functioning properly on my computer, but it works perfectly fine on

While the code functions perfectly in JSFiddle, it seems to fail when I try to implement it in an HTML file. Despite my efforts, I am unable to pinpoint the source of the issue. If you'd like to view the working version, here is the Fiddle demo. Bel ...

Create an event listener for clicking on an image that potentially has a corner ribbon obscuring a portion of it

In my project, there is a div element with an id of items containing several bootstrap cards. Each card includes an image tag and some are accompanied by the following HTML code: <div class="ribbon"><span>Sale</span></div> This co ...

The alignment of Bootstrap input fields is off center

@import url( 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css' ); <div class="row"> <div class="col-xs-12 text-center btn-group" data-toggle="buttons"> <label class="btn btn-primary active"> ...

Circular Segment Button in Ionic 2

Is there a way to change the appearance of a segment button from square to round using CSS? https://i.sstatic.net/KRjSh.png I attempted to use traditional CSS, but encountered issues when Ionic2 attempted to transform the button on segment-activated. You ...

Fieldset is not adhering to the height of its parent

I've been grappling with this issue for quite some time now, but have yet to find a satisfactory solution. Below is the code that I've been working with: CSS .parent{ width:100px; display:table; border:1px solid green; } .child{ ...

Solving the Issue of Assigning a Random Background Color to a Dynamically Created Button from a Selection of Colors

Trying to create my own personal website through Kirby CMS has been both challenging and rewarding. One of the features I'm working on is a navigation menu that dynamically adds buttons for new pages added to the site. What I really want is for each b ...

Tips for incorporating a custom CSS design into a jQueryUI tooltip

I am struggling to apply my custom CSS class on top of the jQueryUI tooltip. Although the tooltip is displaying correctly, my styles are not being applied. Here is the code I'm using: $(document).ready(function () { $("#roles").tooltip({ content: ...

When the content in the div exceeds its boundaries, it causes it to overlap with

In my design, I have a top menu with a z-index of 999 to ensure nothing covers it. However, I am facing an issue where a scrolling div is appearing on top of the menu bar even though it shouldn't. Any idea why this is happening? Here is the CSS for t ...

What is the procedure for adjusting the padding in Material UI's datepicker?

Click here to access the codesandbox link function InlineDatePickerDemo(props) { const [selectedDate, handleDateChange] = useState(new Date()); return ( <Fragment> <MuiPickersUtilsProvider utils={DateFnsUtils}> <Keyboa ...