Positioning two distinct Div elements using CSS

In an attempt to align two lists within a div, I have one list containing .jpg files and another with text files.

<!DOCTYPE html>
<html>
<style>
    .left{height:auto; float:left;}
    .right{height:auto; float:right;}

</style>
<body>
<br>
<br>
<?php
    $files = scandir('files');
    sort($files); // this does the sorting
    foreach($files as $file){
        $extension = pathinfo($file,PATHINFO_EXTENSION);
            if($extension == 'jpg')
                echo'<br><div class="left">'.$file .'</div>';
    }

    $files2 = scandir('files');
    sort($files2); // this does the sorting
    foreach($files2 as $file2){
        $extension2 = pathinfo($file2,PATHINFO_EXTENSION);
            if($extension2 == 'txt')
                echo'<br><div class="left">'.$file2 .'</div>';
    }
?>

</body>
</html>

My goal is to horizontally align them in the center of the browser window like so:

                       test.jpg  test.txt
                       test2.jpg test2.txt
                       here.jpg  here.txt

However, currently I am seeing this output:

test.jpg
test2.jpg
here.jpg
test.txt
test2.txt
here.txt

Despite using float left. I have experimented with float:right for the text files, but the alignment remains off.

I have also tried adding margin:auto;,display inline-block;

But these adjustments do not seem to rectify the issue.

A modified markup...

<!DOCTYPE html>
<html>
<style>
    .left{float:left; height:auto;}
    .right{float:left; height:auto;}

</style>
<body>
<br>
<br>

            test.jpg test.txt<br>
            test2.jpg test2.txt<br>
            here.jpg  here.txt<br>

</body>
</html>

PHP functionality is necessary as I need to scan specific directories, aiming for a visually centered file listing.

Answer №1

You may want to consider utilizing flexbox for this scenario

Here is a sample code snippet to demonstrate:

.container {
  display: flex;
  flex-wrap: wrap;
  border: 1px solid black;
  justify-content: center;
}
.container div {
  width: 40%;
  text-align: center;
}
<div class="container">
  <div>
    <img src="http://www.freedigitalphotos.net/images/img/homepage/87357.jpg" height="20px" width="20px"></img>
  </div>

  <div>
    text1
  </div>

  <div>
    <img src="http://www.freedigitalphotos.net/images/img/homepage/87357.jpg" height="20px" width="20px"></img>
  </div>
  <div>
    text2
  </div>

  <div>
    <img src="http://www.freedigitalphotos.net/images/img/homepage/87357.jpg" height="20px" width="20px"></img>
  </div>
  <div>
    text3
  </div>
</div>

I trust this information proves to be useful

Answer №2

A common method is to align them to the left and specify their width. This approach is typically seen in basic grid systems.

.left {
  background: #ccc;
  float: left;
  width: 50%;
}

.right {
  background: #666;
  float: left;
  width: 50%;
}
<div class="left">
  content on the left side
</div>
<div class="right">
  content on the right side
</div>

Answer №3

To properly align elements using float left and float right, you need to wrap everything in a div tag. Placing the elements within another Div tag will help achieve this alignment.

Here is an example:

<div id='wrap'>

<div id='left'>test.jpg</div>
<div id='right'>test.txt </div>

</div>

Ensure that the wrap div is sized appropriately to hold the other two divs side by side. If the wrap width is set to 1000px, then the inner divs should not exceed 1000px combined (you can use percentages for more flexibility).

The CSS styling for this setup may look like this:

#wrap {
width:1000px;
}

#left{
float:left;
width:50%;
}

#right{
float:right;
width:50%;
}

To center the content, simply add the following to the 'wrap' CSS:

margin-left:auto;
margin-right:auto;
display:block;

For better organization, consider adding a <head> section to your code if it's missing.

Answer №4

Consider organizing the 2 divs by placing them inside a parent div like this:

<div class="container">
   <div class="left"></div>
   <div class="right"></div>
</div>

Also, ensure that the classes of the divs are correct as there are currently 2 left classes.

https://i.sstatic.net/PMrn6.png Link to this fiddle

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

creating an interactive table with the help of the useState hook

I'm new to JavaScipt and ReactJS, so I have a question that may seem obvious but I can't seem to figure it out. I am attempting to display my array in a table using useState, but currently I can only show the header. Additionally, if anyone know ...

Creating a visually appealing table in HTML or experimenting with a tableless design can greatly enhance your website's layout

Presenting data with headers in HTML has been a challenge for me. Below is the portion of the header I'm having difficulty with. Although I've successfully rotated the text, the issue I'm facing now is text overlap. Here's the code st ...

Storing HTML file input in an SQL database as a blob type: A Step-by-Step Guide

