What is the best way to horizontally position two divs side by side?

I am trying to arrange two divs next to each other, each containing a title and a list of items. Here is an example:

<div>
    <span>list of sources</span>
    <select size="10">
        <option />
        <option />
        <option />
    </select>
</div>

<div>
    <span>list of destinations</span>
    <select size="10">
        <option />
        <option />
        <option />
    </select>
</div>

While it's easy to accomplish this with tables, I prefer not to use them. How can I achieve the desired layout?

Answer №1

Arrange the div elements within a parent container and give them this styling:

.parentDiv div {
    float: left;
    clear: none; 
}
<div class="parentDiv">
    <div>
        <span>list of sources</span>
        <select size="10">
            <option />
            <option />
            <option />
        </select>
    </div>
    <div>
        <span>list of destinations</span>
        <select size="10">
            <option />
            <option />
            <option />
        </select>
    </div>
</div>

Answer №2

In the modern world, utilizing flexbox can help us align those divs in a more efficient manner.

.container {
    display: flex;
}
<div class="container">
    <div>
        <span>source list</span>
        <select size="10">
            <option />
            <option />
            <option />
        </select>
    </div>

    <div>
        <span>destination list</span>
        <select size="10">
            <option />
            <option />
            <option />
        </select>
    </div>
</div>

Answer №3

<div>
<div style="float:left;width:45%;" >
    <span>list of sources</span>
    <select size="10">
        <option />
        <option />
        <option />
    </select>
</div>

<div style="float:right;width:45%;">
    <span>list of destinations</span>
    <select size="10">
        <option />
        <option />
        <option />
    </select>
</div>
<div style="clear:both; font-size:1px;"></div>
</div>

Utilize 'clear' to avoid the float bug (height distortion of outer Div).

style="clear:both; font-size:1px;

Answer №4

To align the divs correctly, make sure to float them in the specified direction such as left or right.

Answer №5

To display both lists together, wrap them in a container:

.container{ 
    float:left; 
    width:100%; 
}
.container div{ 
    float:left;
}
<div class='container'>
    <div>
        <span>source list</span>
        <select size="10">
            <option />
            <option />
            <option />
        </select>
    </div>
    <div>
        <span>destination list</span>
        <select size="10">
            <option />
            <option />
            <option />
        </select>
    </div>
</div>

Answer №6

Include a class in each of the division elements:

.left, .right {
    float: left;
    width: 48%;
    margin: 0;
    padding: 0;
}
.left {
    margin-right: 4%;
}
<div class="left">
    <span>left side list</span>
    <select size="10">
        <option />
        <option />
        <option />
    </select>
</div>
<div class="right">
    <span>right side list</span>
    <select size="10">
        <option />
        <option />
        <option />
    </select>
</div>

This approach uses percentages, which may not always be the most reliable. Consider using pixel-based widths for better consistency. Adjusting margin and padding sizes might also be necessary.

For added control, you can wrap the HTML in a container div and apply this CSS:

.container {
  overflow: hidden;
}

By doing so, you prevent content from wrapping around the floated elements.

Answer №7

Don't use float anymore, opt for <code>display: flex;
:

For example:

.parent-div{ display: flex; }

To specify the direction, use flex-direction: row/column;. If there is no space, items will flex-wrap: wrap/nowrap;

Explore more properties here.

Answer №8

To position two divs side by side on the same row, you can utilize this CSS code:

#leftDiv {
    float:left;
    margin-right:20px;
}

#rightDiv {
    float:left;
}
<div id="leftDiv">
Left Element
</div>
<div id="rightDiv">
Right Element
</div>

Answer №9

<html>
<head>
<style type="text/css">
#floatingDivs{float:left;}
</style>
</head>

<body>
<div id="floatingDivs">
    <span>list of sources</span>
    <select size="10">
        <option />
        <option />
        <option />
    </select>
</div>

<div id="floatingDivs">
    <span>list of destinations</span>
    <select size="10">
        <option />
        <option />
        <option />
    </select>
</div>

</body>
</html>

Answer №10

To achieve your goal, it is recommended to utilize position instead of floating elements:

Check out this example for reference: http://jsfiddle.net/aas7w0tw/1/

Start by assigning a relative position to the parent element:

position: relative;

Then, place the child elements in absolute position:

position: absolute;

This approach will also allow you to have more control over the dimensions of your components.

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

What is the best way to assign a class to a child element when the rest of the children are not visible

Looking for a CSS-only solution, I have a simple task involving filtering div elements. The goal is to display an error message when none of the filtered divs match the selected criteria. HTML Structure: <div id="listHolder"> <div class="lis ...

PHP: Retrieve and store XML data for one-time use within a session

I have implemented Simplexml within a class to load specific nodes from my xml file into html. <?xml version="1.0" encoding="UTF-8"?> <note> <item> <title>Reminder</title> </item> <item> ...

