Ways to position divs within a <li> element while maintaining vertical stacking of the <li> entries?

I have an unordered list where each list item will contain 2 divs. I want the divs to float next to each other, while still having the list items stack vertically. The issue is that all my divs are floating instead of each list item going beneath the previous one. What style changes do I need to make here?

Thanks!

// Add the message to the page.
$('#conversation').append('<li><div class="chat-container"><div class="' + cssStyle + '"></div><div class="chat-text"><strong>' + encodedName + '</strong>:&nbsp;&nbsp;' + encodedMsg + '</div><div></li>');

My Styles:

#conversation >li > div > div {
    float: left;
}
.legend {
    font-size: 20px;
}
.chat-text {
    padding-left: 20px;
}

With the answer I now achieve the desired result.

Answer №1

You seem to be experiencing a clearfix issue.

To address this problem, consider adding the following style to your code. It may be necessary to define this as a CSS class to prevent it from affecting all li elements:

li{
    overflow: auto;
}

View a functional example here: http://jsfiddle.net/6J8SU/

To implement the class, modify your script like this:

$('#conversation').append('<li class="row"><div class="chat-container"><div class="' + cssStyle + '"></div><div class="chat-text"><strong>'
            + encodedName + '</strong>:&nbsp;&nbsp;' + encodedMsg + '</div><div></li>');

Don't forget to include the following styling as well:

.row{
    overflow: auto;
}

Example with the added Class: http://jsfiddle.net/6J8SU/1/

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

Create a DIV element that completely fills the vertical space available

My webpage is currently looking quite empty, with only one DIV element present: <div style="height: 20%; min-height: 10px; max-height: 100px; width: 100%; background-color: blue;"></div> I' ...

The Google Javascript API Photo getURL function provides a temporary URL that may not always lead to the correct photo_reference

Currently, I am integrating Google Autocomplete with Maps Javascript API into my Angular 5 application. As part of my website functionality, I fetch details about a place, including available photos. The photo URL is obtained through the getURL() method. ...

I'm having trouble locating the container with only one child element in JavaScript

I am currently working on a project where I need to locate a specific container using JavaScript. More specifically, I am looking to target the container that houses the required address. My aim is to assign a class to the container of the requested addre ...

Send data from JavaScript variables to PHP script

I've been trying to figure out how to pass variables from a js function to my database using php, but I've hit a roadblock. Any suggestions on how I can resolve this issue would be greatly appreciated. <html xmlns="http://www.w3.org/1999/xhtm ...

I am having trouble getting my tag to function properly within the if statement of my template

My goal is to show a notification badge in my inbox only if there are more than 0 messages. However, I'm facing an issue with my if statement. I'm uncertain whether it's a syntax problem or a logic error. The message count is correctly displ ...

As you scroll, the header gradually reduces in size, but at the point where the reduction occurs, a slight flick

As I develop a website, I have implemented a sticky header that shrinks after scrolling past the window height. I experimented with two versions - one using Vanilla-JS and another with jQuery. Both versions work fine, but the issue arises when the header s ...

Script to Trigger Download Button

I'm having trouble with a rar file download on my website (ravens-hangar.tk). A bunch of lines of script keep popping up. Can anyone help me out? Thanks in advance! .download { width: 120px; height: 30px; font-size: 20px; text-align: center ...

Issues with React router arise once the app has been built

I am new to utilizing react and react-router in my project. I have built the application using create-react-app and now I am facing an issue with routing between two main pages. After trying different approaches, I managed to get it working during develop ...

"JavaScript throws an 'Undefined' error when trying to access a basic

I am struggling with a basic set of HTML and JS files that are not functioning properly, and I can't seem to pinpoint the issue: <html> <head> <script type="text/javascript" src="data.json"></script> < ...

Include a selection of images within a popover box

I am currently working on a component that contains various experiences, each with its own popover. My goal is to display an array of images within each popover. Specifically, I have different arrays of images named gastronomiaExperience, deportesExperien ...

In the case of a PDF being in landscape orientation, the full width of the document is not utilized despite the width being set to

Even though I have specified the width of the Header table to be 100% and applied a margin-right of 1cm for PDF reports with landscape orientation, the table is not taking up the full width. However, when my PDF report has portrait orientation, the header ...

Verifying the absence of values in destructured variables

In my code, I have set up three constants based on an object. const {standing_bid_amt, min_increment, starting_price} = props.auction.auction; The problem arises when the object auction is empty, resulting in the constants being undefined. To address this ...

Encountered an error while using vue-tiny-slider: Uncaught TypeError in promise - t2 is not recognized as a function

I'm currently using Vue 3 and have installed the vue-tiny-slider library from https://github.com/viktorlarsson/vue-tiny-slider. However, I encountered an error stating 't2 is not a function'. Since the documentation doesn't provide inst ...

What is the best way to test for errors thrown by async functions using chai or chai-as-promised?

Below is the function in question: async foo() : Promise<Object> { if(...) throw new Error } I'm wondering how I should go about testing whether the error is thrown. This is my current approach: it("checking for thrown error", asy ...

What is the best way to retrieve the root binding node from a viewmodel in order to apply jQuery.blockUI when performing an AJAX post request?

Within my code, I have a designated DIV element that serves as the root node for applying knockout bindings like so: ko.applyBindings(viewModel, document.getElementById('myContainerDiv')); In all of my viewmodel types, there is a generic post m ...

Selecting a pair of radio buttons to toggle the visibility of different div elements using JavaScript

I've been working on two radio button categories that control the visibility of different input fields. I'm making progress, but I'm still struggling to get it to work perfectly. Below are the images for reference: The combination of &apos ...

Alert classes are not included in bootstrap 5 npm package

In my Angular 13 project, I am utilizing Bootstrap version 5.2.1. While most components are working as expected, the alert component seems to be missing some styling elements. Specifically, I have noticed that alerts with the classes alert-success and aler ...

Step-by-step guide to showing a div upon clicking an element using jQuery

I am facing a challenge with an invisible div that I need to make visible upon clicking an element. However, my current approach does not seem to be working. Can you help me figure out what I am missing? I attempted to target <a class=".demo"> using ...

Attempting to send an array of files to a Meteor function

When calling a method within a submit button event, the code looks like this: 'submit #form': function(event, tmpl){ var files = null; if(event.target.fileInput) files = event.target.fileInput.files; console.log(f); Met ...

JavaScript - An Efficient Method to Retrieve Data Imported from the Controller via Ajax

I am a beginner and I'm having trouble getting a list of objects called "Event" (OrderId, Date) from the MVC controller to JavaScript in the view. When I try to display the list as strings, it shows me "undefined". On the controller side, everything ...