Currently, I am in the process of designing a website that includes forms for user submission. The input data provided by the users is stored in a SQL server database. While text, numbers, and dates are successfully being stored, I am encountering an issue ...

What is the proper way to showcase an element positioned absolutely within a Dialog window using material-ui?

My current project involves using React, TypeScript, StyledComponent, and MaterialUI. I am looking to add a button to a Dialog window (material-ui) similar to the design shown in this example image. However, when I apply position: absolute to the button, ...

The font color fails to change when hovering over

I'm experiencing an issue with the styling of my input box. When the user starts typing, a div containing a list of possible clubs is displayed. Currently, I am having trouble with the .clubLink:hover class and its background color. CSS: <style& ...

Problem with the show/hide feature on jQuery. Automatically scrolls to the beginning of the page

On my website, I have successfully implemented two basic Show / Hide links that are working great. Here is the HTML code: <!DOCTYPE html> <html lang="en"> <head profile="http://gmpg.org/xfn/11"> <meta http-equiv="Content-Type" conte ...

Learn how to effectively share an image using the Math.random function

Is there a way to display a random number of images on a webpage using JavaScript? Let's say we want to show X number of images, where X is a randomly generated number. For the sake of this example, let's set X to be 10. <input class="randomb ...

PHP file is functioning properly, yet no Ajax response received

I have a straightforward sign-up form for a mailing list. It sends the user's email address to a store-address.php file using jQuery's ajax object to make the request and receive a response. The issue I'm facing is that I'm not receivi ...

AngularJS users are experiencing issues with the "See More" feature not functioning as expected

One of my tasks involves dealing with a block of text that needs to be truncated using ellipsis. To achieve this, I am utilizing jquery dotdotdot. For more information on dotdotdot, please refer to the documentation. I have created a straightforward dire ...

Guide to choosing the directory for Visual Studio's compilation of LESS files

I currently use VS 2012 on my Windows 7. It seems that by default, it compiles LESS files to CSS in the same directory as the LESS file when I save. However, I would prefer to have all my CSS files saved in a "css" directory located next to the "less" dir ...

Provide a unique <li> attribute for the JavaScript function to utilize

Is there a way to pass specific attributes from dropdown options to a javascript function? I have tried using .data() and .attr(), but the console keeps showing "undefined". Any suggestions on how to achieve this in a cleaner and simpler way would be gre ...

Can Bootswatch be installed using a package manager?

I have recently launched a small asp.net core website. Right now, all JS/CSS imports are from NPM and I am using webpack to bundle them together. Instead of sticking with the standard bootstrap template, I wanted to switch to bootswatch paper, but unfortu ...

What steps should I take to ensure that clicking this button triggers the API request and returns the data in JSON format?

I'm attempting to have the button with id 'testp' return an api request in json format, however it seems to not be functioning properly. You can find the HTML code link here: https://github.com/atullu1234/REST-API-Developer-1/blob/main/js-bu ...

having difficulty placing 3 pop-up windows on a single page

Struggling to implement multiple popups on my page, each with a unique ID assigned. Any assistance would be greatly appreciated. Here is the code snippet: .fa { font-size: 50px; cursor: pointer; user-select: none; } .fa:hover { font-size:20px; t ...

Removing empty commas from a string with JQuery

I am looking to utilize JQuery to eliminate any empty commas from a given string. Take a look at the provided images for further clarity. ...

Ensure that a DIV element stretches beyond the limits of its container

I am having an issue with a div that should overlap everything else as it functions as a menu. Despite trying various positioning tricks, the desired effect is not achieved. The div is only shown when hovering over a certain element. Here is the structure ...

Why is my CSS and Bootstrap full-page image not displaying properly?

I've been struggling to get a full-page image to display on my website and resize responsively across different screens. I've searched through w3schools and Stack Overflow for solutions, but no matter what I try, it just doesn't seem to work ...

What is the best way to make the final character in the input placeholder appear in red?

Here lies my input field https://i.sstatic.net/0mJyJt.png, I'm striving to make the last character '*' appear in bold red rather than blending into the background. Despite numerous attempts, I haven't been successful in achieving the d ...

manipulating dropdown visibility with javascript

I'm feeling a bit lost on how to structure this code. Here's what I need: I have 5 dropdown boxes, with the first one being constant and the rest hidden initially. Depending on the option chosen in the first dropdown, I want to display the corres ...

Issue with JAWS screen reader failing to announce aria-expanded status

My aim is to ensure that the collapsed state is accessible in both NVDA and JAWS, but I am encountering issues with it displaying properly in JAWS. Does anyone have any suggestions on how to rectify this? I have included images of the code and link list b ...