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.

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

How can you employ moment.js to display the most recent update time of mongoDB/mongoose?

I am facing an issue with displaying the last date/time when any entry in my table was edited and updated using moment.js. It seems to be updating the date/time in real-time rather than only showing when a database entry was modified. How can I ensure tha ...

Identify which anchor tag from the group with the matching class was selected and retrieve its unique identifier

There are multiple anchor tags with the same class in my code. <a href='' id='id1' class='abc'>Link 1</a> <a href='' id='id2' class='abc'>Link 2</a> <a href='&apos ...

Adapting padding based on the height of the browser to ensure optimal layout

I'm interested in adjusting the padding value of a button element based on the height of the browser window, not its width. I want to make sure that the padding adjusts proportionally to the browser window's height. Is this achievable using CSS? ...

What is the best method for rounding a decimal number to two decimal places?

Here is the JavaScript code I am using: $("input[name='AuthorizedAmount'], input[name='LessLaborToDate']").change(function () { var sum = parseFloat($("input[name='AuthorizedAmount']").val()).toFixed( ...

Unveiling Elements as You Scroll Within a Specified Area

I have implemented a jQuery and CSS code to reveal my contact form as I scroll down the page. However, I am facing an issue with setting a specific range for displaying the element while scrolling. Currently, I have only managed to handle the scrolling dow ...

Conceal a Component within an Embedded Frame

Hey there! I've been attempting to hide a header within an iframe, but it seems like my code isn't doing the trick. Could someone take a look and help me diagnose why the header is still visible? Thanks in advance! <iframe id="booking_iframe" ...

Loading elements of a webpage in a consecutive order

I am currently working on implementing a content slider within my Django application. The specific slider that I am using can be found at this link. One challenge that I am facing is the need to load close to one hundred 'contentDiv' pages, and I ...

Creating a dynamic feature in React where multiple icons change color individually when hovered over, all implemented

I'm looking to customize the icons in my footer by changing their colors when users hover over them. I have already created a CSS class with the necessary hover effects, but now I want to pass a parameter in my JSX file that specifies which color shou ...

What is the best way to determine the length of an array using input from an HTML form?

Currently, I am in the process of developing a PHP file and one feature that I would like to implement is creating an array as illustrated in Example 1. As far as my understanding goes, an Array functions like a list where items can be added as shown in Ex ...

Retrieving a PHP script from within a DIV element

I am seeking assistance to successfully load a PHP file from a DIV call. Below is the code I have used: <html> <head> </head> <body> <script class="code" type="text/javascript"> $("#LoadPHP").load("fb_marketing ...

Get hexa values from a colorpicker and use them as a background in an angularjs application

I'm currently working on an angularJS project and I've implemented an HTML color picker. My goal is to assign the selected color from the color picker to my div element, ensuring that the values are in hexadecimal format. Below is a snippet of my ...

Revamped Web Presentation: The Dynamic Blend

I'm struggling to arrange a section in the same way as shown in this image. https://i.stack.imgur.com/QVDHn.png I have been using bootstrap 4 and have experimented with rows and columns, but haven't been able to achieve the desired layout. Curr ...

The web application located on the server is having trouble recognizing and loading the stylesheet

After deploying the web app on MS Server 2008 R2, I noticed that it is not reading the style sheets on any of the pages. The masterpage only contains a ToolkitScriptManager with no styles. However, the source code shows the style links and accessing them d ...

Production environment experiencing issues with jQuery tabs functionality

Currently, I have implemented jQuery tabs on a simple HTML page. The tabs are functioning correctly and smoothly transitioning between different content sections. However, upon integrating this setup into my actual project environment, I encountered an is ...

How can one determine the most accurate box-shadow values?

I am trying to extract the precise box-shadow parameters from a CSS style rule generated by the server. My main focus is determining whether the element actually displays a visible shadow or not. There are instances where the shadow rule is set as somethi ...

Problems arising from positioning elements with CSS and organizing list items

I've been working on creating a navigation sidebar using Angular and Bootstrap, but I'm having trouble getting my navigation items to display correctly. APP Component HTML <div class="container-fluid"> <div class="row" id="header"&g ...

HTML: A peculiar table setup with an image displayed on the right side and text aligned to the left side. Strangely, the

Just starting out and seeking assistance <table style="border:none" cellpadding="0" cellspacing="0"> <tbody> <tr> <td style="border:none" valign="top" vertical-align:"top"; width="20%"> <div><img class= ...

Encountering difficulties linking to a stylesheet or a script in an HTML file delivered by Express server

Currently, I'm facing the challenge of breaking down my Express app code into multiple files. Unfortunately, I am unable to utilize <link href> or <script src> for linking stylesheets or scripts. Below is the relevant snippet from my inde ...

Is it considered fashionable to utilize HTML5 data attributes in conjunction with JavaScript?

Currently, I am utilizing HTML5 data attributes to save information such as the targeted DOM element and to initialize events using jQuery's delegation method. An example of this would be: <a href="#" data-target="#target" data-action="/update"> ...

Center the text within the div and have it expand outwards from the middle if there is an abundance of text

I have a div that is in the shape of a square box and I want it to be centered. No matter how much text is inside, I would like it to remain in the middle. I am using JQuery to create my square box and would like to center it using CSS. Here is my code: &l ...