Utilizing the HTML button element within an ASP file combined with JavaScript can lead to the reloading of the

I am attempting to create a JavaScript function for an HTML button element. However, I have noticed that it causes the page to reload. Strangely, when I use an input with a button type, everything works perfectly. What could be causing this issue and why a ...

Implementing initial state checks for Alpine.js checkboxes based on x-modal is not functioning properly

Upon loading alpinejs, for some reason all checkboxes become unchecked. It's perplexing and I can't figure out why. <div x-data="{ colors: [orange] }"> <input type="checkbox" value="red" x-model="co ...

Is it feasible to obtain multiple tag-name indexes using JavaScript?

Exploring the table search function provided by W3Schools has brought up an interesting question in my mind. Is it feasible to simultaneously retrieve multiple indexes using getElementsByTagName and conduct a search across the entire table instead of just ...

Using an inline-block display and pseudo-elements for creating vertically aligned content

Currently, I am utilizing display:inline-block and pseudo-elements (::before, ::after) to achieve vertical alignment in the middle, but unfortunately, it seems to be ineffective. The pseudo-element ends up taking a whole row of space even with a width set ...

How can I ensure perfect center alignment in CSS?

Is there a preferred method for centering elements using CSS? <div style="position: absolute; left: 50%;"> <div style="position: relative; left: -50%"> Centered content </div> </div> or simply <div style=" ...

What is the best way to display an error message for an invalid input using JavaScript?

I am struggling to implement error messages for my form inputs using JavaScript. My attempts so far have not been successful and I need guidance on the correct approach. I want to show an error message and prevent the form from being submitted if any erro ...

Checking if the iframe location has been modified using Jquery

Looking to determine if the location of an iframe has been modified. function checkLocation() { setInterval(function(){alert("Hello")},3000); if (document.getElementById("myiframe").src = 'http://www.constant-creative.com/login';) { } ...

Guide to launching a web browser within a Phonegap app with a button click event

We are developing a PhoneGap application using HTML and JavaScript. Our goal is to integrate PayPal's website so that users can make payments with a simple button click event within the app, and then seamlessly return back to the application afterward ...

Encountering a "Author.p12 file not found" error while developing a simple hello world application on Blackberry platform

On a quest to develop a Hello world application using HTML5 with WebWorks SDK v2.0 Beta for Blackberry 10 OS. After creating the project and installing the Blackberry simulator, I encountered an issue when trying to Build and install from the GUI of WebWo ...

Unexpected lack of parameters in a function triggered by an event listener's callback

I am currently working with a functional react component where I am attempting to attach events to multiple elements simultaneously. However, I am encountering issues with passing a trigger element to a function. export default function Header() { cons ...

Hide popup using HTML when clicking outside of it

Currently, I have implemented Bootstrap Popover with HTML content using the code snippet below. This code is based on the answer provided at . $(".song-status-link").popover({ html: true, placement: 'bottom', ...

"Using Mxgraph's getPrettyXml function does not retrieve the value of a custom

I’m having trouble with mxgraph’s getPrettyXml() not capturing the value of Custom elements. In my customized template, it looks like this: <add as="symbol"> <Symbol label="Symbol" description="" href="" data="{[hi :bill]}"> &l ...

Tips for displaying unformatted text using HTML

Is there a way to display a string in HTML with all of its escape sequences preserved? For example: eipt No. : 1995,\nmount 935\nAdm. : 17/06/2016 INSTRUCTIONS : 1) This card is valid only for the student's name & for the period indic ...

Creating a Timeout Function for Mobile Browsers in JavaScript/PHP

Currently, I am developing a mobile-based web application that heavily relies on AJAX and Javascript. The process involves users logging in through a login page, sending the data via post to the main page where it undergoes a mySQL query for validation. If ...

What strategies can be implemented to enhance the performance of this jQuery slider?

I have successfully created an image carousel. https://i.sstatic.net/CbypXxRr.png Encountered Challenges: The last slide is displayed, but the counter shows the first slide. How can I resolve this issue? When clicking play, it continues to advance ...

Interactions with Various Servlets through Diverse Forms within a single JSP document

I am currently dealing with an index.jsp file that contains two different types of forms. <form action="searchpath" name="searchForm" method="get"> <p>BedType</p> <select name="bedType"> <jsp:include page="/WEB-INF/embed ...

What is the most effective method for generating an HTML sitemap for a website boasting more than 40,000 web pages?

After successfully creating an XML sitemap for my website pages, I am now in need of an HTML version to enhance user experience and improve SEO by ensuring every page is linked to. My PHP website features individual pages for every village, town, county, ...

Display content from a PHP page inside a Twitter Bootstrap modal

I have set up a modal pop-up window to showcase all comments on my blog posts. However, I am facing an issue where I am unable to view individual comments by clicking on the respective link. <!-- Modal --> <div class="modal fade" id="myModal" ...