Utilizing Javascript and CSS for horizontal alignment from left to right

I have a JavaScript function that generates a list of store locations and creates a DIV beneath the map. Currently, the items are displayed top to bottom on the page.

However, I would like to change the layout so that the items are shown as 3 in a row from left to right, and then go top to bottom.

The function looks like this:

function createSidebarEntry(marker, name, address, city, state, zipcode, telephone, images, url) {
    var div = document.createElement('div');
    var html = "<p><a href='http://" + url + "'>" + name + "</a><br/>" + address + "<br/>" + city + ", " + state + " " + zipcode + "<br/>" + (formatPhone(telephone)) + "</p>";
    div.innerHTML = html;
    div.style.marginBottom = '5px'; 
    return div;
}

Also, I'm wondering if using tables would be a better approach for this task? I've tried styling the DIV like this:

#sidebar {
    text-align: left;
    margin: 0px auto;
    padding: 0px;
    border:0;
    width:665px;
    font-size:10pt;
    font-family:Arial;
    color:#656668;
    display: table-cell;
}

Unfortunately, despite playing around with margins and other properties, I haven't been successful. Has anyone successfully formatted dynamic data into three columns using CSS within a DIV container? I've searched online but couldn't find a solution that works for me.

Answer №1

It is recommended to set the parent element with a fixed width and apply float to the child elements, giving them a width of 33%. Remember to add a clear property afterwards for proper layout.

Answer №2

In my opinion, using jQuery to create your elements is a more efficient and stylish approach.

However, the main issue you're facing is that you need to include a style of float:left; in your div. This will ensure all your divs are aligned in the same row.

Also, I recommend minimizing the use of tables for laying out elements on your page. Instead, utilize divs and CSS for a cleaner layout design.

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

Tips for using the arrow keys to navigate the cursor/caret within an input field

I am trying to create a function that allows the cursor/caret to move inside an input field character by character using the arrow keys (ArrowLeft, ArrowRight) on a keydown event. Current Approach: const handleKeyDown = (e: KeyboardEvent<HTMLInputEle ...

eliminating list item from unordered list depending on the input of either first or last name

My objective is to refine a lengthy list as the user types in a search for a specific person by first or last name. The current code I have functions adequately, but encounters an issue when there is a space in the input field. My goal is to allow users to ...

Implementing Jquery Datatables in C# Codebase

I'm struggling to populate my jQuery Datatable with a List. Currently, I am writing the list to a file and accessing it in my view-model. Is this the correct approach? This is the snippet of my code: List<string> list = new List<string> ...

a guide on utilizing knockout js to bind json data within a datalist

Below is the code I have written for my web services: public class SympsService : System.Web.Services.WebService { [WebMethod] public symps GetSymptoms(string organ_name) { symps Symptoms = new symps(); string CS = Configurati ...

Creating a user-friendly form in a Nuxt/Vue application that enables dynamic attribute creation

I am currently working on designing a form that enables users to create different variations for a product within a Nuxt/Vue application. The goal is to provide users with the ability to specify attributes for each variation using a text field. These attr ...

NodeJS/WebdriverJS: Error - 'Element is not connected to the webpage'

Hello there! I am new to WebDriver (chromedriver) and my knowledge of JavaScript syntax is quite basic, so please excuse me if my code appears messy. Currently, I have a clickAllElements(); function that identifies all elements within a class and performs ...

Encountering a problem with React components displaying incorrect sizes while utilizing flexbox styling

I am creating a basic test application using NextJS/React. Take a look at the code snippet below: The content of Board.tsx file: import './Board.css'; export default function Board() { return <div className="board"> < ...

What is the most effective method to guarantee the creation of an object prior to navigating through the code that relies on it?

I recently encountered an issue with my code that utilizes fetch() to retrieve and convert .tiff images into an html5 canvas for display in a browser using tiff.js (https://github.com/seikichi/tiff.js/tree/master). While the functionality mostly works as i ...

Trouble with AJAX communicating with PHP and not rendering fresh data

I have created a row of images that are clickable, and once clicked, a variable is sent via AJAX to a PHP file for a database query. The PHP file receives the variable and executes the correct query. However, instead of updating the HTML on my page, it sim ...

Is it possible to use CSS to target the nth-child while also excluding certain elements with

How can I create a clear break after visible div elements using pure CSS? Since there isn't a :visible selector in CSS, I attempted to assign a hidden class to the div elements that should be hidden. .box.hidden { background: red; } .box:not(.hid ...

Is it possible to achieve this shaded gradient effect using CSS3?

Check out this image for reference. I've managed to style a horizontal rule using SVG as the background image, creating a specific effect. However, I am struggling to achieve the same result using CSS3 alone. If there are any CSS experts who can adv ...

Tips for setting up a grid where boxes have a consistent width but varying heights to maximize space utilization

Seeking design inspiration from Pinterest, where fixed width columns are used but each grid item fills all available space with varying heights. Check out this example: Initially considered using flexbox for this layout, but faced some challenges. Here&ap ...

Run module following a POST request

I am currently working on integrating real-time information transmission through sockets using socket.io, along with push notifications sent via the OneSignal platform. However, I have encountered an issue where placing both functionalities in the same mo ...

Inject CSS into an iframe containing a JavaScript form by iterating over a collection of iframes

My task is to manipulate an iframe (chatbox) once it's loaded on a webpage. This chatbox consists of four iframes, each with a different id that changes with every page load. Since the iframe that needs manipulation is always the last one in the list, ...

Guide on automatically removing a DOM element in Jquery after a set amount of time

Is there a way to delete an HTML element after a specific period of time? If a certain condition is met before the allotted time, I want to pause the timer from deleting the element. Here's a snippet of code for reference: <ul> <li dat ...

Guide to comparing 2 arrays and determining the quantity of identical elements

If an AJAX call returns 2 arrays upon successful execution, the arrays will be different each time but may contain some common elements. For instance: array1 = [x, y, z, a] array2 = [x, y, y, y, x, z, y, z, z] The goal is to determine how many times eac ...

Sorting items using jQuery filter

I am working with two sortable div containers that are connected using connectWith. Both containers contain draggable items that can be moved as desired. These items have specific classes such as group1 and group2. Let's refer to the containers as con ...

The function Jquery.html() seems to be malfunctioning in IE7, however, innerHTML is working correctly in the

One of my recent implementations involves populating a div using an ajax response. Take a look at the code snippet below for better understanding: jQuery.ajax({ type: 'POST', url: url, dataType: 'json', data:data, s ...

Use markdown links instead of html hyperlinks

I encountered a scenario where I have a string formatted like this: 'Hello, my name is <a href="https://stackoverflow.com/foo/bar">foobar</a> My favorite food is <a href="https://google.com/food/pizza">pizza</a ...

Implementing JavaScript alerts with the Internet Explorer WebDriver and Selenium

Currently, I am facing a challenge with a Java based application where I need to click on a cancel button. I am using IE Driver, Eclipse IDE, and my application only supports IE. [I am scripting in Java] Here is the scenario: Login to the application An ...